【发布时间】:2022-01-24 17:00:14
【问题描述】:
我在这里问你一种方法来忽略来自 python-telegram-bot 中用户的传入消息。 我只是指定我的情况,让您了解我正在寻找的代码的用途。
这是机器人的代码,用户发送命令 /order,机器人会询问用户想要订购哪种饮料。 此时,为了避免无用的溢出,我想阻止刚刚订购的特定用户(大约 5 分钟),以便让机器人“免费” " 为其他用户。
几年前,我查看了电报为群组提供的特定选项,例如计时器聊天,不是吗? 但我认为在与机器人的私人聊天中根本不可能。
这是我正在编写的代码的简化版本。
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
)
WORK = range(1)
def cancel(update: Update, context: CallbackContext) -> int:
"""Cancels and ends the conversation."""
user = update.message.from_user
update.message.reply_text(
'Bye! I hope we can talk again some day.', reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def order(update: Update, context: CallbackContext) -> int:
update.message.reply_text("please choose a drink")
update.message.reply_text("Sangria | Martini | Analcolico")
return WORK
def work(update: Update, context: CallbackContext)-> int:
ord_drink= update.message.text
update.message.reply_text("Okay i've just received your drink, you can order your drink in 5 MINUTES")
return ConversationHandler.END
def main() -> None:
"""Run the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater("TOKEN")
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
#blocks the user after the first order for n minutes
conv_handler2 = ConversationHandler(
entry_points=[CommandHandler('order', order)],
states={
WORK : [MessageHandler(Filters.text & ~Filters.command, work)],
},
fallbacks=[CommandHandler('cancel', cancel)],
)
dispatcher.add_handler(conv_handler2)
# Start the Bot
updater.start_polling()
#updater.stop()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()
看看: 显然,用户的拦截功能必须在他的订单之后调用,但机器人应该对其他用户免费
【问题讨论】:
标签: python telegram python-telegram-bot