我已经建立了一个对话流聊天机器人与电报集成,我需要采取的路径,由用户发送的图像在电报聊天。据我所知,dialogflow bot不会侦听图像,因此我使用telegram bot对象来轮询消息以获取图像,但这样dialogflow bot就会停止响应,即使在telegram bot的轮询停止之后也是如此。这两个机器人之间有一些冲突。“恢复”dialogflow机器人的唯一方法是在dialogflow UI中手动重新启动电报集成。有一种方法可以解决两个机器人之间的冲突,以便dialogflow机器人在电报机器人获得图像后继续响应?下面是我写的代码:
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);
});发布于 2020-08-27 22:22:05
解决了。问题是webhook和轮询是两种相互排斥的获取消息的方法。因此,dialogflow-telegram bot使用webhook,而我创建的电报bot对象使用polling let telegramBot = new TG(token, {polling: true});,它会自动删除webhook。要解决此问题,必须在停止轮询后重新设置set挂钩: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
希望这能帮助到别人。
https://stackoverflow.com/questions/63578214
复制相似问题