【问题标题】:Why dialogflow bot stop responding in telegram if i create a telegram bot object with node-telegram-bot-api?如果我使用 node-telegram-bot-api 创建一个电报机器人对象,为什么对话流机器人停止在电报中响应?
【发布时间】:2020-08-25 11:48:59
【问题描述】:

我已经构建了一个带有电报集成的对话流聊天机器人,我需要获取用户在电报聊天中发送的图像的路径。据我所知,对话流机器人不听图像,所以我使用电报机器人对象来轮询消息以获取图像,但这样对话流机器人停止响应,即使在电报机器人的轮询停止后也是如此。两个机器人之间存在一些冲突。 “恢复”对话流机器人的唯一方法是在对话流 UI 中手动重新启动电报集成。 有一种方法可以解决两个机器人之间的冲突,以便在电报机器人获得图像后对话流机器人继续响应? 这是我写的代码:

const TG = require('node-telegram-bot-api');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });

function takeImagePath() {
    agent.add(`send an image/photo`);   // work
    const token = 'my telegram bot token';
    let telegramBot = new TG(token, {polling: true});
    agent.add(`tg bot created`);  // work

    telegramBot.on('message',  async function (msg) {
        const chatId = msg.chat.id;
        agent.add('bot dialogflow in the listener');   // don't work

        if (msg.photo) {
            let ImgID = msg.photo[msg.photo.length - 1].file_id;
            let imgPath = "";

            if (ImgID) {
                telegramBot.getFile(ImgID).then((fileObject) => {
                    imgPath = fileObject.file_path;
                }).then(() => telegramBot.sendMessage(chatId, 'image taken'))   //work
                  .catch(error => console.log("error: " + error.message));
            }
        }
        else { await telegramBot.sendMessage(chatId, "it's not an image, telegram bot shutting down");   //work
               await telegramBot.stopPolling();
               agent.add("bot dialogflow active");   // don't work
        }
    });

}

let intentMap = new Map();
intentMap.set('Image intent', takeImagePath);
agent.handleRequest(intentMap);
});

【问题讨论】:

    标签: node.js telegram-bot dialogflow-es-fulfillment


    【解决方案1】:

    解决了。问题是 webhook 和 polling 是两种相互排斥的获取消息的方法。因此,虽然 dialogflow-telegram bot 使用 webhook,但我创建的电报 bot 对象使用轮询 let telegramBot = new TG(token, {polling: true}); 它会自动删除 webhook。 要解决这个问题,需要在停止轮询后重新设置 webhook:await bot.stopPolling(); bot.setWebHook("your webhook url").then(r => console.log("webhook response: "+r)).catch(err => console.log("webhook error: "+err.message));

    您可以在此处找到您的 dialogflow-telegram 机器人正在使用的 webhook url:

    https://api.telegram.org/botYourTelegramBotToken/getWebhookInfo

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2017-08-18
      • 1970-01-01
      • 2021-02-02
      • 2021-05-13
      • 2021-12-06
      • 2017-05-21
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多