繁体   English   中英

您如何使用 Node.JS 从 Twilio 接收 Whatsapp 消息?

[英]How do you receive Whatsapp messages from Twilio using Node.JS?

我正在尝试使用 Node.JS 构建一个 Whatsapp 聊天机器人,并且在接收来自 Twilio 的 Whatsapp 消息时遇到了一些麻烦。 在检查调试器时,我得到一个 Bad Gateway 错误,即。 错误 11200:HTTP 检索失败。 消息正在发送,ngrok 显示发布请求,但是,dialogflow 没有收到请求。 在终端上,错误显示 UnhandledPromiseRejectionWarning: Error: 3 INVALID ARGUMENT: Input text not set。 我不确定是不是因为消息不是 JSON 格式。 请帮忙!

这是 app.post function:

app.post('/api/whatsapp_query', async (req, res) =>{
        message = req.body;
        chatbot.textQuery(message.body, message.parameters).then(result => {
            twilio.sendMessage(message.from, message.to, result.fulfillmentText).then(result => {
                console.log(result);
            }).catch(error => {
                console.error("Error is: ", error);
            });
            return response.status(200).send("Success");
        })
    });

这是我导入的 sendMessage function:

const config = require('./config/keys');

const twilioAccountID = config.twilioAccountID;
const twilioAuthToken = config.twilioAuthToken;
const myPhoneNumber = config.myPhoneNumber;

const client = require('twilio')(twilioAccountID,twilioAuthToken);

module.exports = {
    sendMessage: async function(to, from, body) {
        return new Promise((resolve, reject) => {
            client.messages.create({
                to,
                from,
                body
            }).then(message => {
                resolve(message.sid);
            }).catch(error => {
                reject(error);
            });
        });
    }
}

这是我导入的 textQuery function:

textQuery: async function(text, parameters = {}) {
        let self = module.exports;
        const request = {
            session: sessionPath,
            queryInput: {
                text: {
                    text: text,
                    languageCode: config.dialogFlowSessionLanguageCode
                },
            },
            queryParams: {
                payload: {
                    date: parameters
                }
            }
        };
        let responses = await sessionClient.detectIntent(request);
        responses = await self.handleAction(responses)
        return responses[0].queryResult;
    },


Twilio 开发人员布道师在这里。

问题是您没有将传入 WhatsApp 消息中的正确消息正文传递给您的textQuery function。

首先,您应该确保将从 Twilio 传入的 webhook 视为application/x-www-form-urlencoded 如果您使用的是 body-parser,请确保您已打开 urlencoded 解析。

app.use(bodyParser.urlencoded());

其次,Twilio发送的参数以大写字母开头。 因此,您的代码当前获取message = req.body ,然后使用message.body 但它应该是message.Body

这两点应该可以解决您的问题。

最后一件事。 Twilio Node.js 库将返回 Promise 如果您不传递回调 ZC1C425268E68385D1AB5074.C 所以你不需要在这里创建 Promise:

module.exports = {
    sendMessage: async function(to, from, body) {
        return new Promise((resolve, reject) => {
            client.messages.create({
                to,
                from,
                body
            }).then(message => {
                resolve(message.sid);
            }).catch(error => {
                reject(error);
            });
        });
    }
}

您可以将调用结果返回给client.messages.create

module.exports = {
    sendMessage: async function(to, from, body) {
        return client.messages.create({ to, from, body });
    }
}

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM