Skip to content

Commit 994e08d

Browse files
committed
FIX: Cancel comments via inline keyboard
fixes #19
1 parent 1576a6c commit 994e08d

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

main.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,41 @@ def comment(bot, update):
168168
if len(params) > 1:
169169
text = " ".join(params[1:])
170170
logger.debug("New comment! {}!".format(user_id))
171-
send_message(chat_id, translate("userComment", lang_id))
172-
for admin_id in db.get_admins():
173-
send_message(admin_id, "New comment:\n\n{}\n\n{} | {} | {} | @{} | {}".format(text, user_id, first_name,
174-
last_name, username,
175-
lang_id))
176171

172+
bot.sendMessage(chat_id=chat_id, text=translate("userComment", lang_id))
173+
for admin_id in db.get_admins():
174+
bot.sendMessage(admin_id, "New comment:\n\n{}\n\n{} | {} | {} | @{} | {}".format(text, user_id, first_name,
175+
last_name, username,
176+
lang_id))
177177
logger.debug("Set {}'s state to IDLE!".format(user_id))
178178
user.set_state(UserState.IDLE)
179179
else:
180180
# The user just wrote "/comment" -> Ask him to send a message
181181
logger.debug("Add {} to comment_list!".format(user_id))
182-
comment_keyboard = ReplyKeyboardMarkup([[KeyboardButton(translate("cancel", lang_id))]])
183-
send_message(chat_id, translate("sendCommentNow", lang_id), reply_markup=comment_keyboard)
184-
user_state.set_state(UserState.COMMENTING)
182+
183+
keyboard = [[InlineKeyboardButton(text=translate("cancel", lang_id), callback_data="cancel_comment")]]
184+
reply_markup = InlineKeyboardMarkup(keyboard)
185+
186+
bot.sendMessage(chat_id=chat_id, text=translate("sendCommentNow", lang_id), reply_markup=reply_markup)
187+
user.set_state(UserState.COMMENTING)
185188

186189

187190
def cancel(bot, update):
188-
user_id = update.message.from_user.id
191+
user_id = update.callback_query.from_user.id
192+
message_id = update.callback_query.message.message_id
193+
callback_query_id = update.callback_query.id
189194
chat_id = update.message.chat_id
190-
db = DBwrapper.get_instance()
191-
lang_id = db.get_lang_id(user_id)
192195

193196
state_handler = StateHandler.get_instance()
194197
user = state_handler.get_user(user_id)
195198

196-
if user_state.get_state() == UserState.COMMENTING:
197-
user_state.set_state(UserState.IDLE)
198-
send_message(chat_id, translate("cancelledMessage", lang_id))
199+
if user.get_state() == UserState.COMMENTING:
200+
db = DBwrapper.get_instance()
201+
lang_id = db.get_lang_id(user_id)
202+
203+
user.set_state(UserState.IDLE)
204+
bot.editMessageText(chat_id=chat_id, message_id=message_id, text=translate("cancelledMessage", lang_id))
205+
bot.answerCallbackQuery(callback_query_id=callback_query_id, text=translate("cancelledMessage", lang_id))
199206

200207

201208
def answer(bot, update):
@@ -245,6 +252,8 @@ def callback_eval(bot, update):
245252
elif query_data == "com_ch_lang":
246253
language(bot, update)
247254

255+
elif query_data == "cancel_comment":
256+
cancel(bot, update)
248257

249258
def send_message(chat_id, text, message_id=None, parse_mode=None, reply_markup=None, game_id=None):
250259
tg_bot.sendMessage(chat_id=chat_id, text=text, reply_to_message_id=message_id, parse_mode=parse_mode,

0 commit comments

Comments
 (0)