Esempio n. 1
0
    def test_make_reply_markup_chat(self):
        bot = self.create_bot()
        user = self.create_user()
        chat = self.create_chat(buttons=['a', 'b', 'c'])
        update = self.create_update(bot=bot, user=user.tg, chat=chat.tg)

        # normal
        kb = make_reply_markup(update, bot, chat=chat,
                               anonymous=False)[1].inline_keyboard
        assert len(kb) == 2
        assert user.first_name in kb[0][0].text
        assert len(kb[1]) == 3  # buttons

        # anonymous
        kb = make_reply_markup(update, bot, chat=chat,
                               anonymous=True)[1].inline_keyboard
        assert len(kb) == 1
        assert [b.text for b in kb[0]] == ['a', 'b', 'c']

        # override buttons
        kb = make_reply_markup(update,
                               bot, ['d', 'e'],
                               chat=chat,
                               anonymous=False)[1]
        kb = kb.inline_keyboard
        assert len(kb) == 2
        assert user.first_name in kb[0][0].text
        assert [b.text for b in kb[1]] == ['d', 'e']

        # remove buttons
        kb = make_reply_markup(update, bot, [], chat=chat, anonymous=False)[1]
        kb = kb.inline_keyboard
        assert len(kb) == 1
        assert user.first_name in kb[0][0].text
Esempio n. 2
0
def handle_publishing(update: Update, context: CallbackContext):
    user: TGUser = update.effective_user

    res = update.chosen_inline_result
    inline_id = res.inline_message_id
    if not inline_id:
        logger.exception("Invalid inline query.")
        return

    try:
        msg, buttons = get_msg_and_buttons(user, res.query)
    except ValueError:
        return

    message = Message.objects.create_from_inline(
        inline_message_id=inline_id,
        from_user=User.objects.from_update(update),
        buttons=buttons,
    )
    _, reply_markup = make_reply_markup(
        update,
        context.bot,
        buttons,
        message=message,
    )
    try:
        context.bot.edit_message_reply_markup(
            reply_markup=reply_markup,
            inline_message_id=inline_id,
        )
    except BadRequest:  # message was deleted too fast (probably by the same bot in chat)
        message.delete()
Esempio n. 3
0
def handle_reaction_response(update: Update, context: CallbackContext):
    """
    Respond to user's reaction after "start" commands.
    Update post's markup.
    """
    user: TGUser = update.effective_user
    msg = update.effective_message
    reaction = msg.text or (msg.sticker and msg.sticker.emoji)

    if reaction not in UNICODE_EMOJI:
        msg.reply_text(f"Reaction should be a single emoji.")
        return

    some_message_id = redis.get_key(user.id, 'message_id')
    try:
        message = Message.objects.prefetch_related().get(id=some_message_id)
    except Message.DoesNotExist:
        logger.debug(f"Message {some_message_id} doesn't exist.")
        msg.reply_text(f"Received invalid message ID from /start command.")
        return

    mids = message.ids
    _, button = Reaction.objects.react(
        user=user,
        button_text=reaction,
        **mids,
    )
    if not button:
        msg.reply_text(f"Post already has too many reactions.")
        return
    _, reply_markup = make_reply_markup(update, context.bot, message=message)
    context.bot.edit_message_reply_markup(reply_markup=reply_markup, **mids)
    msg.reply_text(f"Reacted with {reaction}")
Esempio n. 4
0
def update_markup(update, context, message, tg_message, reply):
    _, reply_markup = make_reply_markup(update, context.bot, message=message)
    try:
        reply.edit_reply_markup(reply_markup=reply_markup)
    except BadRequest as e:
        logger.debug(f"message was not modified (chat.repost=false, toggle anonymity): {e}")
    try_delete(context.bot, update, tg_message)
