【问题标题】:Wildcard for any string - Telegram-Bot任何字符串的通配符 - Telegram-Bot
【发布时间】:2019-02-17 22:44:17
【问题描述】:

嗯,我正在使用电报机器人更新程序来处理发送给它的消息。但是,我希望它响应我给出的任何字符串,前面带有“#”。

尝试了 AnyString 方法,但没有奏效。另外,尝试了“*”通配符。

from telegram.ext import Updater, CommandHandler

def hello(bot, update):
    update.message.reply_text(
        'Hey {}!'.format(update.message.from_user.first_name))


updater = Updater('770165564:AAEJm45dqDNkOnlso0YK6hQoCbXoCySiHcQ')

updater.dispatcher.add_handler(CommandHandler('*', hello))

updater.start_polling()
updater.idle()

当我发送诸如“/anything”、“/test”之类的消息时,机器人将使用我描述的函数进行响应,并说“嘿,{user-name}!”。

【问题讨论】:

    标签: python python-telegram-bot


    【解决方案1】:

    您可以使用 PrefixHandler (docs)

    示例:

    单个前缀和命令:

    PrefixHandler('!', 'test', callback) will respond to '!test'.
    

    多个前缀,单个命令:

    PrefixHandler(['!', '#'], 'test', callback) will respond to '!test' and
    '#test'.
    

    多个前缀和命令:

    PrefixHandler(['!', '#'], ['test', 'help`], callback) will respond to '!test',
    '#test', '!help' and '#help'.
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2018-08-09
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多