【问题标题】:log incoming message twilio node.js记录传入消息 twilio node.js
【发布时间】:2021-05-01 18:04:40
【问题描述】:

我有这个基本功能,可以将消息发送回给我的 Twilio 号码发送消息的用户。

因此,如果号码 1234567890 向我的 twilio 号码发送一条消息,它会向 1234567890 发送一条消息。但是,我希望能够记录用户发送给我的内容。因此,如果他们给我发送hi,那么我可以使用它来查询我的数据库并向他们发送消息。

这是我的功能:

app.post('/sms', (req, res) => {
const twiml = new MessagingResponse();

twiml.message('The Robots are coming! Head for the hills!');

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

【问题讨论】:

    标签: javascript node.js twilio


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    您似乎正在使用 Node.js 和 Express 来接收来自 Twilio 的传入 webhook。 When Twilio sends this webhook it includes a number of parameters that describe the message you received。请求以application/x-www-form-urlencoded 格式发出,因此您需要设置 Express 以解析请求正文中的这些参数。你可以使用内置的Express urlencoded middleware

    const express = require('express');
    const app = express();
    
    app.use(express.urlencoded());
    

    成功解析请求后,即可从req.body读出参数。例如,发送邮件的号码是req.body.From,邮件正文是req.body.Body。例如:

    app.post('/sms', (req, res) => {
      const { Body, From } = request.body;
      console.log(`Message from: ${From}: ${Body}`);
    
      const twiml = new MessagingResponse();
    
      twiml.message('The Robots are coming! Head for the hills!');
    
      res.writeHead(200, {'Content-Type': 'text/xml'});
      res.end(twiml.toString());
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2019-05-19
      • 2012-02-01
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多