IBM Watson 与 Twilio SMS 机器人的集成

IBM Watson integration with Twilio SMS bot

我在这里使用这个示例代码: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 短信而不是 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({});

这里是 Twilio 开发人员布道者。

我注意到有问题的服务器 only parses incoming requests as JSON. Twilio sends webhooks in the format application/x-www-form-urlencoded 所以你需要告诉 bodyParser 也解析这些类型的请求。

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

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

那么您应该能够阅读 Twilio 请求的正文。