def on_callback_query(query: tg.CallbackQuery): query_id = query.id telegram_user_id = query.from_user.id chat = bot_backend.get_chat_by_telegram_id(telegram_user_id, True, Chat.Sources.CALLBACK) data = parse_get_mode_control_button_data(query.data) if data[0] not in CALLBACK_ACTIONS: bot.answer_callback_query(query_id, t('bot.callback.error')) return session_id = int(data[1]) session = bot_backend.get_session_by_id(session_id) if session is None: bot.answer_callback_query(query_id, t('bot.callback.error')) return if session.chat.id != chat.id: bot.answer_callback_query(query_id, t('bot.callback.error')) return if data[0] == CALLBACK_ACTION_CHANGE_MODE: mode = data[2] if mode not in [ Session.Modes.HORIZONTAL, Session.Modes.VERTICAL, Session.Modes.TWO_ROWS, Session.Modes.SQUARE ]: mode = Session.Modes.HORIZONTAL bot_backend.change_mode(session, mode) bot_backend.push_history_item(session) send_combined_image(session)
def on_inline_query(query: tg.InlineQuery): telegram_user_id = query.from_user.id query_id = query.id query = query.query if len(query) == 0: txt = t('bot.inline.switch_pm_text') bot.answer_inline_query(query_id, [], switch_pm_text=txt, switch_pm_parameter='start') return try: session = Session.get_by_query_token(query) except: bot.answer_inline_query(query_id, [], switch_pm_text=t('bot.inline.switch_pm_text'), switch_pm_parameter='start') return if session is None or session.chat.telegram_id != telegram_user_id: bot.answer_inline_query(query_id, [], switch_pm_text=t('bot.inline.switch_pm_text'), switch_pm_parameter='start') return photo = tg.InlineQueryResultCachedPhoto( '%s - %s' % (str(session.id), str(query_id)), session.telegram_photo_file_id) bot.answer_inline_query(query_id, results=[photo])
def on_soedini_command(message: tg.Message): telegram_chat_id = message.chat.id chat = bot_backend.get_chat_by_telegram_id(telegram_chat_id, True, Chat.Sources.SOEDINI) if not bot_backend.chat_has_active_session(chat): bot.send_message(chat.telegram_id, t('bot.state.error')) return session = bot_backend.get_active_session(chat) photos = session.photo_set.all() if len(photos) == 0: bot.send_message(chat.telegram_id, t('bot.state.error')) return bot_backend.finish_session(session) bot_backend.push_history_item(session) send_combined_image(session)
def get_control_markup(session: Session, images_count: int) -> tg.InlineKeyboardMarkup: markup = tg.InlineKeyboardMarkup(row_width=4) mode_is_horizontal = session.mode == Session.Modes.HORIZONTAL mode_is_vertical = session.mode == Session.Modes.VERTICAL mode_is_two_rows = session.mode == Session.Modes.TWO_ROWS # mode_is_square = session.mode == Session.Modes.SQUARE button_h = get_mode_control_button(Session.Modes.HORIZONTAL.label, Session.Modes.HORIZONTAL, session.id, mode_is_horizontal) button_v = get_mode_control_button(Session.Modes.VERTICAL.label, Session.Modes.VERTICAL, session.id, mode_is_vertical) markup.row(button_h) markup.row(button_v) if images_count > 2: button_2r = get_mode_control_button(Session.Modes.TWO_ROWS.label, Session.Modes.TWO_ROWS, session.id, mode_is_two_rows) # button_s = get_mode_control_button(Session.Modes.SQUARE.label, # Session.Modes.SQUARE, # session.id, # mode_is_square) markup.row(button_2r) inline_query = session.query_token button_send = tg.InlineKeyboardButton(t('bot.buttons.send'), switch_inline_query=inline_query) markup.add(button_send) return markup
def on_cancel_command(message): telegram_chat_id = message.chat.id chat = bot_backend.get_chat_by_telegram_id(telegram_chat_id, True, Chat.Sources.CANCEL) if bot_backend.chat_has_active_session(chat): session = bot_backend.get_active_session(chat) bot_backend.cancel_session(session) bot.send_message(telegram_chat_id, t('bot.messages.cancel'))
def on_photo_message(message: tg.Message): telegram_chat_id = message.chat.id if len(message.photo) == 0: return file_id = message.photo[-1].file_id chat = bot_backend.get_chat_by_telegram_id(telegram_chat_id, True, Chat.Sources.PHOTO) new_session_created = False if not bot_backend.chat_has_active_session(chat): active_session = bot_backend.create_session(chat) new_session_created = True else: active_session = bot_backend.get_active_session(chat) bot_backend.add_photo_to_session(active_session, file_id) if new_session_created: bot.send_message(chat.telegram_id, t('bot.messages.continue'))
def on_help_command(message: tg.Message): telegram_chat_id = message.chat.id chat = bot_backend.get_chat_by_telegram_id(telegram_chat_id, True, Chat.Sources.HELP) bot.send_message(chat.telegram_id, t('bot.messages.start'))
def get_control_button_caption(mode_str: str, is_active: bool): if not is_active: return mode_str return emoji.emojize(t('bot.buttons.mode.title', mode=mode_str))