def __send(bot: telegram.Bot, chat_id, reply_to_message_id, date, button_data) -> None: keyboard = [ [telegram.InlineKeyboardButton("Показать оригинал", callback_data=(get_callback_data(button_data)))] ] reply_markup = telegram.InlineKeyboardMarkup(keyboard) bot.send_message(chat_id, f'Баян! Уже было {relative_date(date)}', reply_to_message_id=reply_to_message_id, reply_markup=reply_markup)
def get_reply_markup(buttons): """ Инлайн-кнопки под сообщением """ keyboard = [] for line in buttons: keyboard.append([ telegram.InlineKeyboardButton( button_title, callback_data=(get_callback_data(button_data))) for button_title, button_data in line ]) return telegram.InlineKeyboardMarkup(keyboard)
def get_reply_markup(buttons: List[List[Button]]) -> Optional[telegram.InlineKeyboardMarkup]: """ Инлайн-кнопки под сообщением """ if not buttons: return None keyboard = [] for line in buttons: keyboard.append([ telegram.InlineKeyboardButton( button.title, callback_data=(get_callback_data(button.get_data()))) for button in line ]) return telegram.InlineKeyboardMarkup(keyboard)
def __send(bot: telegram.Bot, chat_id, reply_to_message_id, date, button_data) -> None: def link(chat_id, msg_id, text): prefix = '-100' cid_str = str(chat_id) if not cid_str.startswith(prefix): return text cid_without_100 = cid_str[len(prefix):] return f'<a href="https://t.me/c/{cid_without_100}/{msg_id}">{text}</a>' keyboard = [ [telegram.InlineKeyboardButton("Показать оригинал", callback_data=(get_callback_data(button_data)))] ] reply_markup = telegram.InlineKeyboardMarkup(keyboard) orig_photo: Photo = button_data['orig_photo'] orig_msg_id = orig_photo.message_id msg = f'Баян! Уже было {link(chat_id, orig_msg_id, relative_date(date))}' bot.send_message(chat_id, msg, reply_to_message_id=reply_to_message_id, reply_markup=reply_markup, parse_mode='HTML')
def leave(bot, update): chat_id = update.message.chat_id bot.sendChatAction(chat_id, ChatAction.TYPING) leaves = LeaveCollector.get_leaves(chat_id, 3) joins = LeaveCollector.get_joins(chat_id, 3) reply_markup = None result = "" if len(leaves) > 0: leaves_text = "\n".join(leaves) result = "Убыло за 3 дня:\n\n{}".format(leaves_text) data = { "name": 'last_word', "leaves_uid": LeaveCollector.get_leaves(chat_id, 3, return_id=True) } keyboard = [[ InlineKeyboardButton( "Показать последние слова (нажмите там Start)", callback_data=(get_callback_data(data))) ]] reply_markup = InlineKeyboardMarkup(keyboard) if len(joins) > 0: joins_text = "\n".join(joins) if not result: # здесь это аналогично `if len(leaves) == 0:` result = "Прибыло за 3 дня:\n\n{}" else: result += "\n\nПрибыло:\n\n{}" result = result.format(joins_text) if not result: result = "За 3 дня ничего не произошло" bot.sendMessage(chat_id, result, parse_mode='HTML', reply_markup=reply_markup)
def off_cmd_for_user(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id msg_id = update.message.message_id if not check_admin(bot, chat_id, user_id): bot.sendMessage(chat_id, 'Хуй тебе, а не плохишам команды выключать.', reply_to_message_id=msg_id) return # вычисляем название команды cmd_name = None words = update.message.text.split() reply_to_msg = update.message.reply_to_message if len(words) < 2: if not reply_to_msg: bot.sendMessage(chat_id, 'Ты забыл указать команду.', reply_to_message_id=msg_id) return for entity, entity_text in reply_to_msg.parse_entities().items(): if entity.type == 'bot_command': cmd_name = entity_text break else: cmd_name = words[1] valid_cmd_name = is_valid_command(cmd_name) if not valid_cmd_name: bot.sendMessage(chat_id, f'Нет такой команды: {cmd_name}.', reply_to_message_id=msg_id) return # вычисляем кому отключать if reply_to_msg: plohish_id = reply_to_msg.from_user.id plohish_name = '@' + reply_to_msg.from_user.username else: # если не указали кому, то отключаем для всех if len(words) < 3: off_cmd(bot, update) return plohish_name = words[2] plohish_id = User.get_id_by_name(plohish_name) if not plohish_id: bot.sendMessage(chat_id, f'Нет такого плохиша: {words[2]}', reply_to_message_id=msg_id) return plohish_cmd_cache_key = f'plohish_cmd:{chat_id}:{plohish_id}:{valid_cmd_name}' disabled = cache.get(plohish_cmd_cache_key) if disabled: bot.sendMessage( chat_id, f'Команда /{valid_cmd_name} у плохиша {plohish_name} уже не работает' ) return cache.set(plohish_cmd_cache_key, True, time=MONTH) if reply_to_msg: data = { "name": '/off', "bot_command": cmd_name, "plohish_id": plohish_id, "valid_cmd_name": valid_cmd_name } keyboard = [[ InlineKeyboardButton("Отключить у всех", callback_data=(get_callback_data(data))) ]] reply_markup = InlineKeyboardMarkup(keyboard) bot.sendMessage( chat_id, f'Команда /{valid_cmd_name} у плохиша {plohish_name} теперь не работает.', reply_to_message_id=msg_id, reply_markup=reply_markup) return bot.sendMessage( chat_id, f'Команда /{valid_cmd_name} у плохиша {plohish_name} теперь не работает' )