【问题标题】:Ignore incoming messages from user in python-telegram-bot在 python-telegram-bot 中忽略来自用户的传入消息
【发布时间】: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


    【解决方案1】:

    如果此机器人在本地运行,您可以创建一个临时数组,在其中存储刚刚订购的用户的 ID。 同时,您启动一​​个将在 5 分钟后执行的 Job,并在此 Job 中从数组中弹出 id。

    # on top of the file
    silented_user = []
    
    # in the main function
    j = updater.job_queue 
    
    
    # before the order is sent
    if update.effective_user.id not in silented_user:
        ...
    
    
    # after the order is sent
    silented_user.append(update.effective_user.id)
    j.run_one(pop_user, 5*60, context={'cid_to_pop':update.effective_user.id})
    
    def pop_user(context):
        silented_user.pop(context.job.context['cid_to_pop'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多