IBM Watson与Twilio SMS bot集成

时间:2018-03-15 21:15:54

标签: express twilio watson-conversation

我在此处使用此示例代码:https://github.com/watson-developer-cloud/botkit-middleware

我基本上已经设置了twilio,watson与活动工作流程的对话。我的问题是,当发送文本消息到twilio时,twilio向我使用ngrok公开的webhook发送POST请求。但是当我的代码接收到POST请求时,它会被空体接收。没有文字,用户,从,到。管他呢。我使用的是Twilio SMS Bot,而不是Twilio IPM Bot。

来自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: [] } } }

来自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

利用twilio sms代替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({});

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

我注意到有问题的服务器only parses incoming requests as JSONTwilio sends webhooks in the format application/x-www-form-urlencoded因此您需要告诉bodyParser解析这些类型的请求。

我将以下内容添加到server.js

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

然后你应该能够阅读Twilio请求的主体。