如何阅读发送到我的 Twilio phone 号码的短信?

How do I read an SMS message sent to my Twilio phone number?

我在 Twilio 的 API 文档中找不到这个。

我使用 NodeJS。当用户向我的 Twilio 号码发送短信时,我想使用 NodeJS 来检索消息。

我可以在 Twilio 的 API 参考资料中的什么地方找到它?

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

当用户向您的 Twilio 号码发送短信时,可以通过两种方式获取该消息。

第一个是最有效的,使用了 webhooks。购买 Twilio 号码时,您可以为 Twilio to make a request to every time it receives an SMS for that number 配置 URL。该请求将包含一堆参数,包括消息来源的号码和消息的正文。您可以使用 Node.js 应用程序接收请求,这是一个使用 express 的简单示例:

var app = require("express")();
var bodyParser = require("body-parser");

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

app.post("/messages", function(req, res) {
  console.log(req.body.Body); // the message body
  console.log(req.body.From); // the number that sent the message
  res.send("<Response/>"); // send an empty response (you can also send messages back)
});

app.listen(3000); // listens on port 3000

在开发像这样使用 webhook 的应用程序时,I recommend tunneling to your local machine using ngrok

获取消息的另一种方法是使用 Twilio's REST API. You can list all the messages to your numbers using the messages resource。在 Node.js 中,这看起来像:

var client = require('twilio')(accountSid, authToken);

client.messages.list(function(err, data) {
    data.messages.forEach(function(message) {
        console.log(message.body);
    });
});

如果这有帮助,请告诉我。