示例#1
0
def route_update():
    update_json = flask.request.get_json()
    update = telegram.Update.de_json(update_json, application.bot)
    application.update_queue.put(update)
    logging.get_logger(logging.Type.APP).info("Received update " +
                                              str(update.update_id))
    return ""
def add_sticker(app, bot, update, conversation):
    logger = logging.get_logger(logging.Type.DATABASE_ADD_STICKER,
                               update.update_id)

    user = update.effective_user
    chat_id = update.effective_chat.id
    sticker = conversation.sticker

    with app.app_context():
        try:
            database_user = models.User.get(user.id)
            database_sticker = models.Sticker.get_or_create(sticker)

            for label in conversation.labels:
                database_label = models.Label.get_or_create(label)

                # TODO Work with existing labels
                association = models.Association(
                    database_user, database_sticker, database_label)

            models.database.session.commit()
        except Exception as e:
            response = message.Message(bot, update, logger, chat_id)
            response_content = message.Text.Error.UNKNOWN
            response.set_content(response_content).send()

            models.database.session.rollback()
            conversation.rollback_state()
            return False

    return True
def handle_callback_for_labels(app, bot, update):
    logger = logging.get_logger(logging.Type.HANDLER_CALLBACK_QUERY_LABELS,
                               update.update_id)
    logger.log_start()

    callback_data = CallbackData.unwrap(update.callback_query.data)
    chat_id = update.effective_chat.id
    conversation = conversations.get_or_create(
        update.effective_user, get_only=True)

    response = message.Message(bot, update, logger, chat_id)

    if not conversation:
        logger.log_conversation_not_found(update.effective_user.id)
        response_content = message.Text.Error.UNKNOWN
        response.set_content(response_content).send()
        return

    if callback_data.button_text == CallbackData.ButtonText.CONFIRM:
        logger.debug("Button – confirm")
        try:
            conversation.change_state(
                conversations.Conversation.State.CONFIRMED)
        except ValueError as e:
            state, = e.args
            logger.log_failed_to_change_conversation_state(
                state, conversations.Conversation.State.CONFIRMED)

            response_content = message.Text.Error.UNKNOWN
            response.set_content(response_content).send()
            return

        added = add_sticker(app, bot, update, conversation)
        if not added:
            logger.debug("Failed to add sticker")
            response_content = message.Text.Error.UNKNOWN
            response.set_content(response_content).send()
            conversation.rollback_state()
            return

        logger.debug("Added sticker successfully")
        response_content = message.Text.Other.SUCCESS
        response.set_content(response_content).send()

        try:
            conversation.change_state(conversations.Conversation.State.IDLE)
        except ValueError as e:
            state, = e.args
            logger.log_failed_to_change_conversation_state(
                state, conversations.Conversation.State.IDLE)

            return

    elif callback_data.button_text == CallbackData.ButtonText.CANCEL:
        # TODO Rollback state after pending conversation state is introduced
        conversation.labels = None
        response_content = message.Text.Instruction.RE_LABEL
        response.set_content(response_content).send()
示例#4
0
    def help_handler(bot, update):
        logger = logging.get_logger(logging.Type.HANDLER_HELP,
                                    update.update_id)
        logger.log_start()

        chat_id = update.effective_chat.id

        response = message.Message(bot, update, logger, chat_id)
        response_content = message.Text.Instruction.HELP
        response.set_content(response_content).send()
示例#5
0
    def sticker_handler(bot, update):
        logger = logging.get_logger(logging.Type.HANDLER_STICKER,
                                   update.update_id)
        logger.log_start()

        user = update.effective_user
        sticker = update.effective_message.sticker
        chat_id = update.effective_chat.id

        response = message.Message(bot, update, logger, chat_id)

        conversation = conversations.get_or_create(user,
                                                   chat=update.effective_chat)

        try:
            conversation.change_state(
                conversations.Conversation.State.STICKER,
                pool.submit(sticker_is_new, app, user, sticker), force=True)
        except ValueError as e:
            # TODO Ask user if they want to cancel previous conversation
            # Currently should not enter this branch
            state, = e.args
            logger.log_failed_to_change_conversation_state(
                state, conversations.Conversation.State.STICKER)

            response_content = message.Text.Error.RESTART
            response.set_content(response_content).send()
            return

        logger.debug("Entered STICKER state")
        new_sticker = conversation.get_future_result()

        if new_sticker:
            response_content = message.Text.Instruction.LABEL
            response.set_content(response_content).send()
            conversation.sticker = sticker
        elif new_sticker is False:
            logger.debug("Sticker exists")
            response_content = message.Text.Error.STICKER_EXISTS
            response.set_content(response_content).send()
            # TODO Ask user if they meant to change the sticker's labels
            conversation.rollback_state()
        else:
            logger.debug("Future timed out")
            response_content = message.Text.Error.UNKNOWN
            response.set_content(response_content).send()
            conversation.rollback_state()
    def chosen_inline_result_handler(bot, update):
        logger = logging.get_logger(logging.Type.HANDLER_CHOSEN_INLINE_RESULT,
                                   update.update_id)
        logger.log_start()

        user = update.effective_user
        chosen_inline_result = update.chosen_inline_result
        sticker_id = StickerResult.unwrap(chosen_inline_result.result_id)
        labels = chosen_inline_result.query.split()

        logger.debug("Query: " + chosen_inline_result.query)
        logger.debug("Labels: " + str(labels))

        models.Association.increment_usage(user.id, sticker_id, labels)

        logger.debug("Incremented usage for user " + str(user.id) +
                     "'s sticker " + str(sticker_id))
