如何解决如果我使用node-telegram-bot-api创建电报bot对象,为什么dialogflow bot停止电报响应?
我建立了一个具有电报集成功能的dialogflow聊天机器人,我需要获取用户在电报聊天中发送的图像的路径。据我所知,dialogflow bot不会监听图像,因此我使用电报bot对象轮询消息以获取图像,但是通过这种方式,即使在电报bot的轮询停止之后,dialogflow bot仍会停止响应。两种漫游器之间存在一些冲突。 “复苏”对话框流程机器人的唯一方法是在对话框流程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);
});
解决方法
已解决。问题在于,webhook和轮询是获取消息的两种互斥方法。因此,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网址:
https://api.telegram.org/botYourTelegramBotToken/getWebhookInfo
希望它对某人有帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。