Telegram 机器人 - 删除键盘 - Nodejs
Telegram bot - Remove keyboard - Nodejs
我遇到了一个很奇怪的问题,可能与缓存有关。
原来如此。
我在 nodejs 中开发了一个用于电报的机器人。
这个机器人过去有一个不是 "inline_keyboard" 的自定义键盘
我决定改变这种行为并实施 inline_keyboard.
当前代码是这样的:
var options = {
parse_mode: "Markdown",
disable_web_page_preview: true,
reply_markup: JSON.stringify({
inline_keyboard: [
[{
text: ' English',
callback_data: "SET ENGLISH"
},{
text: ' Français',
callback_data: "SET FRENCH"
}]
]
})
};
bot.sendMessage(msg.chat.id, "Please choose your language",options);
Inline_keyboard 工作正常,但我的旧代码(已被删除)仍然出现在我的用户面前,非常烦人。
在这里;当我的用户登录我的聊天时,它会不断出现。
我有以下资源:
- https://core.telegram.org/bots/api#replykeyboardremove
- https://core.telegram.org/bots/api#editmessagereplymarkup
但我不知道如何实现它,所以我可以为我的用户删除这个烦人的聊天。
有什么建议吗?
感谢您的支持
可能会有不同的解决方案,我的建议是:
您可以使用每个用户的第一个答案来移除键盘,首先使用 editMessageText 移除键盘,然后向他发送适当的答案。(请注意,保留已移除键盘的聊天 ID,因此您将执行此操作每个用户一次)
bot.on('callback_query', function onCallbackQuery(callbackQuery) {
if(!didWeRemoveHisKeyboard(callbackQuery.from.id))
removeHisKeyboard(callbackQuery)
//then handle the user response
})
removeHisKeyboard = function(callbackQuery){
bot.editMessageText(callbackQuery.message.text,
{message_id:callbackQuery.message.message_id , chat_id:callbackQuery.from.id,
reply_markup: {
remove_keyboard: true
}}).catch((err) => {
//some error handling
}).then(function(res){
if(res)
addThisChatToHandledList(callbackQuery.from.id)
})
}
请注意,根据您使用的 node_module,您可能需要对此代码进行一些修改。
我遇到了一个很奇怪的问题,可能与缓存有关。
原来如此。 我在 nodejs 中开发了一个用于电报的机器人。
这个机器人过去有一个不是 "inline_keyboard" 的自定义键盘 我决定改变这种行为并实施 inline_keyboard.
当前代码是这样的:
var options = {
parse_mode: "Markdown",
disable_web_page_preview: true,
reply_markup: JSON.stringify({
inline_keyboard: [
[{
text: ' English',
callback_data: "SET ENGLISH"
},{
text: ' Français',
callback_data: "SET FRENCH"
}]
]
})
};
bot.sendMessage(msg.chat.id, "Please choose your language",options);
Inline_keyboard 工作正常,但我的旧代码(已被删除)仍然出现在我的用户面前,非常烦人。
在这里;当我的用户登录我的聊天时,它会不断出现。
我有以下资源:
- https://core.telegram.org/bots/api#replykeyboardremove
- https://core.telegram.org/bots/api#editmessagereplymarkup
但我不知道如何实现它,所以我可以为我的用户删除这个烦人的聊天。
有什么建议吗? 感谢您的支持
可能会有不同的解决方案,我的建议是:
您可以使用每个用户的第一个答案来移除键盘,首先使用 editMessageText 移除键盘,然后向他发送适当的答案。(请注意,保留已移除键盘的聊天 ID,因此您将执行此操作每个用户一次)
bot.on('callback_query', function onCallbackQuery(callbackQuery) {
if(!didWeRemoveHisKeyboard(callbackQuery.from.id))
removeHisKeyboard(callbackQuery)
//then handle the user response
})
removeHisKeyboard = function(callbackQuery){
bot.editMessageText(callbackQuery.message.text,
{message_id:callbackQuery.message.message_id , chat_id:callbackQuery.from.id,
reply_markup: {
remove_keyboard: true
}}).catch((err) => {
//some error handling
}).then(function(res){
if(res)
addThisChatToHandledList(callbackQuery.from.id)
})
}
请注意,根据您使用的 node_module,您可能需要对此代码进行一些修改。