示例#7
0
    def callback_handler(bot, update):
        logger = logging.get_logger(logging.Type.HANDLER_CALLBACK_QUERY,
                                    update.update_id)
        logger.log_start()

        callback_data = CallbackData.unwrap(update.callback_query.data)
        user = update.effective_user
        chat_id = update.effective_chat.id

        response = message.Message(bot, update, logger, chat_id)

        conversation = conversations.get_or_create(user, get_only=True)
        if not conversation:
            logger.log_conversation_not_found(user.id)
            response_content = message.Text.Error.UNKNOWN
            response.set_content(response_content).send()
            return

        if callback_data.state == conversations.Conversation.State.LABEL:
            labels_callback.handle_callback_for_labels(app, bot, update)
示例#8
0
    def command_start_handler(bot, update):
        logger = logging.get_logger(logging.Type.HANDLER_START,
                                    update.update_id)
        logger.log_start()

        chat_id = update.effective_chat.id
        user_id = update.effective_user.id

        response = message.Message(bot, update, logger, chat_id)

        with app.app_context():
            user = models.User.get(user_id)
            if not user:
                logger.debug("User " + str(user_id) + " not found")
                user = models.User.from_telegram_user(update.effective_user,
                                                      chat_id)
                models.database.session.commit()
                logger.debug("Created user " + str(user_id))

        response_content = message.Text.Instruction.START
        response.set_content(response_content).send()
示例#9
0
    def labels_handler(bot, update):
        logger = logging.get_logger(logging.Type.HANDLER_LABELS,
                                    update.update_id)
        logger.log_start()

        user = update.effective_user
        chat_id = update.effective_chat.id

        response = message.Message(bot, update, logger, chat_id)

        conversation = conversations.get_or_create(user, get_only=True)
        if not conversation:
            logger.log_conversation_not_found(user.id)
            response_content = message.Text.Error.NOT_STARTED
            response.set_content(response_content).send()
            return

        try:
            conversation.change_state(conversations.Conversation.State.LABEL)
        except ValueError as e:
            state, = e.args
            logger.log_failed_to_change_conversation_state(
                state, conversations.Conversation.State.LABEL)
            response_content = message.Text.Error.RESTART
            response.set_content(response_content).send()
            return

        sticker = conversation.sticker

        new_labels = get_labels(update)
        if not new_labels:
            logger.debug("No new labels found. Message: " +
                         update.message.text)

            response_content = message.Text.Error.LABEL_MISSING
            response.set_content(response_content).send()

            conversation.rollback_state()
            return

        conversation.labels = new_labels
        # TODO Include existing labels
        if len(new_labels) > 1:
            message_labels = "\n".join(new_labels)
        else:
            message_labels = new_labels[0]

        message_text = message.Text.Instruction.CONFIRM.value + message_labels

        buttons = [[
            CallbackData.ButtonText.CONFIRM, CallbackData.ButtonText.CANCEL
        ]]
        generator = CallbackData(conversations.Conversation.State.LABEL,
                                 sticker.file_id).generator
        inline_keyboard_markup = generate_inline_keyboard_markup(
            generator, buttons)  # TODO Add re-label

        response_content = sticker
        response.set_content(response_content).send()

        response_confirmation = message.Message(bot,
                                                update,
                                                logger,
                                                chat_id,
                                                content=message_text)
        response_confirmation.send(reply_markup=inline_keyboard_markup)
        logger.debug("Sent sticker with confirmation message")