Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ask/addQuestion type signatures #1818

Merged
merged 3 commits into from
Oct 28, 2019

Conversation

etiennellipse
Copy link
Contributor

@etiennellipse etiennellipse commented Oct 1, 2019

Developing with TypeScript, I stumbled upon issues with the ask & addQuestion signature. As expected by the handlers parameter, I passed a single BotkitConvoTrigger object instead of a function, which caused this error:

message.collect.options.forEach((o) => { if (!o.type) { o.type = 'string'; } });
                                ^
TypeError: Cannot read property 'forEach' of undefined
    at BotkitConversation.addQuestion (/Users/etienne/Code/cerebro/node_modules/botkit/src/conversation.ts:379:33)
    at BotkitConversation.ask (/Users/etienne/Code/cerebro/node_modules/botkit/src/conversation.ts:333:14)

Checking the code, it seems obvious that a single BotkitConvoTrigger is not expected; it is either a BotkitConvoHandler, or an array of BotkitConvoTriggers. Otherwise, the conditional block on lines 367:376 fails.

Here is an example of TypeScript code that validates correctly for the compiler, but crashes at runtime:

const { BotkitConversation } = require("botkit");

module.exports = function (controller) {
    const dialog = new BotkitConversation("intro", controller);

    dialog.ask({
        text: ["Need anything?"],
        quick_replies: [
            {
                title: "Oui",
                payload: "Oui",
            },
            {
                title: "Non",
                payload: "Non",
            },
        ],
    }, {
        default: true,
        handler: async (response, convo, bot) => {
            bot.say("It's dead, Jim.");
        },
    }, "confirmAide");

    controller.addDialog(dialog);
};

With this PR, it now just fails the type checking as it should.

@benbrown benbrown added this to the 4.6 milestone Oct 2, 2019
@designbyadrian
Copy link

How do we go around this problem before 4.6?

@etiennellipse
Copy link
Contributor Author

@designbyadrian It's mostly a syntactic issue when working in TypeScript.

Make sure to pass either a function or an array of handler objects (NOT a single handler object) and it works.

@benbrown benbrown merged commit ee95999 into howdyai:master Oct 28, 2019
@benbrown
Copy link
Contributor

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants