Twilio 11200 HTTP检索失败 – 这是什么意思?

我在Twilio的HTTP检索失败错误上看到了几个问题。

在使用Express快速实现Node.js消息服务器之后,我开始一直出现这个错误。 它偶尔会滞后我的文本消息响应10-20秒,并显示在debugging器中。

我想知道我是否可能会在这里失去明显的东西。 由于我使用的是Twilio的示例代码,因此它似乎应该可以直接使用。

我的代码如下:

app.post('/sms', (req, res) => { const data = req.body const message = data.Body const sender = data.From console.log('We\'ve received a text...') console.log('Sender: ', sender) console.log('Message: ', message) const responses = [ 'Message #1.', 'Message #2.', 'Message #3.', 'Message #4.' ] client.sendMessage({ to: '+11231231234', // Any number Twilio can deliver to from: '+11234564567', // A number you bought from Twilio and can use for outbound communication body: responses[ Math.floor(Math.random() * 4) ] // body of the SMS message }, function(err, responseData) { //this function is executed when a response is received from Twilio if (!err) { // "err" is an error received during the request, if any console.log(responseData.from); // outputs "+14506667788" console.log(responseData.body); // outputs "Printing some stuff." } }); }) 

任何想法我做错了什么和/或如何解决这个问题? 谢谢!

Twilio开发者在这里传道。

通过查看您的代码,似乎您通过在Twilio控制台上设置webhook来响应短信。

当你使用webhook时,Twilio希望你的页面返回TwiML ,如果你的页面没有返回,你会得到1200个间歇性的检索错误。 您的网页目前不会返回任何内容。

好消息是,您可以通过使用TwiML来简化代码,并返回如下所示的内容:

 <?xml version="1.0" encoding="UTF-8"?> <Response> <Sms from="+14105551234" to="+14105556789">The king stay the king.</Sms> </Response> 

您可以使用NodeJS库来生成TwiML,或者像上面那样手动创buildTwiML。 在NodeJS中生成TwiML的例子如下:

 app.post('/message', function (req, res) { var resp = new twilio.TwimlResponse(); resp.message('some message you wanna add'); res.writeHead(200, { 'Content-Type':'text/xml' }); res.end(resp.toString()); }); 

希望这可以帮助你