From dc46f1b9730ee73e1a98f6ad3c4f4d4c879e42b2 Mon Sep 17 00:00:00 2001 From: Ayra Hikari Date: Tue, 25 Sep 2018 03:42:16 +0700 Subject: [PATCH 1/4] Use edit message in help and settings Signed-off-by: Ayra Hikari --- tg_bot/__main__.py | 78 +++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/tg_bot/__main__.py b/tg_bot/__main__.py index 74832101a..d5ded0153 100644 --- a/tg_bot/__main__.py +++ b/tg_bot/__main__.py @@ -191,33 +191,40 @@ def help_button(bot: Bot, update: Update): module = mod_match.group(1) text = "Here is the help for the *{}* module:\n".format(HELPABLE[module].__mod_name__) \ + HELPABLE[module].__help__ - query.message.reply_text(text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Back", callback_data="help_back")]])) elif prev_match: curr_page = int(prev_match.group(1)) - query.message.reply_text(HELP_STRINGS, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( - paginate_modules(curr_page - 1, HELPABLE, "help"))) + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=HELP_STRINGS, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + paginate_modules(curr_page - 1, HELPABLE, "help"))) elif next_match: next_page = int(next_match.group(1)) - query.message.reply_text(HELP_STRINGS, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( - paginate_modules(next_page + 1, HELPABLE, "help"))) + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=HELP_STRINGS, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + paginate_modules(next_page + 1, HELPABLE, "help"))) elif back_match: - query.message.reply_text(text=HELP_STRINGS, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))) + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=HELP_STRINGS, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))) # ensure no spinny white circle bot.answer_callback_query(query.id) - query.message.delete() except BadRequest as excp: if excp.message == "Message is not modified": pass @@ -296,44 +303,51 @@ def settings_button(bot: Bot, update: Update): text = "*{}* has the following settings for the *{}* module:\n\n".format(escape_markdown(chat.title), CHAT_SETTINGS[module].__mod_name__) + \ CHAT_SETTINGS[module].__chat_settings__(chat_id, user.id) - query.message.reply_text(text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( - [[InlineKeyboardButton(text="Back", - callback_data="stngs_back({})".format(chat_id))]])) + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + [[InlineKeyboardButton(text="Back", + callback_data="stngs_back({})".format(chat_id))]])) elif prev_match: chat_id = prev_match.group(1) curr_page = int(prev_match.group(2)) chat = bot.get_chat(chat_id) - query.message.reply_text("Hi there! There are quite a few settings for {} - go ahead and pick what " + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( - paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + reply_markup=InlineKeyboardMarkup( + paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", + chat=chat_id))) elif next_match: chat_id = next_match.group(1) next_page = int(next_match.group(2)) chat = bot.get_chat(chat_id) - query.message.reply_text("Hi there! There are quite a few settings for {} - go ahead and pick what " + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( - paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + reply_markup=InlineKeyboardMarkup( + paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", + chat=chat_id))) elif back_match: chat_id = back_match.group(1) chat = bot.get_chat(chat_id) - query.message.reply_text(text="Hi there! There are quite a few settings for {} - go ahead and pick what " + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " "you're interested in.".format(escape_markdown(chat.title)), - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id))) # ensure no spinny white circle bot.answer_callback_query(query.id) - query.message.delete() except BadRequest as excp: if excp.message == "Message is not modified": pass From 9e0c12847c5601e15d621e9844beaeaf1231ad6f Mon Sep 17 00:00:00 2001 From: Ayra Hikari Date: Tue, 25 Sep 2018 05:19:03 +0700 Subject: [PATCH 2/4] Error Flood control handling in help and settings button Signed-off-by: Ayra Hikari --- tg_bot/__main__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tg_bot/__main__.py b/tg_bot/__main__.py index d5ded0153..c05bc04a1 100644 --- a/tg_bot/__main__.py +++ b/tg_bot/__main__.py @@ -225,7 +225,7 @@ def help_button(bot: Bot, update: Update): # ensure no spinny white circle bot.answer_callback_query(query.id) - except BadRequest as excp: + except Exception as excp: if excp.message == "Message is not modified": pass elif excp.message == "Query_id_invalid": @@ -233,6 +233,9 @@ def help_button(bot: Bot, update: Update): elif excp.message == "Message can't be deleted": pass else: + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=excp.message) LOGGER.exception("Exception in help buttons. %s", str(query.data)) @@ -348,7 +351,7 @@ def settings_button(bot: Bot, update: Update): # ensure no spinny white circle bot.answer_callback_query(query.id) - except BadRequest as excp: + except Exception as excp: if excp.message == "Message is not modified": pass elif excp.message == "Query_id_invalid": @@ -356,6 +359,9 @@ def settings_button(bot: Bot, update: Update): elif excp.message == "Message can't be deleted": pass else: + bot.edit_message_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=excp.message) LOGGER.exception("Exception in settings buttons. %s", str(query.data)) From f0389c26aa533f6112ad0b974726bfd4bee45fec Mon Sep 17 00:00:00 2001 From: Ayra Hikari Date: Tue, 25 Sep 2018 06:32:42 +0700 Subject: [PATCH 3/4] Change Exception to BadRequest Signed-off-by: Ayra Hikari --- tg_bot/__main__.py | 78 +++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/tg_bot/__main__.py b/tg_bot/__main__.py index c05bc04a1..2a946761a 100644 --- a/tg_bot/__main__.py +++ b/tg_bot/__main__.py @@ -192,11 +192,11 @@ def help_button(bot: Bot, update: Update): text = "Here is the help for the *{}* module:\n".format(HELPABLE[module].__mod_name__) \ + HELPABLE[module].__help__ bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( - [[InlineKeyboardButton(text="Back", callback_data="help_back")]])) + message_id=query.message.message_id, + text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( + [[InlineKeyboardButton(text="Back", callback_data="help_back")]])) elif prev_match: curr_page = int(prev_match.group(1)) @@ -210,22 +210,22 @@ def help_button(bot: Bot, update: Update): elif next_match: next_page = int(next_match.group(1)) bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=HELP_STRINGS, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( + message_id=query.message.message_id, + text=HELP_STRINGS, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( paginate_modules(next_page + 1, HELPABLE, "help"))) elif back_match: bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=HELP_STRINGS, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))) + message_id=query.message.message_id, + text=HELP_STRINGS, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup(paginate_modules(0, HELPABLE, "help"))) # ensure no spinny white circle bot.answer_callback_query(query.id) - except Exception as excp: + except BadRequest as excp: if excp.message == "Message is not modified": pass elif excp.message == "Query_id_invalid": @@ -234,8 +234,8 @@ def help_button(bot: Bot, update: Update): pass else: bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=excp.message) + message_id=query.message.message_id, + text=excp.message) LOGGER.exception("Exception in help buttons. %s", str(query.data)) @@ -307,10 +307,10 @@ def settings_button(bot: Bot, update: Update): CHAT_SETTINGS[module].__mod_name__) + \ CHAT_SETTINGS[module].__chat_settings__(chat_id, user.id) bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( + message_id=query.message.message_id, + text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Back", callback_data="stngs_back({})".format(chat_id))]])) @@ -319,39 +319,39 @@ def settings_button(bot: Bot, update: Update): curr_page = int(prev_match.group(2)) chat = bot.get_chat(chat_id) bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " + "you're interested in.".format(chat.title), + reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + chat=chat_id))) elif next_match: chat_id = next_match.group(1) next_page = int(next_match.group(2)) chat = bot.get_chat(chat_id) bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " + "you're interested in.".format(chat.title), + reply_markup=InlineKeyboardMarkup( paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + chat=chat_id))) elif back_match: chat_id = back_match.group(1) chat = bot.get_chat(chat_id) bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(escape_markdown(chat.title)), - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", - chat=chat_id))) + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " + "you're interested in.".format(escape_markdown(chat.title)), + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", + chat=chat_id))) # ensure no spinny white circle bot.answer_callback_query(query.id) - except Exception as excp: + except BadRequest as excp: if excp.message == "Message is not modified": pass elif excp.message == "Query_id_invalid": @@ -360,8 +360,8 @@ def settings_button(bot: Bot, update: Update): pass else: bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=excp.message) + message_id=query.message.message_id, + text=excp.message) LOGGER.exception("Exception in settings buttons. %s", str(query.data)) From c1a52119e7b56c2e9f248ec2da8e9992985811f6 Mon Sep 17 00:00:00 2001 From: Ayra Hikari Date: Tue, 25 Sep 2018 07:07:18 +0700 Subject: [PATCH 4/4] use query edit message instead of bot edit message Signed-off-by: Ayra Hikari --- tg_bot/__main__.py | 70 ++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/tg_bot/__main__.py b/tg_bot/__main__.py index 2a946761a..5dc35c4e7 100644 --- a/tg_bot/__main__.py +++ b/tg_bot/__main__.py @@ -191,18 +191,14 @@ def help_button(bot: Bot, update: Update): module = mod_match.group(1) text = "Here is the help for the *{}* module:\n".format(HELPABLE[module].__mod_name__) \ + HELPABLE[module].__help__ - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( + query.message.edit_text(text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Back", callback_data="help_back")]])) elif prev_match: curr_page = int(prev_match.group(1)) - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=HELP_STRINGS, + query.message.edit_text(text=HELP_STRINGS, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, HELPABLE, "help"))) @@ -233,9 +229,6 @@ def help_button(bot: Bot, update: Update): elif excp.message == "Message can't be deleted": pass else: - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=excp.message) LOGGER.exception("Exception in help buttons. %s", str(query.data)) @@ -306,11 +299,11 @@ def settings_button(bot: Bot, update: Update): text = "*{}* has the following settings for the *{}* module:\n\n".format(escape_markdown(chat.title), CHAT_SETTINGS[module].__mod_name__) + \ CHAT_SETTINGS[module].__chat_settings__(chat_id, user.id) - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=text, - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup( + query.message.edit_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text=text, + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Back", callback_data="stngs_back({})".format(chat_id))]])) @@ -318,36 +311,36 @@ def settings_button(bot: Bot, update: Update): chat_id = prev_match.group(1) curr_page = int(prev_match.group(2)) chat = bot.get_chat(chat_id) - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( - paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + query.message.edit_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " + "you're interested in.".format(chat.title), + reply_markup=InlineKeyboardMarkup( + paginate_modules(curr_page - 1, CHAT_SETTINGS, "stngs", + chat=chat_id))) elif next_match: chat_id = next_match.group(1) next_page = int(next_match.group(2)) chat = bot.get_chat(chat_id) - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(chat.title), - reply_markup=InlineKeyboardMarkup( - paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", - chat=chat_id))) + query.message.edit_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " + "you're interested in.".format(chat.title), + reply_markup=InlineKeyboardMarkup( + paginate_modules(next_page + 1, CHAT_SETTINGS, "stngs", + chat=chat_id))) elif back_match: chat_id = back_match.group(1) chat = bot.get_chat(chat_id) - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text="Hi there! There are quite a few settings for {} - go ahead and pick what " - "you're interested in.".format(escape_markdown(chat.title)), - parse_mode=ParseMode.MARKDOWN, - reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", - chat=chat_id))) + query.message.edit_text(chat_id=query.message.chat_id, + message_id=query.message.message_id, + text="Hi there! There are quite a few settings for {} - go ahead and pick what " + "you're interested in.".format(escape_markdown(chat.title)), + parse_mode=ParseMode.MARKDOWN, + reply_markup=InlineKeyboardMarkup(paginate_modules(0, CHAT_SETTINGS, "stngs", + chat=chat_id))) # ensure no spinny white circle bot.answer_callback_query(query.id) @@ -359,9 +352,6 @@ def settings_button(bot: Bot, update: Update): elif excp.message == "Message can't be deleted": pass else: - bot.edit_message_text(chat_id=query.message.chat_id, - message_id=query.message.message_id, - text=excp.message) LOGGER.exception("Exception in settings buttons. %s", str(query.data))