コード例 #1
0
 def test_make_reactions_keyboard_padding(self):
     kb = make_reactions_keyboard(list('abcdef'), max_cols=5,
                                  padding=True).inline_keyboard
     assert len(kb) == 2
     assert [b.text for b in kb[0]] == ['a', 'b', 'c', 'd', 'e']
     assert [b.text for b in kb[1]] == ['f', '.', '.', '.', '.']
     assert [b.callback_data for b in kb[1][1:]] == [EMPTY_CB_DATA] * 4
コード例 #2
0
    def test_make_reactions_keyboard(self):
        kb = make_reactions_keyboard(['a', 'b']).inline_keyboard
        assert len(kb) == 1
        assert len(kb[0]) == 2

        kb = make_reactions_keyboard(list('abcdef'),
                                     max_cols=5).inline_keyboard
        assert len(kb) == 2
        assert [b.text for b in kb[0]] == ['a', 'b', 'c', 'd', 'e']
        assert [b.text for b in kb[1]] == ['f']

        kb = make_reactions_keyboard(list('abcdef'),
                                     max_cols=3).inline_keyboard
        assert len(kb) == 2
        assert [b.text for b in kb[0]] == ['a', 'b', 'c']
        assert [b.text for b in kb[1]] == ['d', 'e', 'f']
コード例 #3
0
def handle_create_buttons(update: Update, context: CallbackContext):
    """
    Save picked buttons to message-to-publish object.
    Send publishable post.
    """
    user: TGUser = update.effective_user
    msg: TGMessage = update.effective_message

    if msg.text == 'none':
        buttons = []
    else:
        buttons = clear_buttons(msg.text.split(), emojis=True)
        if not buttons:
            msg.reply_text("Buttons should be emojis.")
            return
        UserButtons.create(user.id, buttons)

    mtp = MessageToPublish.last(user.id)
    mtp.buttons = buttons
    mtp.save()

    msg.reply_text("Press 'publish' and choose chat/channel.")
    message = mtp.message_tg
    msg_type = get_message_type(message)
    reply_markup = make_reactions_keyboard(buttons, blank=True)
    update.effective_chat.send_action(ChatAction.TYPING)
    reply_markup.inline_keyboard.append([
        InlineKeyboardButton(
            "publish",
            switch_inline_query=str(mtp.id),
        )
    ])
    repost_message(message, context.bot, msg_type, reply_markup)
    redis.set_state(user, State.create_end)
コード例 #4
0
    def test_merge_keyboards_vertical_limit(self):
        kb = merge_keyboards(
            make_credits_keyboard(**self.user),
            make_reactions_keyboard(['1', '2']),
        )
        markup = merge_keyboards(kb, kb, kb, kb, kb).inline_keyboard
        assert len(markup) == 10

        markup = merge_keyboards(kb, kb, kb, kb, kb, kb).inline_keyboard
        assert len(markup) == 10
コード例 #5
0
def handle_publishing_options(update: Update, _: CallbackContext):
    user: TGUser = update.effective_user

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

    reply_markup = make_reactions_keyboard(buttons or ['-'])
    msg_type = get_message_type(msg)
    config = {
        'id': str(uuid4()),
        'title': msg.text_markdown or msg.caption_markdown
        or "Message to publish.",
        'text': msg.text_markdown,
        'caption': msg.caption_markdown,
        'parse_mode': ParseMode.MARKDOWN,
        'reply_markup': reply_markup,
        # types
        'photo_file_id': msg.photo and msg.photo[0].file_id,
        'video_file_id': msg.video and msg.video.file_id,
        'mpeg4_file_id': msg.animation and msg.animation.file_id,
    }
    if msg_type == 'photo':
        qr = InlineQueryResultCachedPhoto(**config)
    elif msg_type == 'video':
        qr = InlineQueryResultCachedVideo(**config)
    elif msg_type == 'animation':
        qr = InlineQueryResultCachedMpeg4Gif(**config)
    elif msg_type in ('text', 'link'):
        qr = InlineQueryResultArticle(
            input_message_content=InputTextMessageContent(
                msg.text_markdown,
                parse_mode=ParseMode.MARKDOWN,
            ),
            **config,
        )
    else:
        return
    update.inline_query.answer([qr], cache_time=0, is_personal=True)