【问题标题】:How to change inline bot handler in Telegram如何在 Telegram 中更改内联机器人处理程序
【发布时间】:2020-05-30 23:56:21
【问题描述】:

我关注this tutorial 并创建了一个电报机器人。我启用了内联模式并使用以下代码创建了内联查询处理程序:

from telegram import InlineQueryResultArticle, InputTextMessageContent
def inline_caps(update, context):
    query = update.inline_query.query
    if not query:
        return
    results = list()
    results.append(
        InlineQueryResultArticle(
            id=query.upper(),
            title='Caps',
            input_message_content=InputTextMessageContent(query.upper())
        )
    )
    context.bot.answer_inline_query(update.inline_query.id, results)

from telegram.ext import InlineQueryHandler
inline_caps_handler = InlineQueryHandler(inline_caps)
dispatcher.add_handler(inline_caps_handler)

我多次尝试更改此内联查询处理程序,但没有任何效果。这不起作用:

dispatcher.remove_handler(inline_caps_handler)

我还尝试禁用内联模式,然后使用 BotFather 再次启用它,但旧处理程序在重新激活内联模式时仍然保持不变。

创建一个新的处理程序并添加它也不起作用。

【问题讨论】:

    标签: python bots telegram telegram-bot python-telegram-bot


    【解决方案1】:

    你再试一次吗?

    我按照教程(直到“dispatcher.add_handler(inline_caps_handler)”),它没有给我任何问题。 玩完大写字母后,我做了 dispatcher.remove_handler(inline_caps_handler) 并且它停止将我写的单词转换为大写。然后我添加了另一个处理程序(一个小写转换器)并更新了机器人。

    这是我在 Python 交互式 shell 中运行的示例(与教程非常相似,但删除了非内联内容):

    from telegram.ext import Updater
    updater = Updater(token='TOKEN', use_context=True)
    
    dispatcher = updater.dispatcher
    
    import logging
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                         level=logging.INFO)
    
    updater.start_polling()
    
    from telegram import InlineQueryResultArticle, InputTextMessageContent
    def inline_caps(update, context):
        query = update.inline_query.query
        if not query:
            return
        results = list()
        results.append(
            InlineQueryResultArticle(
                id=query.upper(),
                title='Caps',
                input_message_content=InputTextMessageContent(query.upper())
            )
        )
        context.bot.answer_inline_query(update.inline_query.id, results)
    
    from telegram.ext import InlineQueryHandler
    inline_caps_handler = InlineQueryHandler(inline_caps)
    dispatcher.add_handler(inline_caps_handler)
    
    dispatcher.remove_handler(inline_caps_handler)
    def inline_lower(update, context):
        query = update.inline_query.query
        if not query:
            return
        results = list()
        results.append(
            InlineQueryResultArticle(
                id=query.lower(),
                title='lows',
                input_message_content=InputTextMessageContent(query.lower())
            )
        )
        context.bot.answer_inline_query(update.inline_query.id, results)
    
    from telegram.ext import InlineQueryHandler
    inline_lower_handler = InlineQueryHandler(inline_lower)
    dispatcher.add_handler(inline_lower_handler)
    

    在完成测试之后。

    updater.stop()
    

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 2021-12-06
      • 2021-10-22
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多