Ejemplo n.º 1
0
    def _commands_command_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)
Ejemplo n.º 2
0
 def _start_callback(self, update: Update, context: CallbackContext) -> None:
     """
     Welcomes a new user with an example image and a greeting message
     :param update: the chat update object
     :param context: telegram context
     """
     bot = context.bot
     self._send_random_quote(update, context)
     greeting_message = self._config.TELEGRAM_GREETING_MESSAGE.value
     if greeting_message is not None and len(greeting_message) > 0:
         send_message(bot=bot, chat_id=update.message.chat_id, message=greeting_message)
Ejemplo n.º 3
0
    def _version_command_callback(self, update: Update, context: CallbackContext) -> None:
        """
        /stats command handler
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message = update.effective_message
        chat_id = update.effective_chat.id

        from infinitewisdom.const import __version__
        text = "{}".format(__version__)
        send_message(bot, chat_id, text, reply_to=message.message_id)
Ejemplo n.º 4
0
    def _stats_callback(self, update: Update, context: CallbackContext) -> None:
        """
        /stats command handler
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message = update.effective_message
        chat_id = update.effective_chat.id

        text = format_metrics()

        send_message(bot, chat_id, text, reply_to=message.message_id)
Ejemplo n.º 5
0
    def _reply_info_command_callback(self, update: Update, context: CallbackContext,
                                     entity_of_reply: Image or None) -> None:
        """
        /info reply command handler
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message = update.effective_message
        chat_id = update.effective_chat.id

        send_message(bot, chat_id, "{}".format(entity_of_reply),
                     parse_mode=ParseMode.MARKDOWN,
                     reply_to=message.message_id)
Ejemplo n.º 6
0
    def wrapper(self, update: Update, context: CallbackContext, *args, **kwargs):
        bot = context.bot
        message = update.effective_message
        chat_id = message.chat_id
        reply_to_message = message.reply_to_message

        with _session_scope(False) as session:
            entity = self._find_entity_for_message(session, bot.id, reply_to_message)
        if entity is None:
            send_message(bot, chat_id,
                         ":exclamation: You must directly reply to an image send by this bot to use reply commands.",
                         reply_to=message.message_id)

        # otherwise call wrapped function as normal
        return func(self, update, context, entity, *args, **kwargs)
Ejemplo n.º 7
0
    def _config_command_callback(self, update: Update, context: CallbackContext):
        """
        /config command handler
        :param update: the chat update object
        :param context: telegram context
        """
        from container_app_conf.formatter.toml import TomlFormatter

        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        text = self._config.print(formatter=TomlFormatter())
        text = "```\n{}\n```".format(text)
        send_message(bot, chat_id, text, parse_mode=ParseMode.MARKDOWN, reply_to=message_id)
Ejemplo n.º 8
0
    def _reply_text_command_callback(self, update: Update, context: CallbackContext,
                                     entity_of_reply: Image or None, text: str) -> None:
        """
        /text reply command handler
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message = update.effective_message
        chat_id = update.effective_chat.id

        entity_of_reply.analyser = IMAGE_ANALYSIS_TYPE_HUMAN
        entity_of_reply.analyser_quality = 1.0
        entity_of_reply.text = text
        with _session_scope() as session:
            self._persistence.update(session, entity_of_reply)
        send_message(bot, chat_id,
                     ":wrench: Updated text for referenced image to '{}' (Hash: {})".format(entity_of_reply.text,
                                                                                            entity_of_reply.image_hash),
                     reply_to=message.message_id)
Ejemplo n.º 9
0
    def _reply_delete_command_callback(self, update: Update, context: CallbackContext,
                                       entity_of_reply: Image or None) -> None:
        """
        /text reply command handler
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message = update.effective_message
        chat_id = update.effective_chat.id
        is_edit = hasattr(message, 'edited_message') and message.edited_message is not None

        if is_edit:
            LOGGER.debug("Ignoring edited delete command")
            return

        with _session_scope() as session:
            self._persistence.delete(session, entity_of_reply)
            send_message(bot, chat_id,
                         "Deleted referenced image from persistence (Hash: {})".format(entity_of_reply.image_hash),
                         reply_to=message.message_id)
Ejemplo n.º 10
0
    def _forceanalysis_callback(self, update: Update, context: CallbackContext, image_hash: str or None) -> None:
        """
        /forceanalysis command handler (with an argument)
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message = update.effective_message
        chat_id = update.effective_chat.id

        with _session_scope() as session:
            if image_hash is not None:
                entity = self._persistence.find_by_image_hash(session, image_hash)
            elif message.reply_to_message is not None:
                reply_to_message = message.reply_to_message

                entity = self._find_entity_for_message(session, bot.id, reply_to_message)
            else:
                send_message(bot, chat_id,
                             ":exclamation: Missing image reply or image hash argument".format(image_hash),
                             reply_to=message.message_id)
                return

            if entity is None:
                send_message(bot, chat_id,
                             ":exclamation: Image entity not found".format(image_hash),
                             reply_to=message.message_id)
                return

            entity.analyser = None
            entity.analyser_quality = None
            self._persistence.update(session, entity)
            send_message(bot, chat_id,
                         ":wrench: Reset analyser data for image with hash: {})".format(entity.image_hash),
                         reply_to=message.message_id)