任何字符串电报的通配符b

2024-10-02 20:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我用telegram bot更新程序来处理从发送到它的消息。但是,我想让它对我给出的任何字符串做出响应,前面加一个“#”。在

尝试了AnyString方法,但不起作用。还有,试过“*”wilcard的东西。在

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”之类的消息时,bot将用我描述的函数来响应,说“嘿{user name}!”。在


Tags: 字符串namefrom程序消息messagehellobot
1条回答
网友
1楼 · 发布于 2024-10-02 20:31:33

您可以使用前缀处理器(docs

示例:

命令和单个前缀:

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

多个前缀,单个命令:

^{pr2}$

多前缀和命令:

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

相关问题 更多 >