def confirm(self, text): while True: send_message( text, reply_markup=ReplyKeyboardMarkup( [[KeyboardButton("Yes"), KeyboardButton("No")]]), ) reply = get_next_message() if Filters.text("Yes").filter(reply): return True if Filters.text("No").filter(reply): return False
def listen(self): while True: commands = { "List all tasks": lambda: self.list_task(self.edit_task), "Add a new task": self.add_task, } keyboard = [[KeyboardButton(c) for c in commands]] send_message("I'm listening.", reply_markup=ReplyKeyboardMarkup(keyboard)) message = get_next_message() _tw_lock.acquire() _tw.sync() if (message.entities and message.entities[0].type == MessageEntity.BOT_COMMAND and message.entities[0].offset == 0): args = message.text.split()[1:] command = message.text[1:message.entities[0].length].split( '@')[0] self.handle_command(command, args) else: for c in commands: if Filters.text(c).filter(message): commands[c]() break _tw.sync() _tw_lock.release()
def add_task(self): send_message( "Send description for new task.", reply_markup=ReplyKeyboardMarkup( [[KeyboardButton("# CANCEL OPERATION")]]), ) reply = get_next_message() if Filters.text("# CANCEL OPERATION").filter(reply): send_message("Operation cancelled.") else: new_task = Task(_tw, description=reply.text) send_message(task2message(new_task), parse_mode=ParseMode.HTML) if self.confirm("Do you want to create above task?"): # Dirty hack for saving keys = [] for attr in new_task.__dict__['_data']: if attr != 'description': keys.append(attr) for key in keys: new_task.__dict__['_data'].pop(key) new_task.save() send_message("Task saved.") else: send_message("Operation cancelled.")
def send_user(bot, update): session = DBSession() users = session.query(User).all() if not (Filters.text(update.message) | Filters.sticker(update.message) | Filters.photo(update.message) | Filters.video(update.message) | Filters.document(update.message)): bot.send_message(chat_id=update.message.id, text=NO_SUPPORT_FORMAT) return send_admin_msg(users=users, bot=bot, update=update) bot.send_message(chat_id=update.message.chat_id, text=USER_FORWARD_STOP) session.close() return ConversationHandler.END
def main(): dotenv.load_dotenv() TOKEN = os.environ["BOT_TOKEN"] updater = Updater(TOKEN) conv_handler = ConversationHandler( entry_points=[ MessageHandler( Filters.text(Config.TELEGRAM_PRODUCT_BUTTONS + Config.INSTAGRAM_PRODUCT_BUTTONS), product_choose_handler) ], states={ Steps.PRODUCT_COUNT_STEP: [MessageHandler(Filters.text, order_confirm_handler)], # Steps.ORDER_CONFIRM_STEP: [MessageHandler( # Filters.text, # )] }, fallbacks=[CommandHandler('start', start_handler)]) updater.dispatcher.add_handler(conv_handler) updater.dispatcher.add_handler(CommandHandler('start', start_handler)) updater.dispatcher.add_handler(CommandHandler('cancel', start_handler)) updater.dispatcher.add_handler(CommandHandler('help', help_command)) updater.dispatcher.add_handler( MessageHandler(Filters.text(Config.MAIN_MENU_BUTTONS), button)) updater.dispatcher.add_handler( MessageHandler(Filters.text(Config.PRODUCT_BUTTONS), product_button_handler)) updater.dispatcher.add_handler( MessageHandler(Filters.text("بازگشت به منوی اصلی ◀️"), start_handler)) updater.dispatcher.add_handler(MessageHandler(Filters.all, help_command)) updater.start_polling() updater.idle()
def main(): updater = Updater(token=TOKEN, use_context=True) dispatcher = updater.dispatcher # Register callback functions start_handler = CommandHandler('start', start) dispatcher.add_handler(start_handler) join_handler = CommandHandler('join', join) dispatcher.add_handler(join_handler) status_handler = CommandHandler('status', status) dispatcher.add_handler(status_handler) help_handler = CommandHandler('help', help) dispatcher.add_handler(help_handler) move_handler = MessageHandler(Filters.text([SCISSORS, PAPER, STONE]), handle_move) dispatcher.add_handler(move_handler) # Set up webhook/poll, depending on dev or prod mode if MODE == 'dev': # Use polling (i.e. getUpdates API method) if in development mode: # Periodically connects to Telegram's servers to check for new update LOGGER.info("Starting bot in development mode...") updater.start_polling() elif MODE == 'prod': # Use webhooks if in production mode: # Whenever a new update for our bot arrives, Telegram sends that update to a specified URL. LOGGER.info("Starting bot in production mode...") updater.start_webhook(listen='0.0.0.0', port=PORT, url_path=TOKEN) updater.bot.set_webhook('https://{}.herokuapp.com/{}'.format( HEROKU_APP_NAME, TOKEN)) LOGGER.info("Webhook set at https://{}.herokuapp.com/<token>".format( HEROKU_APP_NAME)) updater.idle() else: LOGGER.error( "Invalid TELEGRAM_SPS_BOT_MODE value! Should be 'dev' or 'prod'.") sys.exit(1)
def main(token): logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG) updater = Updater(token=read_token(token), use_context=True) dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler(['start', 'help'], help)) dispatcher.add_handler(CommandHandler('new', new_game)) dispatcher.add_handler(CommandHandler('go', start_game)) dispatcher.add_handler(CommandHandler('reg', register)) dispatcher.add_handler( MessageHandler( Filters.text([ START_BUTTON_TEXT, WORD_EXPLAINED_BUTTON_TEXT, WORD_SKIPPED_BUTTON_TEXT ]), explain_cb)) updater.start_polling() updater.idle()
def send_admin_msg(users, bot, update): if Filters.text(update.message): for user in users: bot.send_message(chat_id=user.id, text=update.message.text, parse_mode=ParseMode.HTML) sleep(1) elif Filters.sticker(update.message): for user in users: bot.send_sticker(chat_id=user.id, sticker=update.message.sticker.file_id) sleep(1) elif Filters.photo(update.message): for photo in update.message.photo: for user in users: bot.send_photo(chat_id=user.id, photo=photo.file_id, caption=update.message.caption) sleep(1) elif Filters.video(update.message): for user in users: bot.send_video(chat_id=user.id, video=update.message.video.file_id, caption=update.message.caption) sleep(1) elif Filters.audio(update.message): for user in users: bot.send_audio(chat_id=user.id, audio=update.message.audio.file_id, caption=update.message.caption) sleep(SEND_SLEEP) elif Filters.document(update.message): for user in users: bot.send_document(chat_id=user.id, document=update.message.document.file_id, caption=update.message.caption) sleep(1) else: return
def filter(self, message): return Filters.text(message) and self.f(message.text)