howdyai / botkit

Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
MIT License
11.48k stars 2.29k forks source link

can't receive messages using twilio and whatsapp #1771

Closed AutomataVM closed 5 years ago

AutomataVM commented 5 years ago

Hello! I was using the botkit package to make a simple chatbot using nodejs, Twilio and Whatsapp. I can receive messages from Whatsapp on my application just fine, the problem is when I try sending a reply to that message using bot.reply. The message that appears on the console output whenever the controller hears the message is:

(node:8880) UnhandledPromiseRejectionWarning (node:8880) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

The problem seems to be a missing catch block, though I don't know where it is supposed to be, inserted into, the controller? the bot.reply?

Here's my code:

const { Botkit } = require('botkit');
const { BotkitCMSHelper } = require('botkit-plugin-cms');
const { TwilioAdapter } = require('botbuilder-adapter-twilio-sms');

require('dotenv').config();

const adapter = new TwilioAdapter({
    twilio_number: process.env.TWILIO_NUMBER,
    account_sid: process.env.TWILIO_ACCOUNT_SID,
    auth_token: process.env.TWILIO_AUTH_TOKEN,
});

const controller = new Botkit({
    webhook_uri: '/api/messages',
    adapter: adapter,
});

if (process.env.cms_uri) {
    controller.usePlugin(new BotkitCMSHelper({
        uri: process.env.cms_uri,
        token: process.env.cms_token,
    }));
}

controller.ready(() => {

    // load traditional developer-created local custom feature modules
    controller.loadModules(__dirname + '/features');

    /* catch-all that uses the CMS to trigger dialogs */
    if (controller.plugins.cms) {
        controller.on('message,direct_message', async (bot, message) => {
            console.log(bot,message);
            let results = false;
            results = await controller.plugins.cms.testTrigger(bot, message);

            if (results !== false) {
                // do not continue middleware!
                return false;
            }
        });
    }

    controller.hears('.*','message', (bot, message) => {
        console.log('recibi el siguiente mensaje: ');
        console.log(message);
        bot.reply(message,'recibi el mensaje fuerte y claro');
    });
});

controller.webserver.get('/', (req, res) => {
    res.send(`This app is running Botkit ${ controller.version }.`);
});

I use ngrok to set up a server that can be accesed from outside. Afterwards, I configured the webhooks inside the phone numbers and whatsapp webhook. Also, I am using the whatsapp phone number instead of the trial number for the Twilio settings.

What was the result you received?

UnhandledPromiseRejectionWarning message

What did you expect?

the message to appear in the whatsapp conversation

benbrown commented 5 years ago

this code should be changed, from:

controller.hears('.*','message', (bot, message) => {
        console.log('recibi el siguiente mensaje: ');
        console.log(message);
        bot.reply(message,'recibi el mensaje fuerte y claro');
    });

to:

    controller.hears('.*','message',async (bot, message) => {
        console.log('recibi el siguiente mensaje: ');
        console.log(message);
        try {
       await bot.reply(message,'recibi el mensaje fuerte y claro');
       } catch(err) {
         console.log('ERROR FROM TWILIO', err);
      }
    });

to

benbrown commented 5 years ago

Note the async and await

AutomataVM commented 5 years ago

ah, it was so easy after all! thank you for the tip on the try catch code. Still I receive the same output when the bot.reply line gets executed:

(node:9388) UnhandledPromiseRejectionWarning (node:9388) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:9388) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre cated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

could it be something on twilio's end? I configured both webhooks for the whatsapp option and the phone number I am using towards my ngrok server in this way:

https://numbers.ngrok.io/api/messages

AutomataVM commented 5 years ago

seems like it is something on twilio's', I keep getting this error

Error - 11200

Properties DEBUG EVENT SID NOe074bd2d2d32e55a467689617c8b6fff SERVICE SID RESOURCE SID SMfc372db44f9d9ec2311a1d5954e61075 TIMESTAMP 2019-08-16 12:08:10 UTC MESSAGE The URI scheme, of the URI null, must be equal (ignoring case) to 'http', 'https', 'ws', or 'wss'

not sure what it means though, guessing it is related to the url of the webhook

edit: it is not webhook related I think, changing it back to the https:// makes it work, maybe I am missing something in the bot.reply function??

benbrown commented 5 years ago

What is the format of the message you are sending back to Twilio?

Is there a URL in it?

AutomataVM commented 5 years ago

pretty sure it is the same as format the message parameter in the hears callback. Here's a bit of it:

{ SmsMessageSid: 'SM83f047ab14a65e47445fd4591863c1f0', NumMedia: '0', SmsSid: 'string', SmsStatus: 'received', Body: 'dasdas', To: 'whatsapp:+number', NumSegments: '1', MessageSid: 'string', AccountSid: 'string', From: 'whatsapp:+number', ApiVersion: '2010-04-01', type: 'message', user: 'whatsapp:+number', text: 'dasdas', channel: 'whatsapp:+number', value: undefined, reference: { activityId: 'string', user: { id: 'whatsapp:+number' }, bot: { id: 'whatsapp:+number' }, conversation: { id: 'whatsapp:+number' }, channelId: 'twilio-sms', serviceUrl: undefined }, . . . .

there's a serviceUrl field that is undefined, otherwise no URL

AutomataVM commented 5 years ago

did you got any leads to it? regardless of the url, the main problem is that I can't catch the exception thrown.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

gp187 commented 4 years ago

This issue has not been solved!

kayprogrammer commented 2 years ago

Twilio should solve this issue

harshalcgaikwad commented 2 years ago

I am also facing the same issue when using Java + Twilio to send message on whatsapp. message reaches to application but fail during response

Macdeltahhatledcam commented 2 years ago

once had the same problem and the solution is to sign up on the ngrok website then grab the auth token key and synchronize it with the ngrok on your machine .It should work