Exemplo n.º 1
0
def test_default_language_code(execute, raw_message):
    del raw_message["message"]["from"]["language_code"]

    execute(raw_message)

    chat = Chat.select().first()
    assert chat.language_code == "en"
Exemplo n.º 2
0
def test_previous_chat_messages_stood_intact(chat, execute, raw_message):
    raw_message["message"]["chat"]["id"] = 300500

    execute(raw_message)

    new_chat = Chat.select()[1]
    assert chat.messages.count() == 0
    assert new_chat.messages.count() == 1
Exemplo n.º 3
0
def test_message_added_to_new_chat(execute, raw_message):
    execute(raw_message)

    chat = Chat.select().first()
    message = Message.select().first()
    assert message.message_id == 48
    assert message.text == "/start"
    assert message.chat == chat
    assert message.date == "2017-07-14 02:40:00+00:00"
Exemplo n.º 4
0
def test_chat_created(execute, raw_message):
    execute(raw_message)

    chat = Chat.select().first()
    assert chat.chat_id == 200500
    assert chat.username == "boi"
    assert chat.first_name == "John"
    assert chat.last_name == "Doe"
    assert chat.chat_type == "private"
    assert chat.language_code == "ru"
Exemplo n.º 5
0
    def answer(self, bot, update):
        data = {}
        query = Chat.select().order_by(-Chat.id)
        for msg in query:
            try:
                data[msg.user_id][0] += msg.msg_len
                data[msg.user_id][1] += 1
            except KeyError:
                data[msg.user_id] = [msg.msg_len, 1, msg.full_name]

        by_chars = sorted(data.items(), key=lambda x: x[1][0], reverse=True)
        by_msgs = sorted(data.items(), key=lambda x: x[1][1], reverse=True)
        by_chars_avg = sorted(filter(lambda x: x[1][1] > 5, data.items()),
                              key=lambda x: (x[1][0] / x[1][1]))

        template = '<a href="tg://user?id={}">{}</a> {}'

        top_chars = [
            template.format(user[0], user[1][2], user[1][0]) + '\n'
            for user in by_chars[:10]
        ]
        top_msgs = [
            template.format(user[0], user[1][2], user[1][1]) + '\n'
            for user in by_msgs[:10]
        ]
        top_chars_avg_min = [
            template.format(user[0], user[1][2], round(
                user[1][0] / user[1][1])) + '\n' for user in by_chars_avg[:10]
        ]
        top_chars_avg_max = [
            template.format(user[0], user[1][2], round(
                user[1][0] / user[1][1])) + '\n' for user in by_chars_avg[-10:]
        ]
        top_chars_avg_max.reverse()
        total_chars = sum([i[0] for i in data.values()])

        show_list = ('<i>Вже написано {:.2%} роману Війна і Мир</i>'.format(total_chars/WarAndPeace) + '\n\n' +
                     '<b>Лідери по кількості знаків</b>\n' + '{}'
                     * len(top_chars)).format(*top_chars) + '\n' + \
                    ('<b>Лідери по кількості повідомлень</b>\n' + '{}'
                     * len(top_msgs)).format(*top_msgs) + '\n' + \
                    ('<b>Найдовші повідомлення</b> <i>Сер. довжина</i>\n' + '{}'
                     * len(top_chars_avg_max)).format(*top_chars_avg_max) + '\n' + \
                    ('<b>Найкоротші повідомлення</b> <i>Сер. довжина</i>\n' + '{}'
                     * len(top_chars_avg_min)).format(*top_chars_avg_min)

        bot.editMessageText(chat_id=update.effective_user.id,
                            message_id=update.effective_message.message_id,
                            parse_mode=ParseMode.HTML,
                            text=show_list,
                            reply_markup=self.reply_markup)
Exemplo n.º 6
0
async def message_handler(message: Message):
    mode = message.chat.type
    Chat.save_chat(message)
    user = User.get_by_message(message)
    Msg.create(text=message.text, user=user, mode=mode)

    if mode == 'private':
        for chat in Chat.select():
            sender_title = translator.translate(
                f"Unidentified {user.animal} writes:", dest='ru',
                src='en').text
            msg_text = f"*{sender_title}* \n{message.text}"

            inline_keyboard = InlineKeyboardMarkup()
            inline_button = InlineKeyboardButton(
                'Ответить в боте',
                switch_inline_query=f'Ответить {user.animal}:')
            inline_keyboard.add(inline_button)
            await bot.send_message(chat_id=chat.id,
                                   text=msg_text,
                                   parse_mode='Markdown',
                                   reply_markup=inline_keyboard)
            await sleep(1)
Exemplo n.º 7
0
def test_add_new_chat_by_id(chat, execute, raw_message):
    raw_message["message"]["chat"]["id"] = 300500

    execute(raw_message)

    assert Chat.select().count() == 2
Exemplo n.º 8
0
def test_no_chats_by_default():
    assert Chat.select().count() == 0
Exemplo n.º 9
0
 def send_msg(self, msg):
     bot = self.updater_.dispatcher.bot
     for chat in Chat.select():
         bot.send_message(chat.chat_id, text=msg)