def test_inverted_filters(self, message): message.text = '/test' assert Filters.command(message) assert not (~Filters.command)(message) message.text = 'test' assert not Filters.command(message) assert (~Filters.command)(message)
def test_filters_command_anywhere(self, update): update.message.text = 'test /cmd' assert not (Filters.command(False))(update) update.message.entities = [ MessageEntity(MessageEntity.BOT_COMMAND, 5, 4) ] assert (Filters.command(False))(update)
def test_inverted_filters(self, update): update.message.text = '/test' assert Filters.command(update) assert not (~Filters.command)(update) update.message.text = 'test' assert not Filters.command(update) assert (~Filters.command)(update)
def test_inverted_filters(self, update): update.message.text = '/test' update.message.entities = [MessageEntity(MessageEntity.BOT_COMMAND, 0, 5)] assert Filters.command(update) assert not (~Filters.command)(update) update.message.text = 'test' update.message.entities = [] assert not Filters.command(update) assert (~Filters.command)(update)
def test_filters_command_default(self, update): update.message.text = 'test' assert not Filters.command(update) update.message.text = '/test' assert not Filters.command(update) # Only accept commands at the beginning update.message.entities = [MessageEntity(MessageEntity.BOT_COMMAND, 3, 5)] assert not Filters.command(update) update.message.entities = [MessageEntity(MessageEntity.BOT_COMMAND, 0, 5)] assert Filters.command(update)
def main(): updater = Updater(token=TOKEN, request_kwargs=REQUEST_KWARGS, use_context=True) dispatcher = updater.dispatcher conversation_handler = ConversationHandler( entry_points=[CommandHandler('start', start)], states={ TRACK: [MessageHandler(Filters.audio, download_track)], TITLE: [MessageHandler(Filters.text & (~Filters.command('cancel')), get_title)], PERFORMER: [MessageHandler(Filters.text & (~Filters.command('cancel')), get_performer)], }, fallbacks=[CommandHandler('cancel', cancel)] ) dispatcher.add_handler(conversation_handler) dispatcher.add_error_handler(error_handler) updater.start_polling() updater.idle()
def check_update(self, update: Update) -> bool: """ Checks if the update is to be handled. Args: update: The update Returns: True: If the update is to be handled False: Otherwise. """ if update.effective_message and Filters.command(update): return False user_id = update.effective_user.id with self._questioners_lock: if user_id in self._questioners and self._questioners[ user_id].check_update(update): return True return False
def test_filters_command(self): self.message.text = 'test' self.assertFalse(Filters.command(self.message)) self.message.text = '/test' self.assertTrue(Filters.command(self.message))
stickersbot.add_handler( ConversationHandler( name='create_or_add', persistent=True, entry_points=[ # CREATE CommandHandler(['create', 'new'], create.on_create_static_pack_command), # ADD CommandHandler(['add', 'a'], add.on_add_command) ], states={ # CREATE Status.CREATE_WAITING_TITLE: [ MessageHandler( Filters.text & ~Filters.command(STANDARD_CANCEL_COMMANDS), create.on_pack_title_receive), MessageHandler(~Filters.text, create.on_waiting_title_invalid_message) ], Status.CREATE_WAITING_NAME: [ MessageHandler( Filters.text & ~Filters.command(STANDARD_CANCEL_COMMANDS), create.on_pack_name_receive), MessageHandler(~Filters.text, create.on_waiting_name_invalid_message) ], Status.CREATE_WAITING_FIRST_STICKER: [ MessageHandler(Filters.text & ~Filters.command, create.on_first_sticker_text_receive ), # in case the user sends the emojis
def test_filters_command(self, update): update.message.text = 'test' assert not Filters.command(update) update.message.text = '/test' assert Filters.command(update)
def start_handler(update: Update, context: CallbackContext): db: PinginatorDb = context.bot_data['db'] message_str = 'Hi ' + update.effective_user.first_name + ',\n\n' \ 'I am ' + context.bot.first_name + \ ', a bot which would help you to notify all people in a group.\n\n' \ 'Use /ping or /all in a chat to take attention\n\n' \ 'Also, you could configure me:\n' \ 'Use /quiet_hours to set a time range in which I won\'t ping anyone\n' \ 'Write /admin_only to introduce a dictatorship of administrators into your chat.\n' \ 'You could schedule reapeted messages by /schedule and /unschedule commands\n' if update.effective_chat.type == 'group': group = db.get_group(group_id=update.effective_chat.id) message_str += '\nHere is the current configuration:\n' \ 'May any user pings all? --> ' + str(not group.is_admin_only) + '\n' \ 'Were quiet hours enabled? --> ' + \ str(False if group.quiet_hours is None else 'Yes, It is on from {} to {}'.format( group.quiet_hours[0], group.quiet_hours[1])) context.bot.send_message(update.effective_chat.id, message_str) DISPATCHER_HANDLERS = [ CommandHandler(['start', 'help'], start_handler), MessageHandler( Filters.command(['ping', 'all', 'quiet_hours', 'admin_only']) & Filters.private, private_message_text_handler), CommandHandler(['all', 'ping'], ping, filters=Filters.group), MessageHandler(Filters.text & (~Filters.command), text_handler) ]
def __init__(self, db): ''' Init self.updater,Jobqueue, Adds message handlers, nested conversation handlers, starts polling ''' self.db = db self.daylst = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', "Back" ] pp = PicklePersistence(filename='data/Tchcraltbot') self.updater = Updater(token=tchtkn, persistence=pp, use_context=True) dp = self.updater.dispatcher j = self.updater.job_queue j.run_daily(self.updaytt, datetime.time(11, 0, 0, 0), (0, 1, 2, 3, 4), context=telegram.ext.CallbackContext) j.run_daily(self.callback_daily, datetime.time(18, 47, 0, 0), (0, 1, 2, 3, 6), context=telegram.ext.CallbackContext) # daily timetable cov Daily_tt_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.text("Daily Timetable")), self.daykb) ], states={ self.Day_MH: [ MessageHandler((Filters.regex(".*[dD][aA][yY]$")), self.tchdtt), MessageHandler((Filters.text('Back')), self.bckmenu) ] }, allow_reentry=True, fallbacks=[ MessageHandler((~Filters.regex(".*[dD][aA][yY]$") & ~Filters.text('Back')), self.ivdlyday) ], name="dailyttcov", persistent=True) # Grade timetable cov Grade_tt_sub_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.regex("^[CEce][SsCc][Ee][0-9][0-9]$")), self.grdttdaykb) ], states={ self.Grade_sub_MH: [ MessageHandler((Filters.regex(".*[dD][aA][yY]$")), self.grddtt), MessageHandler((Filters.text('Back')), self.bckgrdgrdkb), CommandHandler('menu', self.menucall) ] }, allow_reentry=True, fallbacks=[ MessageHandler( (~Filters.regex(".*[dD][aA][yY]$") & ~Filters.text('Back') & ~Filters.command('menu')), self.ivgrdday) ], # map_to_parent={ self.STOPPING: END, self.Grade_btt_MH: self.Grade_btt_MH }, name="gradesubcov", persistent=True) Grade_tt_btt_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.text("Batch Timetable")), self.grdgrdkb) ], states={ self.Grade_btt_MH: [ Grade_tt_sub_cov, MessageHandler((Filters.text('Back')), self.bckmenu) ] }, allow_reentry=True, fallbacks=[ MessageHandler((~Filters.regex("^[CEce][SsCc][Ee][0-9][0-9]$") & ~Filters.text('Back')), self.ivgrdgrd) ], name="gradebttcov", persistent=True) # Announcement cov Announce_conf_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.regex('^MSG-.*')), self.anncon) ], states={ self.Announce_conf_MH: [ MessageHandler((Filters.text('Send')), self.annsnd), MessageHandler(Filters.text('Back'), self.menucall), CommandHandler('menu', self.menucall) ] }, allow_reentry=True, fallbacks=[ MessageHandler((~Filters.text('Send') & ~Filters.text('Back') & ~Filters.command('menu')), self.ivanncon) ], map_to_parent={ self.STOPPING: self.STOPPING, self.Announce_grd_MH: self.Announce_grd_MH }, name="annconfcov", persistent=True) Announce_msg_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.regex("^[CEce][SsCc][Ee][0-9][0-9]$")), self.annmsg) ], states={ self.Announce_msg_MH: [ Announce_conf_cov, MessageHandler((Filters.text('Back')), self.bckanngrdkb), CommandHandler('menu', self.menucall) ] }, allow_reentry=True, fallbacks=[ MessageHandler( (~Filters.regex('^MSG-.*') & ~Filters.text('Back') & ~Filters.command('menu')), self.ivannmsg) ], map_to_parent={ self.STOPPING: END, self.Announce_grd_MH: self.Announce_grd_MH }, name="annmsgcov", persistent=True) Announce_grd_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.text("Announcement")), self.anngrdkb) ], states={ self.Announce_grd_MH: [ Announce_msg_cov, MessageHandler((Filters.text('Back')), self.bckmenu) ] }, allow_reentry=True, fallbacks=[ MessageHandler((~Filters.regex("^[CEce][SsCc][Ee][0-9][0-9]$") & ~Filters.text('Back')), self.ivanngrd) ], name="anngrdcov", persistent=True) # Take class cov Take_day_cov = ConversationHandler( entry_points=[ MessageHandler(Filters.regex(".*[dD][aA][yY]$"), self.tkeperkb) ], states={ self.Take_day_MH: [ MessageHandler( Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]$"), self.tkecls), MessageHandler((Filters.text('Back')), self.bcktkedaykb), CommandHandler('menu', self.menucall) ] }, allow_reentry=True, fallbacks=[ MessageHandler( (~Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]$") & ~Filters.text('Back') & ~Filters.command('menu')), self.ivtkper) ], map_to_parent={self.STOPPING: self.STOPPING}, name="tkedaycov", persistent=True) Take_grd_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.regex( r"^[CEce][SsCc][Ee][0-9][0-9]:[A-Za-z][A-Za-z][A-Za-z][A-Za-z][0-9][0-9]$" ) | Filters.regex(r"^[CEce][SsCc][Ee][0-9][0-9]:[Ee][0-9]$") | Filters.regex( r"^[CEce][SsCc][Ee][0-9][0-9]:T&P$")), self.tkedaykb) ], states={ self.Take_grd_MH: [ Take_day_cov, MessageHandler((Filters.text('Back')), self.bcktkegrdkb), CommandHandler('menu', self.menucall) ] }, allow_reentry=True, fallbacks=[ MessageHandler( (~Filters.regex(".*[dD][aA][yY]$") & ~Filters.text('Back') & ~Filters.command('menu')), self.ivtkday) ], # map_to_parent={self.STOPPING: END}, name="tkegrdcov", persistent=True) Take_cls_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.text("Take Class")), self.tkegrdkb) ], states={ self.Take_cls_MH: [ Take_grd_cov, MessageHandler((Filters.text('Back')), self.bckmenu) ] }, allow_reentry=True, fallbacks=[ MessageHandler((~Filters.regex( r"^[CEce][SsCc][Ee][0-9][0-9]:[A-Za-z][A-Za-z][A-Za-z][A-Za-z][0-9][0-9]$" ) & ~Filters.regex(r"^[CEce][SsCc][Ee][0-9][0-9]:[Ee][0-9]$") & ~Filters.regex( r"^[CEce][SsCc][Ee][0-9][0-9]:T&P$")), self.ivtkgs), MessageHandler((~Filters.text('Back')), self.ivtkgs) ], name="tkeclscov", persistent=True) # cancel class cov Ccl_day_cov = ConversationHandler( entry_points=[ MessageHandler(Filters.regex(".*[dD][aA][yY]$"), self.ccgrdkb) ], states={ self.Ccl_day_MH: [(MessageHandler((Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[CEce][SsCc][Ee][0-9][0-9]:[A-Za-z][A-Za-z][A-Za-z][A-Za-z][0-9][0-9]$" )) | (Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[CEce][SsCc][Ee][0-9][0-9]:[Ee][0-9]$" )) | (Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[CEce][SsCc][Ee][0-9][0-9]:T&P$" )), self.ccls)), MessageHandler((Filters.text('Back')), self.bckccdaykb), CommandHandler('menu', self.menucall)] }, allow_reentry=True, fallbacks=[(MessageHandler((~Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[CEce][SsCc][Ee][0-9][0-9]:[A-Za-z][A-Za-z][A-Za-z][A-Za-z][0-9][0-9]$" )) & (~Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[CEce][SsCc][Ee][0-9][0-9]:[Ee][0-9]$" )) & (~Filters.regex( r"^[0-9][0-9].[0-9][0-9]-[0-9][0-9].[0-9][0-9]:[CEce][SsCc][Ee][0-9][0-9]:T&P$" )), self.ivccpgs)), MessageHandler( (~Filters.text('Back') & ~Filters.command('menu')), self.ivccpgs)], map_to_parent={self.STOPPING: END}, name="ccldaycov", persistent=True) Ccl_cls_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.text("Cancel Class")), self.ccdaykb) ], states={ self.Ccl_cls_MH: [ Ccl_day_cov, MessageHandler((Filters.text('Back')), self.bckmenu) ] }, allow_reentry=True, fallbacks=[ MessageHandler( ~Filters.text('Back') & ~Filters.regex(".*[dD][aA][yY]$"), self.ivccday) ], name="cclclscov", persistent=True) # Menu cov Menu_cov = ConversationHandler( entry_points=[ MessageHandler((Filters.text('Menu') | Filters.text('Cancel')), self.menu) ], states={ self.Menu_opt_MH: [ MessageHandler(Filters.text("Today's Timetable"), self.tchtdt), MessageHandler(Filters.text('Change Your EMPLOYEE ID'), self.empupd), Daily_tt_cov, Take_cls_cov, Ccl_cls_cov, Grade_tt_btt_cov, Announce_grd_cov ] }, allow_reentry=True, fallbacks=[ MessageHandler((~Filters.command('menu')) & ~Filters.text("Today's Timetable") & ~Filters.text('Change Your EMPLOYEE ID') & ~Filters.text("Cancel Class") & ~Filters.text("Take Class") & ~Filters.text("Announcement") & ~Filters.text("Batch Timetable") & ~Filters.text("Daily Timetable"), self.ivmnuopt) ], name="menucov", persistent=True) # Start cov Setup_cov = ConversationHandler( entry_points=[CommandHandler('start', self.start)], states={ self.Setup_MH: [(MessageHandler( (Filters.regex('^[iI][Ii][Ii][Tt][tT]0[0-9][0-9]$')), self.empid))], self.Empupd_MH: [(MessageHandler( (Filters.regex('^[iI][Ii][Ii][Tt][tT]0[0-9][0-9]$')), self.empid)), Menu_cov] }, allow_reentry=True, fallbacks=[ MessageHandler( (~Filters.regex('^[iI][Ii][Ii][Tt][tT]0[0-9][0-9]$')) & (~Filters.command('menu')) & ~Filters.text('Menu') & ~Filters.text('Cancel'), self.ivid) ], name="setupcov", persistent=True) dp.add_handler(Setup_cov) dp.add_error_handler(self.error)
@decorators.failwithmessage @decorators.logconversation def on_bad_animated_sticker_receive(update: Update, _): logger.info('user sent a static sticker instead of an animated one') update.message.reply_text(Strings.ADD_STICKER_EXPECTING_ANIMATED) return Status.WAITING_ANIMATED_STICKERS stickersbot.add_handler(ConversationHandler( name='adding_stickers', persistent=True, entry_points=[CommandHandler(['add', 'a'], on_add_command)], states={ Status.WAITING_TITLE: [MessageHandler(Filters.text & ~Filters.command(STANDARD_CANCEL_COMMANDS), on_pack_title)], Status.WAITING_NAME: [MessageHandler(Filters.text & ~Filters.command(STANDARD_CANCEL_COMMANDS), on_pack_name)], Status.WAITING_STATIC_STICKERS: [ MessageHandler( CustomFilters.static_sticker | Filters.document.category('image/png'), on_static_sticker_receive ), MessageHandler(CustomFilters.animated_sticker, on_bad_static_sticker_receive), ], Status.WAITING_ANIMATED_STICKERS: [ MessageHandler(CustomFilters.animated_sticker, on_animated_sticker_receive), MessageHandler( CustomFilters.static_sticker | Filters.document.category('image/png'), on_bad_animated_sticker_receive ),
notion_balance = "https://www.notion.so/chenchiks/2062899533a048579f572a7e3d40182f?v=1fb6c93b1a5045af9ea3a83b4aa90dd0" notion_transactions = "https://www.notion.so/chenchiks/1604cc3bb0614273a690710f17b138ca?v=8f278effcac4457d803aeb5cc0a1c93e" credit_limit = int(LIMIT) recalculate = "Recalculate" newlink = "Update numbers" recalculate_keyboard = KeyboardButton(text=recalculate) link_keyboard = KeyboardButton(text=newlink) custom_keyboard = [[recalculate_keyboard, link_keyboard]] reply_markup = ReplyKeyboardMarkup(custom_keyboard, resize_keyboard=True) newlink_filter = Filters.text([newlink]) & (Filters.user( user_id=POWER_USER_ID) | Filters.user(username=POWER_USER_NAME)) recalculate_filter = Filters.text([recalculate]) & (Filters.user( user_id=POWER_USER_ID) | Filters.user(username=POWER_USER_NAME)) in_known_filters = newlink_filter | recalculate_filter | Filters.command( 'start') def start(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!", reply_markup=reply_markup) def unknown(update, context): context.bot.send_message(chat_id=update.effective_chat.id, text="I'm sorry Dave I'm afraid I can't do that.", reply_markup=reply_markup) def daily_status(day, date, planned_month, daily):
def filter_wanted_words(msg): return wanted_words and Filters.text(msg) and not Filters.forwarded(msg) and not Filters.command(msg) and \ bool(re.search('\\b' + '\\b|\\b'.join(wanted_words) + '\\b', msg.text, re.IGNORECASE))
def filter_group_name_reward(msg): return Filters.private(msg) and Filters.text(msg) and not Filters.command(msg) \ and variables.poles \ and (int(msg.from_user.id) == variables.poles[1] or int(msg.from_user.id) == variables.poles[2]) \ and datetime.datetime.today().weekday() == 5
def test_filters_command(self, message): message.text = 'test' assert not Filters.command(message) message.text = '/test' assert Filters.command(message)