使用 twilio 的 whatsapp api 检索发件人的 whatsapp 号码 and/or 消息?
Retrieving the sender's whatsapp number and/or the message with twilio's whatsapp api?
我已经使用 twilio 沙箱为 twilio 的 whatsapp api 设置了一个 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 }));
我已经使用 twilio 沙箱为 twilio 的 whatsapp api 设置了一个 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 }));