【问题标题】:How do you receive Whatsapp messages from Twilio using Node.JS?您如何使用 Node.JS 从 Twilio 接收 Whatsapp 消息?
【发布时间】:2020-05-11 10:22:33
【问题描述】:

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

这是 app.post 函数:

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 函数:

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 函数:

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;
    },


【问题讨论】:

    标签: javascript node.js twilio whatsapp


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

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

    首先,您应该确保将从 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。所以你不需要在这里创建 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 });
        }
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      相关资源
      最近更新 更多