Bertrand Kintanar
Bertrand Kintanar

Reputation: 132

IBM Watson integration with Twilio SMS bot

I'm using this sample code here: https://github.com/watson-developer-cloud/botkit-middleware.

I have essentially setup twilio, watson conversation with an active workflow already. My problem is that when sending text message to twilio, twilio sends a POST request to a webhook that I exposed using ngrok. But when my code picks up the POST request it's being received with empty body. no text, user, from, to. or whatever. I'm using Twilio SMS Bot and not the Twilio IPM Bot.

from botkit

Initializing Botkit v0.6.11
info: ** No persistent storage method specified! Data may be lost when process shuts down.
info: ** Serving webhook endpoints for Twilio Programmable SMS at: localhost:5000/sms/receive
Twilio bot is live
Client server listening on port 5000
info: => Got a message hook
{}
{ raw_message: {},
  _pipeline: { stage: 'receive' },
  text: undefined,
  user: undefined,
  channel: undefined,
  from: undefined,
  to: undefined,
  timestamp: 1521132895282,
  sid: undefined,
  type: 'message_received',
  watsonData: { output: { text: [] } } }

from ngrok

ngrok by @inconshreveable                                                                                                                   (Ctrl+C to quit)

Session Status                online
Session Expires               3 hours, 23 minutes
Update                        update available (version 2.2.8, Ctrl-U to update)
Version                       2.2.3
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://********.ngrok.io -> localhost:5000
Forwarding                    https://********.ngrok.io -> localhost:5000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              8       0       0.00    0.00    0.02    0.29

HTTP Requests
-------------

POST /sms/receive              200 OK

changes made to leverage twilio sms instead of ipm.

const configuration = {
  account_sid: process.env.TWILIO_ACCOUNT_SID,
  auth_token: process.env.TWILIO_AUTH_TOKEN,
  twilio_number: process.env.TWILIO_NUMBER
};

const Botkit = require('botkit');

const controller = Botkit.twiliosmsbot(configuration);

var bot = controller.spawn({});

Upvotes: 0

Views: 519

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

I noticed the server in question only parses incoming requests as JSON. Twilio sends webhooks in the format application/x-www-form-urlencoded so you need to tell bodyParser to parse those types of requests too.

I'd add the following to server.js

app.use(bodyParser.urlencoded({ extended: true }));

Then you should be able to read the body of Twilio requests.

Upvotes: 1

Related Questions