def periodic_state(bot, update): str_date = update.get_effective_message().text notification[Attr.date_time] = jdatetime.datetime.strptime( str_date, Config.accepted_datetime_format) general_message = TextMessage(BotMessages.periodic_state_selection) btn_list = [ TemplateMessageButton(BotMessages.only_once, ResponseValue.only_once, MessageButtonAction.default), TemplateMessageButton(BotMessages.iterative, ResponseValue.iterative, MessageButtonAction.default) ] message = TemplateMessage(general_message=general_message, btn_list=btn_list) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.periodic_state, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success_callback=success, failure_callback=failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler( TemplateResponseFilter(keywords=[ResponseValue.only_once]), ask_type), MessageHandler( TemplateResponseFilter(keywords=[ResponseValue.iterative]), period_type), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_periodic_state) ])
def period_type(bot, update): general_message = TextMessage(BotMessages.periodic_type_selection) btn_list = [ TemplateMessageButton(BotMessages.daily, ResponseValue.daily, MessageButtonAction.default), TemplateMessageButton(BotMessages.weekly, ResponseValue.weekly, MessageButtonAction.default), TemplateMessageButton(BotMessages.monthly, ResponseValue.monthly, MessageButtonAction.default) ] message = TemplateMessage(general_message=general_message, btn_list=btn_list) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.period_type, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success_callback=success, failure_callback=failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TemplateResponseFilter(keywords=[ResponseValue.daily]), ask_iterate_number), MessageHandler(TemplateResponseFilter(keywords=[ResponseValue.weekly]), ask_iterate_number), MessageHandler( TemplateResponseFilter(keywords=[ResponseValue.monthly]), ask_iterate_number), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_period_type) ])
def ask_picture(bot, update): if notification.get(Attr.type): notification[Attr.money_amount] = update.get_effective_message().text else: notification[Attr.type] = ResponseValue.normal message = TemplateMessage( general_message=TextMessage(BotMessages.picture_request), btn_list=[ TemplateMessageButton(BotMessages.no_picture_needed, ResponseValue.no_picture, MessageButtonAction.default) ]) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: UserData.ask_picture, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler( TemplateResponseFilter(keywords=[ResponseValue.no_picture]), ask_text), MessageHandler(PhotoFilter(), getting_picture), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_picture) ])
def get_tweet_text(bot, update): user_peer = update.get_effective_user() user_id = user_peer.peer_id user = get_user(user_id=user_id) if not check_user_registration(update, user_peer, user): return 0 general_message = TextMessage(ReadyMessage.send_text_twitter) btn_list = [ TemplateMessageButton(text=TMessage.cancel, value=TMessage.cancel, action=0) ] template_message = TemplateMessage(general_message=general_message, btn_list=btn_list) kwargs = { "message": template_message, "user_peer": user_peer, "try_times": 1 } bot.send_message(template_message, user_peer, success_callback=success, failure_callback=failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ CommandHandler("info", info), MessageHandler(TextFilter(), send_tweet), MessageHandler(DefaultFilter(), start_conversation) ])
def upload_file(bot, update): def file_upload_success(result, user_data): """Its the link of upload photo but u cant see anything with it because you need to take a token from server. actually this link is just for uploading a file not download. If you want to download this file you should use get_file_download_url() and take a token from server. """ logger.info("file upload success", extra={"tag": "info"}) def file_upload_failure(result, user_data): logger.error("file upload failure", extra={"tag": "error"}) message = TextMessage("Uploading ...") user_peer = update.get_effective_user() bot.send_message(message, user_peer, success_callback=success_send_message, failure_callback=failure_send_message) stream = dispatcher.get_conversation_data(update, "stream") bot.upload_file(file=stream, file_type="file", success_callback=file_upload_success, failure_callback=file_upload_failure) message = TextMessage( "Uploading is finish.\nyou can try this link and see nothing show to you\n" "finish conversion with below command\n" "[/finish](send:/finish)") bot.send_message(message, user_peer, success_callback=success_send_message, failure_callback=failure_send_message) dispatcher.register_conversation_next_step_handler( update, CommandHandler("/finish", finish_conversion))
def __init__(self, dispatcher): self.dispatcher = dispatcher self.handlers = [ CommandHandler(['start', 'menu'], self.show_menu), MessageHandler( TemplateResponseFilter(ButtonMessage.main_menu_message), self.show_menu) ]
def start_bot(bot, update): user = _get_peer(update) _is_user = is_user(user.peer_id) if _is_user: txt = BotMessage.choose_one_option txt_msg = TextMessage(txt) btn = [ TemplateMessageButton(ButtonMessage.tarjome, ButtonMessage.tarjome, ButtonAction.default), TemplateMessageButton(ButtonMessage.tafsir, ButtonMessage.tafsir, ButtonAction.default), TemplateMessageButton(ButtonMessage.read, ButtonMessage.read, ButtonAction.default) ] msg = TemplateMessage(txt_msg, btn) send_message(msg, _get_peer(update), Step.showing_menu) dispatcher.register_conversation_next_step_handler( update, [ CommandHandler("/msgtoall", get_message_of_send_to_all), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.tarjome), tarjome_step_1), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.tafsir), tafsir_step_1), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.read), read_step_1), MessageHandler(DefaultFilter(), start_bot) ]) else: add_user(user.peer_id, user.access_hash) txt = BotMessage.greeting txt_msg = TextMessage(txt) btn = [ TemplateMessageButton(ButtonMessage.tarjome, ButtonMessage.tarjome, ButtonAction.default), TemplateMessageButton(ButtonMessage.tafsir, ButtonMessage.tafsir, ButtonAction.default), TemplateMessageButton(ButtonMessage.read, ButtonMessage.read, ButtonAction.default) ] msg = TemplateMessage(txt_msg, btn) send_message(msg, _get_peer(update), Step.showing_menu) dispatcher.register_conversation_next_step_handler( update, [ MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.tarjome), tarjome_step_1), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.tafsir), tafsir_step_1), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.read), read_step_1), MessageHandler(DefaultFilter(), start_bot) ])
def download_file(bot, update): user_peer = update.get_effective_user() user_id = update.body.sender_user.peer_id message = TextMessage("Downloading ... ") bot.send_message(message, user_peer, success_callback=success_send_message, failure_callback=failure_send_message) file_id = update.get_effective_message().file_id def final_download_success(result, user_data): logger.info("download was successful", extra={"tag": "info"}) stream = user_data.get("byte_stream", None) dispatcher.set_conversation_data(update, "stream", stream) now = str(datetime.datetime.now().time().strftime('%Y-%m-%d_%H:%M:%f')) with open("doc_downloaded_" + now, "wb") as file: file.write(stream) file.close() def final_download_failure(result, user_data): logger.error("download was failure", extra={"tag": "error"}) bot.download_file(file_id=file_id, user_id=user_id, file_type="file", success_callback=final_download_success, failure_callback=final_download_failure) def get_download_url_success(result, user_data): logger.info("get download url successful", extra={"tag": "info"}) url = str(result.body.url) logger.info( "Link of downloaded photo:\n(Note : this link is for limited time)\n" + url, extra={"tag": "info"}) def get_download_url_failure(result, user_data): logger.error("get download url failure", extra={"tag": "error"}) bot.get_file_download_url(file_id, user_id, "file", success_callback=get_download_url_success, failure_callback=get_download_url_failure) message = TextMessage( "Download was successful\n" "use below command to upload the document we already downloaded\n" "[/upload](send:/upload)") bot.send_message(message, user_peer, success_callback=success_send_message, failure_callback=final_download_success) dispatcher.register_conversation_next_step_handler( update, CommandHandler("upload", upload_file))
def main(): """Start the bot.""" # Bale Bot Authorization Token updater = Updater("TOKEN") # Get the dispatcher to register handlers dp = updater.dispatcher # on different commands - answer in Bale dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) # on noncommand i.e message - echo the message on Bale dp.add_handler(MessageHandler(DefaultFilter(), echo)) # log all errors dp.add_error_handler(error) # Start the Bot updater.run()
def positive_answer(bot, update): message = TextMessage("*Your have answered 'yes'* \n" "end the conversion with below command: \n" "[/end](send:/end)") user_peer = update.get_effective_user() bot.send_message(message, user_peer, success_callback=success_send_message, failure_callback=failure_send_message) # Use CommandHandler to handle a command which is sent by client dispatcher.register_conversation_next_step_handler( update, CommandHandler("/end", finish_conversion))
def negative_answer(bot, update): message = TextMessage( "*Your have answered 'no'* \n" "Write a new question or end the conversion with below command: \n" "[/end](send:/end)") user_peer = update.get_effective_user() bot.send_message(message, user_peer, success_callback=success_send_message, failure_callback=failure_send_message) dispatcher.register_conversation_next_step_handler( update, [CommandHandler("/end", finish_conversion)])
def wrong_amount(bot, update): message = TextMessage(BotMessages.wrong_answer_pls_number) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.wrong_amount, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TextFilter(pattern=Pattern.number), ask_picture), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_amount) ])
def wrong_name_response(bot, update): message = TextMessage(BotMessages.wrong_answer_pls_text) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.wrong_name_response, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TextFilter(), finnish_notification_register), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_name_response) ])
def wrong_time(bot, update): message = TextMessage(BotMessages.wrong_format) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.wrong_time, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TextFilter(pattern=Pattern.persian_datetime), periodic_state), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_time) ])
def ask_card_number(bot, update): notification[Attr.type] = ResponseValue.debt message = TextMessage(BotMessages.card_number_entering) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.ask_card_number, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TextFilter(pattern=Pattern.card_number), ask_amount), MessageHandler(DefaultFilter(), wrong_card_number), CommandHandler([Command.start], conversation_starter) ])
def ask_iterate_number(bot, update): notification[ Attr.periodic_type] = update.get_effective_message().text_message message = TextMessage(BotMessages.iterate_number_selection) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.ask_iterate_number, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TextFilter(pattern=Pattern.number), ask_type), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_iterate_number) ])
def getting_picture(bot, update): notification[Attr.file_id] = update.body.message.file_id notification[Attr.file_access_hash] = update.body.message.access_hash notification[Attr.file_size] = update.body.message.file_size message = TextMessage(BotMessages.notification_text_entering) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.getting_picture, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler(TextFilter(), finnish_notification_register), MessageHandler(DefaultFilter(), wrong_name_response), CommandHandler([Command.start], conversation_starter) ])
def wrong_picture(bot, update): message = TextMessage(BotMessages.wrong_answer) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.wrong_picture, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success, failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler( TemplateResponseFilter(keywords=[ResponseValue.no_picture]), ask_text), MessageHandler(PhotoFilter(), getting_picture), CommandHandler([Command.start], conversation_starter), MessageHandler(DefaultFilter(), wrong_picture) ])
def get_search_text(bot, update): user_peer = update.get_effective_user() user_id = user_peer.peer_id user = get_user(user_id=user_id) if not check_user_registration(update, user_peer, user): return 0 text_message = TextMessage(ReadyMessage.send_search_text) kwargs = {"message": text_message, "user_peer": user_peer, "try_times": 1} bot.send_message(text_message, user_peer, success_callback=success, failure_callback=failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ CommandHandler("info", info), MessageHandler(TextFilter(), search_tweets), MessageHandler(DefaultFilter(), start_conversation) ])
def registration(bot, update): dispatcher.clear_conversation_data(update) user_peer = update.get_effective_user() auth = get_verify_link() dispatcher.set_conversation_data(update, "auth", auth) verify_link = auth['auth_url'] text_message = TextMessage( ReadyMessage.send_verify_number.format(verify_link)) kwargs = {"message": text_message, "user_peer": user_peer, "try_times": 1} bot.send_message(text_message, user_peer, success_callback=success, failure_callback=failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ CommandHandler("info", info), MessageHandler(TextFilter(), verify), MessageHandler(DefaultFilter(), start_conversation) ])
def download_file(bot, update): def success_get_download_url(result, user_data): print("success : ", result) url = str(result.body.url) url_link = TextMessage( "Link of downloaded photo:\n(Note : this link is for limited time)\n" + url) bot.send_message(url_link, user_peer, success_callback=success, failure_callback=failure) message = TextMessage("Downloading ... ") user_peer = update.get_effective_user() user_id = update.body.sender_user.peer_id bot.send_message(message, user_peer, success_callback=success, failure_callback=failure) file_id = update.body.message.file_id bot.download_file(file_id=file_id, user_id=user_id, file_type="file", success_callback=final_download_success, failure_callback=failure) bot.get_file_download_url(file_id, user_id, "file", success_callback=success_get_download_url, failure_callback=failure) message = TextMessage( "Download was successful\n" "use below command to upload that document we already downloaded\n" "[/upload](send:/upload)") bot.send_message(message, user_peer, success_callback=success, failure_callback=failure) dispatcher.register_conversation_next_step_handler( update, CommandHandler("upload", upload_file))
def conversation_starter(bot, update): global notification, in_conversation in_conversation = True notification.clear() notification[Attr.peer_id] = update.body.sender_user.peer_id notification[Attr.peer_access_hash] = update.body.sender_user.access_hash general_message = TextMessage(BotMessages.service_selection) btn_list = [ TemplateMessageButton(BotMessages.setup_notification, ResponseValue.setup_notification, MessageButtonAction.default), TemplateMessageButton(BotMessages.showing_receipts, ResponseValue.showing_receipts, MessageButtonAction.default) ] message = TemplateMessage(general_message=general_message, btn_list=btn_list) kwargs = { UserData.user_peer: update.get_effective_user(), UserData.step_name: Step.conversation_starter, UserData.message: message, UserData.attempt: SendingAttempt.first } bot.respond(update, message, success_callback=success, failure_callback=failure, kwargs=kwargs) dispatcher.register_conversation_next_step_handler(update, [ MessageHandler( TemplateResponseFilter( keywords=[ResponseValue.setup_notification]), ask_time), MessageHandler( TemplateResponseFilter(keywords=[ResponseValue.showing_receipts]), showing_receipts), MessageHandler(DefaultFilter(), wrong_periodic_state), CommandHandler([Command.start], conversation_starter) ])
def upload_file(bot, update): def file_upload_success(result, user_data): """Its the link of upload photo but u cant see anything with it because you need to take a token from server. actually this link is just for uploading a file not download. If you want to download this file you should use get_file_download_url() and take a token from server. """ print("upload was successful : ", result) print(user_data) url = user_data.get("url", None) url_message = TextMessage(url) bot.send_message(url_message, user_peer, success_callback=success, failure_callback=failure) message = TextMessage("Uploading ...") user_peer = update.get_effective_user() bot.send_message(message, user_peer, success_callback=success, failure_callback=failure) bot.upload_file(file="../files/upload_file_test.jpeg", file_type="file", success_callback=file_upload_success, failure_callback=failure) message = TextMessage( "Uploading is finish.\nyou can try this link and see nothing show to you\n" "finish conversion with below command\n" "[/finish](send:/finish)") bot.send_message(message, user_peer, success_callback=success, failure_callback=failure) dispatcher.register_conversation_next_step_handler( update, CommandHandler("/finish", finish_conversion))
def decorator(callback_func): handler = CommandHandler(commands, callback_func, include_template_response) self.add_handler(handler) return callback_func
value=TMessage.back, action=0) ] general_message = TextMessage(ReadyMessage.help) template_message = TemplateMessage(general_message=general_message, btn_list=btn_list) kwargs = { "message": template_message, "user_peer": user_peer, "try_times": 1 } bot.send_message(template_message, user_peer, success_callback=success_send_message, failure_callback=failure_send_message, kwargs=kwargs) dispatcher.finish_conversation(update) common_handlers = [ CommandHandler(commands="/start", callback=start_conversation, include_template_response=True), CommandHandler(commands="/help", callback=help_me, include_template_response=True), MessageHandler(TemplateResponseFilter(keywords=[TMessage.help]), help_me) ] updater.run()
btn_list = [ TemplateMessageButton(text=ButtonText.back, value=ButtonText.back, action=0) ] general_message = TextMessage(ReadyText.information) template_message = TemplateMessage(general_message=general_message, btn_list=btn_list) kwargs = { "message": template_message, "update": update, "bot": bot, "try_times": 1 } bot.respond(update, template_message, success_callback=success_send_message, failure_callback=failure_send_message, kwargs=kwargs) dispatcher.finish_conversation(update) # =================================== Handlers ========================================= common_handlers = [ MessageHandler(filters=TemplateResponseFilter(keywords=[ButtonText.back]), callback=start_conversation), CommandHandler(commands="start", callback=start_conversation, include_template_response=True) ]
send_message(message=template_message, peer=user_peer, step=Step.update_money_changer) dispatcher.register_conversation_next_step_handler( update, handlers=common_handlers + [ MessageHandler(TextFilter(Patterns.float_numbers), request_sender_name) ]) else: buttons_list = [BotButtons.back_to_main_menu] template_message = TemplateMessage(TextMessage(BotTexts.error), buttons_list) send_message(message=template_message, peer=user_peer, step=Step.update_money_changer) dispatcher.finish_conversation(update) # +++++++++++++++++++++++++++++ COMMON HANDLERS ++++++++++++++++++++++++++++++ common_handlers = [ CommandHandler(commands=["/start"], callback=start), CommandHandler(commands=["/help"], callback=help_me), CommandHandler(commands=["/report"], callback=get_excel_report), MessageHandler(TemplateResponseFilter(keywords=[BotButtons.help.value]), callback=help_me), MessageHandler( TemplateResponseFilter(keywords=[BotButtons.back_to_main_menu.value]), callback=start) ]
def tafsir_step_3(_, update): # check_message("s + {}".format(_get_msg(update).text), update) msg = _get_msg(update).text number_of_ayes = dispatcher.get_conversation_data(update, "number_of_ayes") if msg.isnumeric() and 1 <= int(msg) <= number_of_ayes: soore_number = dispatcher.get_conversation_data(update, "soore_number") soore = dispatcher.get_conversation_data(update, "soore") aye = get_aye(soore_number, int(msg)) aye_text = aye.aye_text tafsir_text = aye.tafsir_text btn = [ TemplateMessageButton(ButtonMessage.tarjome, ButtonMessage.tarjome, 0), TemplateMessageButton(ButtonMessage.tafsir, ButtonMessage.tafsir, 0), TemplateMessageButton(ButtonMessage.read, ButtonMessage.read, ButtonAction.default) ] def send_done(_, __): continue_msg = TextMessage(BotMessage.continue_msg) _msg = TemplateMessage(continue_msg, btn) send_message(_msg, _get_peer(update)) dispatcher.register_conversation_next_step_handler( update, [ MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.tafsir), tafsir_step_1), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.tarjome), tarjome_step_1), MessageHandler( TemplateResponseFilter(keywords=ButtonMessage.read), read_step_1), MessageHandler(DefaultFilter(), start_bot) ]) def send_fail(_, __): pass text_aye = BotMessage.aye_text.format( soore, persian.convert_en_numbers(int(msg)), aye_text) text_tafsir = BotMessage.tafsir_text.format(tafsir_text) all_msg = TextMessage(text_aye + "\n" + text_tafsir) bot.send_message(all_msg, _get_peer(update), success_callback=send_done, failure_callback=send_fail) else: soore = dispatcher.get_conversation_data(update, "soore") txt_msg = TextMessage( BotMessage.wrong_input + "\n" + BotMessage.choose_aye_number.format( soore, persian.convert_en_numbers(number_of_ayes))) send_message(txt_msg, _get_peer(update), Step.conversation_starter) dispatcher.register_conversation_next_step_handler( update, [ MessageHandler(TextFilter(), tafsir_step_3), MessageHandler(DefaultFilter(), start_bot), CommandHandler(Command.start, start_bot), ])
def __init__(self, dispatcher): self.dispatcher = dispatcher self.handlers = [ CommandHandler(['start', 'menu'], self.show_menu) ]
def send_message(message, user_peer, msg, sending_attempt): random_id = generate_random_id() kwargs = { UserData.random_id: random_id, UserData.db_msg: msg, UserData.base_message: message, UserData.user_peer: user_peer, UserData.sending_attempt: sending_attempt, UserData.logger: my_logger, UserData.send_message: send_message } updater.dispatcher.bot.send_message(message, user_peer, random_id=random_id, success_callback=reminder_success, failure_callback=reminder_failure, kwargs=kwargs) common_handlers = [ CommandHandler([Command.start], start, include_template_response=True), MessageHandler(TemplateResponseFilter(keywords=[ReadyText.add_reminder]), add_reminder), MessageHandler(TemplateResponseFilter(keywords=[ReadyText.show_receipts]), receipts), MessageHandler(TemplateResponseFilter(keywords=[ReadyText.help_me]), help_me), ]