Esempio n. 5
0
def handle_button_callback(update: Update, context: CallbackContext):
    msg = update.effective_message
    user = update.effective_user
    query = update.callback_query
    text = context.match[1]

    mids = dict(
        chat_id=msg and msg.chat and msg.chat_id,
        message_id=msg and msg.message_id,
        inline_message_id=query.inline_message_id,
    )
    try:
        message = Message.objects.prefetch_related().get_by_ids(**mids)
    except Message.DoesNotExist:
        logger.debug(f"Message {mids} doesn't exist.")
        return

    reaction, button = Reaction.objects.react(
        user=user,
        button_text=text,
        **mids,
    )
    reply_to_reaction(context.bot, query, button, reaction)
    _, reply_markup = make_reply_markup(update, context.bot, message=message)
    try:
        context.bot.edit_message_reply_markup(reply_markup=reply_markup,
                                              **mids)
    except TimedOut:
        logger.debug("timeout")
    except BadRequest as e:
        logger.debug(f"😡 {e}")
Esempio n. 6
0
    def test_make_reply_markup_message_from_chat(self):
        bot = self.create_bot()
        user = self.create_user()
        user2 = self.create_user(first_name='user2',
                                 username='******')  # make user linkable
        chat = self.create_chat(buttons=['1', '2', '3'])

        # normal
        message = self.create_message(buttons=['a', 'b', 'c'],
                                      chat=chat,
                                      from_user=user)
        update = self.create_update(bot=bot, message=message.tg)
        chat, kb = make_reply_markup(update, bot, message=message)
        kb = kb.inline_keyboard
        assert len(kb) == 2
        assert user.first_name in kb[0][0].text
        assert [b.text for b in kb[1]] == ['a', 'b', 'c']

        # anonymous
        message = self.create_message(
            buttons=['a', 'b', 'c'],
            chat=chat,
            anonymous=True,
            from_user=user,
        )
        update = self.create_update(bot=bot, message=message.tg)
        kb = make_reply_markup(update, bot, message=message)[1].inline_keyboard
        assert len(kb) == 1
        assert [b.text for b in kb[0]] == ['a', 'b', 'c']

        # forward
        message = self.create_message(
            buttons=['a', 'b', 'c'],
            chat=chat,
            anonymous=False,
            from_user=user,
            forward_from=user2,
        )
        update = self.create_update(bot=bot, message=message.tg)
        kb = make_reply_markup(update, bot, message=message)[1].inline_keyboard
        assert len(kb) == 2
        assert len(kb[0]) == 2
        assert [b.text for b in kb[1]] == ['a', 'b', 'c']
Esempio n. 7
0
 def make_msg_kb(self, bot, update, buttons):
     inline_id = update.chosen_inline_result.inline_message_id
     message = Message.objects.create_from_inline(
         inline_message_id=inline_id,
         from_user=User.objects.from_update(update),
         buttons=buttons,
     )
     kb = make_reply_markup(update, bot, buttons,
                            message=message)[1].inline_keyboard
     return kb
Esempio n. 8
0
def process_message(
    update: Update,
    context: CallbackContext,
    msg_type: str,
    chat: Chat,
    anonymous: bool,
    buttons=None,
    repost=False,
):
    msg: TGMessage = update.effective_message
    bot = context.bot

    chat, reply_markup = make_reply_markup(update, bot, buttons, chat=chat, anonymous=anonymous)

    should_repost = (chat.repost or repost) and msg_type != 'album'

    if should_repost:
        sent_msg = repost_message(msg, bot, msg_type, reply_markup)
    else:
        sent_msg = msg.reply_text(
            text='^',
            disable_notification=True,
            reply_markup=reply_markup,
        )

    logger.debug(f"sent_msg: {sent_msg}")
    if sent_msg:
        if should_repost:
            try_delete(bot, update, msg)
        Message.objects.create_from_tg_ids(
            sent_msg.chat_id,
            sent_msg.message_id,
            date=timezone.make_aware(msg.date),
            buttons=buttons,
            anonymous=anonymous,
            original_message_id=msg.message_id,
            from_user=User.objects.from_update(update),
            forward_from=get_forward_from(msg),
            forward_from_chat=get_forward_from_chat(msg),
            forward_from_message_id=msg.forward_from_message_id,
        )