def on_notification(self, data: dict): """ Handles incoming notifications (via Webhook) :param data: notification data """ KEEL_NOTIFICATION_COUNTER.inc() identifier = data.get("identifier", None) title = data.get("name", None) type = data.get("type", None) level = data.get("level", None) # success/failure message = data.get("message", None) text = "\n".join([ f"<b>{title}: {level}</b>", f"{identifier}", f"{type}", f"{message}", ]) for chat_id in self._config.TELEGRAM_CHAT_IDS.value: send_message( self.bot, chat_id, text, parse_mode=ParseMode.HTML, menu=None )
def _version_callback(self, update: Update, context: CallbackContext): bot = context.bot message = update.effective_message chat_id = update.effective_chat.id from keel_telegram_bot import __version__ text = __version__ send_message(bot, chat_id, text, reply_to=message.message_id)
def execute(update: Update, context: CallbackContext, item: dict, data: dict): bot = context.bot message = update.effective_message chat_id = update.effective_chat.id self._api_client.delete(item["id"], item["identifier"], voter) text = f"Deleted {item['identifier']}" send_message(bot, chat_id, text, reply_to=message.message_id, menu=ReplyKeyboardRemove(selective=True))
def _help_callback(self, update: Update, context: CallbackContext): bot = context.bot message = update.effective_message chat_id = update.effective_chat.id from telegram_click import generate_command_list text = generate_command_list(update, context) send_message(bot, chat_id, text, parse_mode=ParseMode.MARKDOWN, reply_to=message.message_id)
def cancel_keyboard_callback(self, update: Update, context: CallbackContext): bot = context.bot chat_id = update.effective_chat.id user_id = update.effective_user.id message_id = update.effective_message.message_id text = "Cancelled" send_message(bot, chat_id, text, reply_to=message_id, menu=ReplyKeyboardRemove(selective=True)) if user_id in self.awaiting_response: self.awaiting_response.pop(user_id)
def _list_approvals_callback(self, update: Update, context: CallbackContext, archived: bool, approved: bool, rejected: bool) -> None: """ List pending approvals """ bot = context.bot message = update.effective_message chat_id = update.effective_chat.id items = self._api_client.get_approvals() rejected_items = list(filter(lambda x: x[KEY_REJECTED], items)) archived_items = list(filter(lambda x: x[KEY_ARCHIVED], items)) pending_items = list(filter( lambda x: x not in archived_items and x not in rejected_items and x[KEY_VOTES_RECEIVED] < x[KEY_VOTES_REQUIRED], items)) approved_items = list( filter(lambda x: x not in rejected_items and x not in archived_items and x not in pending_items, items)) lines = [] if archived: lines.append("\n".join([ f"<b>=== Archived ({len(archived_items)}) ===</b>", "", "\n\n".join(list(map(lambda x: "> " + approval_to_str(x), archived_items))) ]).strip()) if approved: lines.append("\n".join([ f"<b>=== Approved ({len(approved_items)}) ===</b>", "", "\n\n".join(list(map(lambda x: "> " + approval_to_str(x), approved_items))), ]).strip()) if rejected: lines.append("\n".join([ f"<b>=== Rejected ({len(rejected_items)}) ===</b>", "", "\n\n".join(list(map(lambda x: "> " + approval_to_str(x), rejected_items))), ]).strip()) lines.append("\n".join([ f"<b>=== Pending ({len(pending_items)}) ===</b>", "", "\n\n".join(list(map(lambda x: "> " + approval_to_str(x), pending_items))), ])) text = "\n\n".join(lines).strip() send_message(bot, chat_id, text, reply_to=message.message_id, parse_mode=ParseMode.HTML)
def _start_callback(self, update: Update, context: CallbackContext) -> None: """ Welcomes a new user with a greeting message :param update: the chat update object :param context: telegram context """ bot = context.bot chat_id = update.effective_chat.id user_first_name = update.effective_user.first_name if not CONFIG_ADMINS.evaluate(update, context): send_message(bot, chat_id, "Sorry, you do not have permissions to use this bot.") return send_message(bot, chat_id, f"Welcome {user_first_name},\nthis is your keel-telegram-bot instance, ready to go!")
def await_user_selection(self, update: Update, context: CallbackContext, selection: str or None, choices: List[Any], key: callable, callback: callable, callback_data: dict = None): """ Sends a ReplyKeyboard to the user and waits for a valid selection. :param update: Update :param context: CallbackContext :param selection: the current user selection (if any) :param choices: list of choices to select from :param key: function to create unique string key for a choice :param callback: the function to call, when a selection was made :param callback_data: data to pass to the callback function """ bot = context.bot chat_id = update.effective_chat.id message_id = update.effective_message.message_id user_id = update.effective_user.id fuzzy_matches = fuzzy_match(selection, choices=choices, key=key, limit=5) # check if something matches perfectly perfect_matches = list(filter(lambda x: x[1] == 100, fuzzy_matches)) if len(perfect_matches) == 1: choice = perfect_matches[0][0] callback(update, context, choice, callback_data) return # send reply keyboard with fuzzy matches to user keyboard_texts = list(map(lambda x: "{}".format(key(x[0])), fuzzy_matches)) keyboard = self.build_reply_keyboard(keyboard_texts) text = "No unique perfect match found, please select one of the menu options" self.await_response( user_id=user_id, options=keyboard_texts, callback=self._on_user_selection, callback_data={ "choices": choices, "key": key, "callback": callback, "callback_data": callback_data, }) send_message(bot, chat_id, text, parse_mode=ParseMode.MARKDOWN, reply_to=message_id, menu=keyboard)
def on_new_pending_approval(self, item: dict): """ Handles new pending approvals by sending a message including an inline keyboard to all configured chat ids :param item: new pending approval """ text = approval_to_str(item) keyboard_items = { "Approve": BUTTON_DATA_APPROVE, "Reject": BUTTON_DATA_REJECT } keyboard = self._build_inline_keyboard(keyboard_items) for chat_id in self._config.TELEGRAM_CHAT_IDS.value: send_message( self.bot, chat_id, text, parse_mode=ParseMode.HTML, menu=keyboard )
def on_new_pending_approval(self, item: dict): """ Handles new pending approvals by sending a message including an inline keyboard to all configured chat ids :param item: new pending approval """ text = approval_to_str(item) menu = self.create_approval_notification_menu(item) for chat_id in self._config.TELEGRAM_CHAT_IDS.value: try: response = send_message(self.bot, chat_id, text, parse_mode=ParseMode.HTML, menu=menu) self._register_message(response.chat_id, response.message_id, item["id"], item["identifier"]) except Exception as ex: LOGGER.exception(ex)
def _config_callback(self, update: Update, context: CallbackContext): bot = context.bot message = update.effective_message chat_id = update.effective_chat.id text = self._config.print(TomlFormatter()) send_message(bot, chat_id, text, reply_to=message.message_id)