gratifyguy / botkit-mock

Write tests for Botkit.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Facebook bot.identity is undefined

ouadie-lahdioui opened this issue · comments

Hello,

Facebook messenger doesn't support direct mention, so when i want to test some facebook conversations with this awesome botkit-mock, i got this error :

// BotmockWorker.js:66 -> var direct_mention = new RegExp('^\<\@' + bot.identity.id + '\>', 'i');
TypeError: Cannot read property 'id' of undefined

In BotmockWorker.js you try to prune off the bot's name and trim whitespaces, in my case bot.identity is undefined so bot.identity.id throw an error.

// Example of code
beforeEach(() => {
        let controller = Botmock({});
        bot = controller.spawn({type: 'facebook'});

        controller.hears(['hi'], 'direct_message', function(bot, message){
            bot.startConversation(message, function(response, convo){
                convo.ask('hello there', function(response, convo){
                    convo.say('..user typed any text after `hi`');
                    convo.ask('here a question', function(response, convo){
                        convo.say('here an answer');
                        convo.next();
                    });
                    convo.next();
                });
            })
        });
    });

it('should return `help message` if user types `help`', (done) => {

        let sequence = [
            {
                type: null, // direct_message, message_received, interactive_message_callback, defaults to direct_message
                user: 'someUserId', // this.userInfo.slackId
                channel: 'someChannel', //his.userInfo.channel
                messages:[
                    {
                        text: 'hi',
                    },
                    {
                        text: 'its true',
                        deep: 1,
                        isAssertion:true,
                    }
                ]
            }
        ];

        bot.usersInput(sequence).then((message) => {
            console.log(message);
            expect(message.text).toBe('help message');
            done();
        });
    });

Oups ! this fixed in the latest version 👍