【问题标题】:How can I get Twilio webhooks to work with Heroku?如何让 Twilio webhooks 与 Heroku 一起使用?
【发布时间】:2020-01-26 00:33:26
【问题描述】:

您好,我正在使用 Twilio 向多个电话号码发送短信。

当用户从我的 React.js 应用程序收到短信时,我希望他们能够回复该短信并让我的应用程序进行一些处理,然后发回短信通知处理已完成。

基于 Twilio web page

但是我不断收到 404 响应。

以下是此问题的详细信息:

该应用程序是在 Heroku 上运行的 REACT 应用程序

我的“server.js”文件中的重要内容

const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const path = require('path');

const mongoose = require('mongoose');

const sms = require('./routes/api/sms');

const app = express();


// body parser
let size = '50mb';
app.use(bodyParser.urlencoded({limit: size,extended: false}));
app.use(bodyParser.json({limit: size}));


// passport middleware
app.use(passport.initialize());

// passport config file.
// passport uses a strategy
require('./config/passport')(passport);

// use routes

app.use('/api/sms', sms);


// server static assests if in production
if (process.env.NODE_ENV === 'production') {
    // Set static folder
    app.use(express.static('client/build'));

    app.get('*', (req, res) => {
      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
    });
  }

const serverPort = require('./config/keys').serverPort;

const port = process.env.PORT || serverPort;

app.listen(port, () => console.log(`server running on port ${port}`));

这里是 sms.js 文件内容(“应该”响应网络挂钩调用的 URL)

router.post('/twiml', (req, res) => {

    let debugThis = true;
    if(debugThis){
        console.log('post api/sms/twiml');
        console.log(req.body);
    }

    const twiml = new MessagingResponse();

    if (req.body.Body == 'hi') {
        twiml.message('Hello!');
      } else if (req.body.Body == 'bye') {
        twiml.message('Goodbye');
      } else {
        twiml.message(
          'No Body param match, Twilio sends this in the request to your server.'
        );
      }

      res.writeHead(200, { 'Content-Type': 'text/xml' });
      res.end(twiml.toString());

});

所以我认为,当我拨打电话时,我应该在 Twilio 中的电话号码上配置我应该用作网络挂钩的 URL,如下所示:(前面缺少的部分是 HTTPS://)

当我发短信给我的 Twilio 号码时,这是我在日志中得到的:

2020-01-25T23:53:17.755941+00:00 heroku[router]: at=info method=POST path="/sms/twiml" host=choremonger.herokuapp.com request_id=7d44f910-2850-4282-aa14-08270384e99a fwd="54.198.69.37" dyno=web.1 connect=36ms service=23ms status=404 bytes=393 protocol=https

换句话说:

我的 Twilio 号码是 555-555-5555 乔的电话号码是 111-111-1111

我设想消息如下所示:

从 555-555-5555 发送到 111-111-1111“任务 A 完成了吗”

RC 在 111-111-1111“任务 A 完成了吗”

从 111-111-1111 发送到 555-555-5555“是”

RC 在 555-555-5555 “是”

这是通过网络挂钩通知我的应用程序“是”已发送短信的地方。

从 555-555-5555 发送到 111-111-1111“任务 A 已关闭”

更新 1

我添加这个的原因是由于评论说网络挂钩 URL 不正确。

当我使用 Visual Studio Code 和 Postman 中的“npm run server”来检查 URL 时,我得到了这个:

Visual Studio 中的服务器输出

[nodemon] 1.18.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
server running on port 5000
DB connected
post api/sms/twiml
{ Body: 'hi' }

PostMan URL 调用:

http://localhost:5000/api/sms/twiml

邮递员正文:

{
    "Body" :"hi"
}

邮递员结果: 状态:200 OK

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>Hello!</Message>
</Response>

更新 2

当我从 Twilio 中的 webhook URL 中删除 'twilml' 并发短信时,我得到了这个:

2020-01-26T18:05:09.862565+00:00 heroku[router]: at=info method=POST path="/sms" host=choremonger.herokuapp.com request_id=8b0d0987-2907-4796-a8b2-d4e6d14e209a fwd="54.208.241.142" dyno=web.1 connect=0ms service=2ms status=404 bytes=387 protocol=https

当我把它放回去并再次发短信时,得到了这个:

2020-01-26T18:06:26.639155+00:00 heroku[router]: at=info method=POST path="/sms/twiml" host=choremonger.herokuapp.com request_id=0af446f3-4432-40fe-b6e6-1b64ecf6608c fwd="3.89.83.196" dyno=web.1 connect=0ms service=3ms status=404 bytes=393 protocol=https

更新 3

我认为这可能是由于 HTTPS 与 HTTP 的关系,所以我将 web 挂钩更改为 HTTP 并再次尝试...请求获取了 http 更改...但结果相同。

2020-01-27T03:13:51.506124+00:00 heroku[router]: at=info method=POST path="/sms/twiml" host=choremonger.herokuapp.com request_id=539604b1-3b08-47d9-a124-3d2e30596043 fwd="54.166.158.158" dyno=web.1 connect=1ms service=2ms status=404 bytes=393 protocol=http

更新 4

我去了 Twilio 的调试器并提取了一些信息......希望有人可以帮助指导我如何解决这个问题

电话号码和 SIDS 正确

谢谢!

【问题讨论】:

标签: heroku twilio webhooks phone-number


【解决方案1】:

好的...对于所有像我一样刚接触 Web 编程和 REACT 的人...

这就是原因:

这是我 server.js 文件中将 URL 放入世界中的行:

const sms = require('./routes/api/sms');

我在 Twilio 配置链接中的 URL 中缺少“api

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2013-11-30
    • 1970-01-01
    • 2010-10-10
    • 2013-11-25
    • 2012-04-19
    • 2013-07-02
    相关资源
    最近更新 更多