node.js - 使用 twilio 的 whatsapp api 检索发件人的 whatsapp 号码和/或消息?
问题描述
我已经为 twilio 的 whatsapp api 设置了一个带有 twilio 沙箱的 webhook。目前,它所做的只是向硬编码的 whatsapp 号码(我的)发送消息。一切正常,因为我可以在收到消息时发送消息,但是,我无法检索发件人的号码或他发送的消息。我曾尝试查看该req
对象(见下文),但我还没有找到我要找的东西。例如req.body = {}
,没有“发件人”或“收件人”或“发件人”。
这就是我现在所拥有的。
'use strict';
const express = require('express')
const router = express.Router()
const whatsAppConfig = {
accountSid: process.env.WHATSAPPACCOUNTSID || 'AC73XXXXXXXXXXXXXXXXXXXXXXX',
authToken: process.env.WHATSAPPAUTHTOKEN || 'd3XXXXXXXXXXXXXXXXXXXXXXXXX',
from: process.env.WHATSAPPFROM || 'whatsapp:+1**********',
};
const client = require('twilio')(whatsAppConfig.accountSid, whatsAppConfig.authToken);
router.post('/whatsapp', (req, res) => {
console.log(req); //this is the object that contains the request, so all the information I need should be in here
// Return a '200 OK' response to all events
res.status(200).send('EVENT_RECEIVED');
let message = "THIS IS A TEST MESSAGE";
sendWhatsAppMessage(whatsAppConfig.from, message, "whatsapp:+1******"); //This works
});
//
function sendWhatsAppMessage(from_number, message, to_number) {
client.messages
.create({
from: from_number,
body: message,
to: to_number
})
.then(message => console.log(message.sid));
}
module.exports = router
有什么我遗漏的东西,甚至可能吗,我应该期待另一个参数吗?
解决方案
// Body Parser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
推荐阅读
- loops - Puppet 3.8.# inline_template 无法拾取 hieradata
- c++ - 正在运行的 Docker 容器中不允许操作 chmod 0777
- javascript - 如何在 Axios 实例中制作动态 baseURL
- php - 为关联数组中的器官赋予价值 - php
- javascript - 在块上挂起事件时,如何更改所需的元素
- python - Django Rest Framework 只读字段问题
- php - woocommerce自定义切换循环
- lotus-notes - LotusScript:通过 POST 方法 HTTP 发送数据
- autodesk-forge - 运行 hide() 函数时没有隐藏任何内容
- c++ - cc1plus.exe - 未找到入口点