Exemplo n.º 1
0
def do_press_inline_button(bot, update, job_queue):
    """
    Handle any inline button pressing (considering it's not an inline query nor a link)
    :param bot:
    :param update:
    :param job_queue:
    :return:
    """
    from bot_app import Bot
    global data
    try:
        chat_id = update.callback_query.message.chat_id
        query = update.callback_query
        sender = query.from_user.id
        new_vote = False
        msg_id = query.message.message_id
        if chat_id in data.conversations:
            conversation = data.conversations[chat_id]
            if query.data in keyboards.InlineKeyboard.user_based_actions:
                if conversation.is_vote_message_stored(msg_id):
                    msg_vote = conversation.get_vote_message(message_id=msg_id)
                    user = conversation.get_single_user(msg_vote.pynder_user_id)

                    if query.data == keyboards.InlineKeyboard.MORE_PICS:

                        send_more_photos(private_chat_id=sender, group_chat_id=chat_id, bot=bot,
                                         incoming_message=update.callback_query.message, user=user)

                    elif query.data == keyboards.InlineKeyboard.INSTAGRAM:
                        send_instagram_urls(private_chat_id=sender, group_chat_id=chat_id, bot=bot,
                                            incoming_message=update.callback_query.message, user=user)

                    elif query.data == keyboards.InlineKeyboard.BIO:
                        send_bio(private_chat_id=sender, group_chat_id=chat_id, bot=bot,
                                            incoming_message=update.callback_query.message, user=user)

                else:
                    # messages.send_custom_message(bot=bot, chat_id=chat_id, message="Unknown user.")
                    bot.editMessageCaption(chat_id=chat_id,
                                           message_id=query.message.message_id,
                                           caption=query.message.caption)
            else:
                if (sender in conversation.current_votes and not conversation.current_votes[sender] == query.data) \
                        or sender not in conversation.current_votes:
                    if conversation.is_vote_message_stored(msg_id) \
                            and conversation.current_user is not None \
                            and conversation.get_vote_message(msg_id).pynder_user_id == conversation.current_user.id:
                        new_vote = True
                        # Registering vote
                        conversation.current_votes[sender] = query.data
                    elif not conversation.is_vote_message_stored(msg_id):
                        # Unknown user - Remove keyboard
                        bot.editMessageCaption(chat_id=chat_id,
                                               message_id=query.message.message_id,
                                               caption=query.message.caption)
                    elif conversation.current_user is None \
                            or conversation.get_vote_message(msg_id).pynder_user_id != conversation.current_user.id:
                        # Old user - voting not allowed - Switch keyboard
                        msg_vote = conversation.get_vote_message(message_id=msg_id)
                        user = conversation.get_single_user(msg_vote.pynder_user_id)
                        keyboard = keyboards.get_vote_finished_keyboard(conversation=conversation, user=user)
                        bot.editMessageCaption(chat_id=chat_id,
                                               message_id=query.message.message_id,
                                               reply_markup=keyboard,
                                               caption=query.message.caption)

                # Schedule end of voting session
                if not data.conversations[chat_id].is_alarm_set:
                    data.conversations[chat_id].is_alarm_set = True
                    Bot.alarm_vote(bot, chat_id, job_queue)

            # Send back updated inline keyboard
            if new_vote:
                reply_markup = keyboards.get_vote_keyboard(data.conversations[chat_id],
                                                           bot_name=bot.username)
                current_vote = len(conversation.get_votes())
                max_vote = conversation.settings.get_setting("min_votes_before_timeout")
                caption = messages.get_caption_match(conversation.current_user, current_vote, max_vote, bio=True)
                bot.editMessageCaption(chat_id=chat_id,
                                       message_id=query.message.message_id,
                                       reply_markup=reply_markup,
                                       caption=caption)
        else:
            messages.send_error(bot=bot, chat_id=chat_id, name="account_not_setup")
    # will catch when pressing same button twice # TODO fix the rotating icon
    except TelegramError as e:
        raise e