Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
Use edit message in help and settings
Browse files Browse the repository at this point in the history
Signed-off-by: Ayra Hikari <[email protected]>
  • Loading branch information
AyraHikari committed Sep 24, 2018
1 parent 08b0a41 commit dc46f1b
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions tg_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

3 comments on commit dc46f1b

@PaulSonOfLars
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What rate limits get applied to message editing, and how do you handle the case where the message is too old to be edited? I'm guessing that throws an exception.

@AyraHikari
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It work on old message, but dont know what exception
And tested in my bot Anna

@PaulSonOfLars
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How old is old?

Please sign in to comment.