我的nodejs代码和到官方whatsapp business API的连接有问题。
机器人正确连接了webhook,消息正确到达服务器,但我实现的使其响应的代码无效,我从上到下检查了代码,但我找不到故障。
我把代码留给你们,让你们有更多的背景:
whatsappController.js:
const fs = require("fs");
const myConsole = new console.Console(fs.createWriteStream("./logs.txt"));
const whatsappService = require("../services/whatsappService")
const VerifyToken = (req, res) => {
try {
var accessToken = "456E7GR****************************";
var token = req.query["hub.verify_token"];
var challenge = req.query["hub.challenge"];
if(challenge != null && token != null && token == accessToken){
res.send(challenge);
}
else{
res.status(400).send();
}
} catch(e) {
res.status(400).send();
}
}
const ReceivedMessage = (req, res) => {
try {
var entry = (req.body["entry"])[0];
var changes = (entry["changes"])[0];
var value = changes["value"];
var messageObject = value["messages"];
if(typeof messageObject != "undefined"){
var messages = messageObject[0];
var text = GetTextUser(messages);
var number = messages["from"];
myConsole.log("Message: " + text + " from: " + number);
whatsappService.SendMessageWhatsApp("The user say: " + text, number);
myConsole.log(messages);
myConsole.log(messageObject);
}
res.send("EVENT_RECEIVED");
}catch(e) {
myConsole.log(e);
res.send("EVENT_RECEIVED");
}
}
function GetTextUser(messages){
var text = "";
var typeMessage = messages["type"];
if(typeMessage == "text"){
text = (messages["text"])["body"];
}
else if(typeMessage == "interactive"){
var interactiveObject = messages["interactive"];
var typeInteractive = interactiveObject["type"];
if(typeInteractive == "button_reply"){
text = (interactiveObject["button_reply"])["title"];
}
else if(typeInteractive == "list_reply"){
text = (interactiveObject["list_reply"])["title"];
}else{
myConsole.log("sin mensaje");
}
}else{
myConsole.log("sin mensaje");
}
return text;
}
module.exports = {
VerifyToken,
ReceivedMessage
}
第二个文件是whatsapService,我使用令牌与API建立连接,并且我还发送当我收到hello时要发送的消息的格式,例如...
const https = require("https");
function SendMessageWhatsApp(textResponse, number){
const data = JSON.stringify({
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": number,
"type": "text",
"text": {
"preview_url": false,
"body": textResponse
}
});
const options = {
host:"graph.facebook.com",
path:"/v15.0/1119744*************/messages",
method:"POST",
body:data,
headers: {
"Content-Type":"application/json",
Authorization:"Bearer EAAWNbICfuWEBAK5ObPbD******************************************************"
}
};
const req = https.request(options, res => {
res.on("data", d=> {
process.stdout.write(d);
});
});
req.on("error", error => {
console.error(error);
});
req.write(data);
req.end();
}
module.exports = {
SendMessageWhatsApp
};
然后,我声明get(检查令牌)和post(接收和回复消息)方法的路由:
const expres = require("express");
const router = expres.Router();
const whatsappController = require("../controllers/whatsappControllers");
router
.get("/", whatsappController.VerifyToken)
.post("/", whatsappController.ReceivedMessage)
module.exports = router;
最后但并非最不重要的索引文件,以便代码正确运行:
const express = require("express");
const apiRoute = require("./routes/routes");
const app = express();
const PORT = process.env.PORT || 3000
app.use(express.json());
app.use("/whatsapp", apiRoute);
app.listen(PORT, () => (console.log("El puerto es: " + PORT)));
我应该澄清一下,我用Postman做了测试,它们都很成功,它正确地响应和接收消息,最后我通过将机器人上传到Azure服务来做测试,它工作没有问题,直到它必须回答/复制用户的消息。
当用户与机器人对话时,机器人没有响应,但所有内容都正确地到达服务器,并以200响应进行处理。我附上接收没有问题的证据。
最后,我必须说,在 meta平台中,我已经按照相同平台的规定配置了所有内容,我已经配置了API通过webhook回答消息,所有内容都是正确的,我只是不能让bot正确回答。
机器人程序托管在Azure服务中。
1条答案
按热度按时间cu6pst1q1#
已解决:一些号码有一个问题,与API的WAB在我的国家(阿根廷)的电话号码开始在+54 9 11.问题是9在电话号码,这有一个冲突,在 meta服务器,解决方案退出号码9到这个国家的号码,消息将发送给用户.