我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用telegram.ext.RegexHandler()。
def start_bot(): my_bot = Updater(settings.TELEGRAM_API_KEY) dp = my_bot.dispatcher dp.add_handler(CommandHandler("start", reply_to_start_command)) conv_handler = ConversationHandler( entry_points=[RegexHandler('^(????????? ??????)$', start_anketa, pass_user_data=True)], states={ "name": [MessageHandler(Filters.text, get_name, pass_user_data=True)], "attitude": [RegexHandler('^(1|2|3|4|5)$', attitude, pass_user_data=True)], "understanding": [RegexHandler('^(1|2|3|4|5)$', understanding, pass_user_data=True)], "comment": [MessageHandler(Filters.text, comment, pass_user_data=True), CommandHandler('skip', skip_comment, pass_user_data=True)], }, fallbacks=[MessageHandler(Filters.text, dontknow, pass_user_data=True)] ) dp.add_handler(conv_handler) my_bot.start_polling() my_bot.idle()
def start_bot(): my_bot = Updater(TELEGRAM_API_KEY) dp = my_bot.dispatcher dp.add_handler(CommandHandler("start", reply_to_start_command, pass_user_data=True)) dp.add_handler(CommandHandler("cat", send_cat, pass_user_data=True)) dp.add_handler(RegexHandler("^(???????? ??????)$", send_cat, pass_user_data=True)) dp.add_handler(RegexHandler("^(??????? ????????)$", change_avatar_step1, pass_user_data=True)) dp.add_handler(CommandHandler("avatar", change_avatar_step2, pass_args=True, pass_user_data=True)) dp.add_handler(MessageHandler(Filters.contact, get_contact, pass_user_data=True)) dp.add_handler(MessageHandler(Filters.location, get_location, pass_user_data=True)) my_bot.start_polling() my_bot.idle()
def register(): dispatcher.add_handler(CommandHandler('settings', show_settings)) dispatcher.add_handler(RegexHandler('^([' + '??' + '??' + '?' + ']) .+$', kb_select, pass_groups=True)) dispatcher.add_handler(RegexHandler(r'^(\w\w_\w\w) - .*', locale_select, pass_groups=True))
def get_settings_change_state(): return [WorkflowEnum.SETTINGS_CHANGE, [RegexHandler("^(" + regex_settings_or() + ")$", settings_change, pass_chat_data=True), RegexHandler("^(CANCEL)$", cancel)]] # Will return the SETTINGS_SAVE state for a conversation handler # This way the state is reusable
def get_settings_confirm_state(): return [WorkflowEnum.SETTINGS_CONFIRM, [RegexHandler("^(YES|NO)$", settings_confirm, pass_chat_data=True)]] # BOT conversation handler
def main(): utils.create_database() updater = Updater(token=TOKEN_ID) # Get the dispatcher to register handlers dp = updater.dispatcher # on different commands - answer in Telegram dp.add_handler(CommandHandler('start', start, pass_args=True)) dp.add_handler(CommandHandler('help', start)) dp.add_handler(CommandHandler('who', ts_stats)) dp.add_handler(CommandHandler('ts', utils.ts_view)) dp.add_handler(CommandHandler('mention', mention_toggle)) dp.add_handler(CommandHandler('generate', generate_invitation, Filters.user(user_id=ADMIN_ID))) dp.add_handler(CommandHandler('notify', notify, Filters.user(user_id=ADMIN_ID), pass_args=True)) dp.add_handler(CommandHandler('id', get_id)) dp.add_handler(RegexHandler(r'(?i).*\@flandas\b', utils.mention_forwarder)) dp.add_handler(CallbackQueryHandler(utils.callback_query_handler, pass_chat_data=True)) dp.add_handler(CommandHandler('users', utils.send_users_tsdb, Filters.user(user_id=ADMIN_ID), pass_chat_data=True)) dp.add_handler(CommandHandler('groups', utils.send_ts_groups, Filters.user(user_id=ADMIN_ID), pass_chat_data=True)) dp.add_handler(MessageHandler(filter_assign_alias, utils.assign_user_alias_step2, pass_chat_data=True)) gm_handler = ConversationHandler( allow_reentry=True, entry_points=[CommandHandler('gm', utils.pre_send_gm)], states={ 0: [MessageHandler(Filters.text, utils.send_gm)], }, fallbacks=[CommandHandler('cancel', cancel), CommandHandler('headshot', cancel)] ) dp.add_handler(gm_handler) # Add response for unknown private messages dp.add_handler(MessageHandler(Filters.private, unknown)) # log all errors dp.add_error_handler(log_error) # Start the Bot updater.start_polling() # Run the bot until the you presses 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()
def main(): TOKEN = "382499494:AAEJrdhHmXy46VV-RrBv0xmkIJps09eJyD4" updater = Updater(token=TOKEN) dispatcher = updater.dispatcher options_handler = ConversationHandler( entry_points=[CommandHandler('opciones', options), CommandHandler('start', options), MessageHandler(bancoFilter.filter_return, options)], states={ OPTIONS: [MessageHandler(bancoFilter.filter_service, bancoServices.services), MessageHandler(bancoFilter.filter_new_account, create_account), MessageHandler(bancoFilter.filter_desactivate_account, desactivate_account), MessageHandler(bancoFilter.filter_activate_account, active_account)] }, fallbacks=[CommandHandler('cancel', cancel)], allow_reentry=True ) dispatcher.add_handler(options_handler) delete_handler = ConversationHandler( entry_points=[CommandHandler('desactivar', sure_desactivate_account)], states={ DELETE: [RegexHandler('^(Si|No)$', desactivate_account)] }, fallbacks=[CommandHandler('cancel', cancel)], allow_reentry=True ) dispatcher.add_handler(delete_handler) dispatcher.add_handler(bancoServices.add_balance_handler) active_handler = CommandHandler('activar', active_account) dispatcher.add_handler(active_handler) show_handler = CommandHandler('vercuenta', show_account) dispatcher.add_handler(show_handler) PORT = int(os.environ.get('PORT', '5000')) updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) updater.bot.set_webhook("https://kodefest6.herokuapp.com/" + TOKEN) updater.idle()
def main(): global mClient, mDatabase mClient = MongoClient(mongoURI) mDatabase = mClient[mDatabase] try: serverInfo = mClient.server_info() logger.info("Mongo Connection Achieved") logger.debug("Connected to Mongo Server: %s" % serverInfo) except: logger.error("Could not connect to Mongo Server at %s" % mongoURI) raise updater = Updater(authToken) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) charCreationHandler = ConversationHandler( entry_points=[CommandHandler('meAbout',meAbout, pass_user_data=True)], states={ TEXTRESP:[MessageHandler(Filters.text, meTextResp, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)], PHOTORESP:[MessageHandler(Filters.photo or Filters.sticker, mePhotoResp, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)], CHOOSE:[RegexHandler("^(Name, Discription, Photo, Bio, Cancel)$", meChoose, pass_user_data=True)] }, fallbacks=[CommandHandler('cancel',meAboutCancel, pass_user_data=True)] ) structCreationHandler = ConversationHandler( entry_points=[CommandHandler('newStruct', editStructStart, pass_user_data=True)], states={STRUCTNAME:[MessageHandler(Filters.text, editStructSetName, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)] STRUCTDESC:[MessageHandler(Filters.text, editStructSetDesc, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)], STRUCTDOOR:[MessageHandler(Filters.text, editStructSetDoor, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)]}, fallbacks=[CommandHandler("cancel", cancel)]) roomCreationHandler = ConversationHandler( entry_points=[ConversationHandler("editRoom", editRoomStart)], states={}, fallbacks=[CommandHandler("cancel", cancel)]) dp.add_handler(CommandHandler("roomAddObjDisc", roomAddObjDisc, allow_edited=True)) dp.add_handler(CommandHandler("roomRemObjDisc", roomRemObjDisc)) dp.add_handler(CommandHandler("roomAddTempDisc", roomAddTempDisc, allow_edited=True)) dp.add_handler(CommandHandler("roomRemTempDisc", roomRemTempDisc)) dp.add_handler(CommandHandler("doorLock",doorLock)) dp.add_handler(CommandHandler("doorUnlock", doorUnlock)) dp.add_handler(CommandHandler("doorKnock", doorKnock)) updater.start_polling() updater.idle() mClient.close()