From bd8e4fbb41bcab89c508a6211ec2d6da465388f2 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 15:45:29 +0300 Subject: [PATCH 01/19] #289 Added sendDice method --- aiogram/bot/api.py | 1 + aiogram/bot/bot.py | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index a6dbd0ea..8e2538a6 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -181,6 +181,7 @@ class Methods(Helper): SEND_VENUE = Item() # sendVenue SEND_CONTACT = Item() # sendContact SEND_POLL = Item() # sendPoll + SEND_DICE = Item() # sendDice SEND_CHAT_ACTION = Item() # sendChatAction GET_USER_PROFILE_PHOTOS = Item() # getUserProfilePhotos GET_FILE = Item() # getFile diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index 39313558..914fbd82 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -914,6 +914,41 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.SEND_POLL, payload) return types.Message(**result) + async def send_dice(self, chat_id: typing.Union[base.Integer, base.String], + disable_notification: typing.Union[base.Boolean, None] = None, + reply_to_message_id: typing.Union[base.Integer, None] = None, + reply_markup: typing.Union[types.InlineKeyboardMarkup, + types.ReplyKeyboardMarkup, + types.ReplyKeyboardRemove, + types.ForceReply, None] = None) -> types.Message: + """ + Use this method to send a dice, which will have a random value from 1 to 6. + On success, the sent Message is returned. + (Yes, we're aware of the “proper” singular of die. + But it's awkward, and we decided to help it change. One dice at a time!) + + Source: https://core.telegram.org/bots/api#senddice + + :param chat_id: Unique identifier for the target chat or username of the target channel + :type chat_id: :obj:`typing.Union[base.Integer, base.String]` + :param disable_notification: Sends the message silently. Users will receive a notification with no sound + :type disable_notification: :obj:`typing.Union[base.Boolean, None]` + :param reply_to_message_id: If the message is a reply, ID of the original message + :type reply_to_message_id: :obj:`typing.Union[base.Integer, None]` + :param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard, + custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user + :type reply_markup: :obj:`typing.Union[types.InlineKeyboardMarkup, + types.ReplyKeyboardMarkup, types.ReplyKeyboardRemove, types.ForceReply, None]` + :return: On success, the sent Message is returned + :rtype: :obj:`types.Message` + """ + + reply_markup = prepare_arg(reply_markup) + payload = generate_payload(**locals()) + + result = await self.request(api.Methods.SEND_DICE, payload) + return types.Message(**result) + async def send_chat_action(self, chat_id: typing.Union[base.Integer, base.String], action: base.String) -> base.Boolean: """ @@ -1134,8 +1169,9 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.PROMOTE_CHAT_MEMBER, payload) return result - - async def set_chat_administrator_custom_title(self, chat_id: typing.Union[base.Integer, base.String], user_id: base.Integer, custom_title: base.String) -> base.Boolean: + + async def set_chat_administrator_custom_title(self, chat_id: typing.Union[base.Integer, base.String], + user_id: base.Integer, custom_title: base.String) -> base.Boolean: """ Use this method to set a custom title for an administrator in a supergroup promoted by the bot. From 13462abe473858d332d612b682879aea143a53ec Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 15:49:18 +0300 Subject: [PATCH 02/19] test_bot minor refactoring --- tests/test_bot.py | 76 +++-------------------------------------------- 1 file changed, 4 insertions(+), 72 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 3e48ea57..6b7a04dd 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1,48 +1,19 @@ -import aresponses import pytest from aiogram import Bot, types +from . import FakeTelegram, TOKEN -TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' +pytestmark = pytest.mark.asyncio -class FakeTelegram(aresponses.ResponsesMockServer): - def __init__(self, message_dict, **kwargs): - super().__init__(**kwargs) - self._body, self._headers = self.parse_data(message_dict) - - async def __aenter__(self): - await super().__aenter__() - _response = self.Response(text=self._body, headers=self._headers, status=200, reason='OK') - self.add(self.ANY, response=_response) - - @staticmethod - def parse_data(message_dict): - import json - - _body = '{"ok":true,"result":' + json.dumps(message_dict) + '}' - _headers = {'Server': 'nginx/1.12.2', - 'Date': 'Tue, 03 Apr 2018 16:59:54 GMT', - 'Content-Type': 'application/json', - 'Content-Length': str(len(_body)), - 'Connection': 'keep-alive', - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', - 'Access-Control-Expose-Headers': 'Content-Length,Content-Type,Date,Server,Connection', - 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains'} - return _body, _headers - - -@pytest.yield_fixture() -@pytest.mark.asyncio -async def bot(event_loop): +@pytest.yield_fixture(name='bot') +async def bot_fixture(event_loop): """ Bot fixture """ _bot = Bot(TOKEN, loop=event_loop, parse_mode=types.ParseMode.MARKDOWN) yield _bot await _bot.close() -@pytest.mark.asyncio async def test_get_me(bot: Bot, event_loop): """ getMe method test """ from .types.dataset import USER @@ -53,7 +24,6 @@ async def test_get_me(bot: Bot, event_loop): assert result == user -@pytest.mark.asyncio async def test_send_message(bot: Bot, event_loop): """ sendMessage method test """ from .types.dataset import MESSAGE @@ -64,7 +34,6 @@ async def test_send_message(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_forward_message(bot: Bot, event_loop): """ forwardMessage method test """ from .types.dataset import FORWARDED_MESSAGE @@ -76,7 +45,6 @@ async def test_forward_message(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_photo(bot: Bot, event_loop): """ sendPhoto method test with file_id """ from .types.dataset import MESSAGE_WITH_PHOTO, PHOTO @@ -89,7 +57,6 @@ async def test_send_photo(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_audio(bot: Bot, event_loop): """ sendAudio method test with file_id """ from .types.dataset import MESSAGE_WITH_AUDIO @@ -102,7 +69,6 @@ async def test_send_audio(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_document(bot: Bot, event_loop): """ sendDocument method test with file_id """ from .types.dataset import MESSAGE_WITH_DOCUMENT @@ -114,7 +80,6 @@ async def test_send_document(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_video(bot: Bot, event_loop): """ sendVideo method test with file_id """ from .types.dataset import MESSAGE_WITH_VIDEO, VIDEO @@ -129,7 +94,6 @@ async def test_send_video(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_voice(bot: Bot, event_loop): """ sendVoice method test with file_id """ from .types.dataset import MESSAGE_WITH_VOICE, VOICE @@ -143,7 +107,6 @@ async def test_send_voice(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_video_note(bot: Bot, event_loop): """ sendVideoNote method test with file_id """ from .types.dataset import MESSAGE_WITH_VIDEO_NOTE, VIDEO_NOTE @@ -157,7 +120,6 @@ async def test_send_video_note(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_media_group(bot: Bot, event_loop): """ sendMediaGroup method test with file_id """ from .types.dataset import MESSAGE_WITH_MEDIA_GROUP, PHOTO @@ -171,7 +133,6 @@ async def test_send_media_group(bot: Bot, event_loop): assert result.pop().media_group_id -@pytest.mark.asyncio async def test_send_location(bot: Bot, event_loop): """ sendLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION @@ -184,7 +145,6 @@ async def test_send_location(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_edit_message_live_location_by_bot(bot: Bot, event_loop): """ editMessageLiveLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION @@ -198,7 +158,6 @@ async def test_edit_message_live_location_by_bot(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_edit_message_live_location_by_user(bot: Bot, event_loop): """ editMessageLiveLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION, LOCATION @@ -212,7 +171,6 @@ async def test_edit_message_live_location_by_user(bot: Bot, event_loop): assert isinstance(result, bool) and result is True -@pytest.mark.asyncio async def test_stop_message_live_location_by_bot(bot: Bot, event_loop): """ stopMessageLiveLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION @@ -224,7 +182,6 @@ async def test_stop_message_live_location_by_bot(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_stop_message_live_location_by_user(bot: Bot, event_loop): """ stopMessageLiveLocation method test """ from .types.dataset import MESSAGE_WITH_LOCATION @@ -237,7 +194,6 @@ async def test_stop_message_live_location_by_user(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_send_venue(bot: Bot, event_loop): """ sendVenue method test """ from .types.dataset import MESSAGE_WITH_VENUE, VENUE, LOCATION @@ -252,7 +208,6 @@ async def test_send_venue(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_contact(bot: Bot, event_loop): """ sendContact method test """ from .types.dataset import MESSAGE_WITH_CONTACT, CONTACT @@ -265,7 +220,6 @@ async def test_send_contact(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_send_chat_action(bot: Bot, event_loop): """ sendChatAction method test """ from .types.dataset import CHAT @@ -277,7 +231,6 @@ async def test_send_chat_action(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_get_user_profile_photo(bot: Bot, event_loop): """ getUserProfilePhotos method test """ from .types.dataset import USER_PROFILE_PHOTOS, USER @@ -288,7 +241,6 @@ async def test_get_user_profile_photo(bot: Bot, event_loop): assert isinstance(result, types.UserProfilePhotos) -@pytest.mark.asyncio async def test_get_file(bot: Bot, event_loop): """ getFile method test """ from .types.dataset import FILE @@ -299,7 +251,6 @@ async def test_get_file(bot: Bot, event_loop): assert isinstance(result, types.File) -@pytest.mark.asyncio async def test_kick_chat_member(bot: Bot, event_loop): """ kickChatMember method test """ from .types.dataset import USER, CHAT @@ -312,7 +263,6 @@ async def test_kick_chat_member(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_unban_chat_member(bot: Bot, event_loop): """ unbanChatMember method test """ from .types.dataset import USER, CHAT @@ -325,7 +275,6 @@ async def test_unban_chat_member(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_restrict_chat_member(bot: Bot, event_loop): """ restrictChatMember method test """ from .types.dataset import USER, CHAT @@ -346,7 +295,6 @@ async def test_restrict_chat_member(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_promote_chat_member(bot: Bot, event_loop): """ promoteChatMember method test """ from .types.dataset import USER, CHAT @@ -362,7 +310,6 @@ async def test_promote_chat_member(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_export_chat_invite_link(bot: Bot, event_loop): """ exportChatInviteLink method test """ from .types.dataset import CHAT, INVITE_LINK @@ -373,7 +320,6 @@ async def test_export_chat_invite_link(bot: Bot, event_loop): assert result == INVITE_LINK -@pytest.mark.asyncio async def test_delete_chat_photo(bot: Bot, event_loop): """ deleteChatPhoto method test """ from .types.dataset import CHAT @@ -385,7 +331,6 @@ async def test_delete_chat_photo(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_set_chat_title(bot: Bot, event_loop): """ setChatTitle method test """ from .types.dataset import CHAT @@ -397,7 +342,6 @@ async def test_set_chat_title(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_set_chat_description(bot: Bot, event_loop): """ setChatDescription method test """ from .types.dataset import CHAT @@ -409,7 +353,6 @@ async def test_set_chat_description(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_pin_chat_message(bot: Bot, event_loop): """ pinChatMessage method test """ from .types.dataset import MESSAGE @@ -422,7 +365,6 @@ async def test_pin_chat_message(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_unpin_chat_message(bot: Bot, event_loop): """ unpinChatMessage method test """ from .types.dataset import CHAT @@ -434,7 +376,6 @@ async def test_unpin_chat_message(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_leave_chat(bot: Bot, event_loop): """ leaveChat method test """ from .types.dataset import CHAT @@ -446,7 +387,6 @@ async def test_leave_chat(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_get_chat(bot: Bot, event_loop): """ getChat method test """ from .types.dataset import CHAT @@ -457,7 +397,6 @@ async def test_get_chat(bot: Bot, event_loop): assert result == chat -@pytest.mark.asyncio async def test_get_chat_administrators(bot: Bot, event_loop): """ getChatAdministrators method test """ from .types.dataset import CHAT, CHAT_MEMBER @@ -470,7 +409,6 @@ async def test_get_chat_administrators(bot: Bot, event_loop): assert len(result) == 2 -@pytest.mark.asyncio async def test_get_chat_members_count(bot: Bot, event_loop): """ getChatMembersCount method test """ from .types.dataset import CHAT @@ -482,7 +420,6 @@ async def test_get_chat_members_count(bot: Bot, event_loop): assert result == count -@pytest.mark.asyncio async def test_get_chat_member(bot: Bot, event_loop): """ getChatMember method test """ from .types.dataset import CHAT, CHAT_MEMBER @@ -495,7 +432,6 @@ async def test_get_chat_member(bot: Bot, event_loop): assert result == member -@pytest.mark.asyncio async def test_set_chat_sticker_set(bot: Bot, event_loop): """ setChatStickerSet method test """ from .types.dataset import CHAT @@ -507,7 +443,6 @@ async def test_set_chat_sticker_set(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_delete_chat_sticker_set(bot: Bot, event_loop): """ setChatStickerSet method test """ from .types.dataset import CHAT @@ -519,7 +454,6 @@ async def test_delete_chat_sticker_set(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_answer_callback_query(bot: Bot, event_loop): """ answerCallbackQuery method test """ @@ -529,7 +463,6 @@ async def test_answer_callback_query(bot: Bot, event_loop): assert result is True -@pytest.mark.asyncio async def test_edit_message_text_by_bot(bot: Bot, event_loop): """ editMessageText method test """ from .types.dataset import EDITED_MESSAGE @@ -541,7 +474,6 @@ async def test_edit_message_text_by_bot(bot: Bot, event_loop): assert result == msg -@pytest.mark.asyncio async def test_edit_message_text_by_user(bot: Bot, event_loop): """ editMessageText method test """ from .types.dataset import EDITED_MESSAGE From 44221872eb52e81869966c9a904013986e30e325 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 16:07:01 +0300 Subject: [PATCH 03/19] #289 added Dice type and ContentType --- aiogram/types/__init__.py | 1 + aiogram/types/dice.py | 13 +++++++++++++ aiogram/types/message.py | 14 ++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 aiogram/types/dice.py diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index 35461bb9..7d201ede 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -11,6 +11,7 @@ from .chat_permissions import ChatPermissions from .chat_photo import ChatPhoto from .chosen_inline_result import ChosenInlineResult from .contact import Contact +from .dice import Dice from .document import Document from .encrypted_credentials import EncryptedCredentials from .encrypted_passport_element import EncryptedPassportElement diff --git a/aiogram/types/dice.py b/aiogram/types/dice.py new file mode 100644 index 00000000..9d0e22d0 --- /dev/null +++ b/aiogram/types/dice.py @@ -0,0 +1,13 @@ +from . import base, fields + + +class Dice(base.TelegramObject): + """ + This object represents a dice with random value from 1 to 6. + (Yes, we're aware of the “proper” singular of die. + But it's awkward, and we decided to help it change. One dice at a time!) + + https://core.telegram.org/bots/api#dice + """ + + value: base.Integer = fields.Field() diff --git a/aiogram/types/message.py b/aiogram/types/message.py index badf1d51..d50eaf28 100644 --- a/aiogram/types/message.py +++ b/aiogram/types/message.py @@ -10,6 +10,7 @@ from .animation import Animation from .audio import Audio from .chat import Chat, ChatType from .contact import Contact +from .dice import Dice from .document import Document from .force_reply import ForceReply from .game import Game @@ -70,6 +71,8 @@ class Message(base.TelegramObject): contact: Contact = fields.Field(base=Contact) location: Location = fields.Field(base=Location) venue: Venue = fields.Field(base=Venue) + poll: Poll = fields.Field(base=Poll) + dice: Dice = fields.Field(base=Dice) new_chat_members: typing.List[User] = fields.ListField(base=User) left_chat_member: User = fields.Field(base=User) new_chat_title: base.String = fields.Field() @@ -85,7 +88,6 @@ class Message(base.TelegramObject): successful_payment: SuccessfulPayment = fields.Field(base=SuccessfulPayment) connected_website: base.String = fields.Field() passport_data: PassportData = fields.Field(base=PassportData) - poll: Poll = fields.Field(base=Poll) reply_markup: InlineKeyboardMarkup = fields.Field(base=InlineKeyboardMarkup) @property @@ -117,6 +119,10 @@ class Message(base.TelegramObject): return ContentType.VENUE if self.location: return ContentType.LOCATION + if self.poll: + return ContentType.POLL + if self.dice: + return ContentType.DICE if self.new_chat_members: return ContentType.NEW_CHAT_MEMBERS if self.left_chat_member: @@ -143,8 +149,7 @@ class Message(base.TelegramObject): return ContentType.GROUP_CHAT_CREATED if self.passport_data: return ContentType.PASSPORT_DATA - if self.poll: - return ContentType.POLL + return ContentType.UNKNOWN @@ -1685,6 +1690,8 @@ class ContentType(helper.Helper): CONTACT = helper.Item() # contact LOCATION = helper.Item() # location VENUE = helper.Item() # venue + POLL = helper.Item() # poll + DICE = helper.Item() # dice NEW_CHAT_MEMBERS = helper.Item() # new_chat_member LEFT_CHAT_MEMBER = helper.Item() # left_chat_member INVOICE = helper.Item() # invoice @@ -1698,7 +1705,6 @@ class ContentType(helper.Helper): DELETE_CHAT_PHOTO = helper.Item() # delete_chat_photo GROUP_CHAT_CREATED = helper.Item() # group_chat_created PASSPORT_DATA = helper.Item() # passport_data - POLL = helper.Item() UNKNOWN = helper.Item() # unknown ANY = helper.Item() # any From 0f245bbf56863ba79a34aca7c95539c60df18f78 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 16:14:26 +0300 Subject: [PATCH 04/19] #289 added sendDice test and dataset --- tests/test_bot.py | 10 ++++++++++ tests/types/dataset.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 6b7a04dd..0fa23df4 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -220,6 +220,16 @@ async def test_send_contact(bot: Bot, event_loop): assert result == msg +async def test_send_dice(bot: Bot, event_loop): + """ sendDice method test """ + from .types.dataset import MESSAGE_WITH_DICE + msg = types.Message(**MESSAGE_WITH_DICE) + + async with FakeTelegram(message_dict=MESSAGE_WITH_DICE, loop=event_loop): + result = await bot.send_dice(msg.chat.id, disable_notification=False) + assert result == msg + + async def test_send_chat_action(bot: Bot, event_loop): """ sendChatAction method test """ from .types.dataset import CHAT diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 18bcbdad..8950344c 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -53,6 +53,10 @@ CONTACT = { "last_name": "Smith", } +DICE = { + "value": 6 +} + DOCUMENT = { "file_name": "test.docx", "mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -255,6 +259,14 @@ MESSAGE_WITH_CONTACT = { MESSAGE_WITH_DELETE_CHAT_PHOTO = {} +MESSAGE_WITH_DICE = { + "message_id": 12345, + "from": USER, + "chat": CHAT, + "date": 1508768012, + "dice": DICE +} + MESSAGE_WITH_DOCUMENT = { "message_id": 12345, "from": USER, From b77ed1ad92ff543f34e4a8a4b2211691e8f26e48 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 16:35:56 +0300 Subject: [PATCH 05/19] #289 added setMyCommands method; added BotCommand type --- aiogram/bot/api.py | 1 + aiogram/bot/bot.py | 18 ++++++++++++++++++ aiogram/types/__init__.py | 1 + aiogram/types/bot_command.py | 12 ++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 aiogram/types/bot_command.py diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index 8e2538a6..6f3dc951 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -206,6 +206,7 @@ class Methods(Helper): SET_CHAT_STICKER_SET = Item() # setChatStickerSet DELETE_CHAT_STICKER_SET = Item() # deleteChatStickerSet ANSWER_CALLBACK_QUERY = Item() # answerCallbackQuery + SET_MY_COMMANDS = Item() # setMyCommands # Updating messages EDIT_MESSAGE_TEXT = Item() # editMessageText diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index 914fbd82..1b1e8615 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1520,6 +1520,24 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.ANSWER_CALLBACK_QUERY, payload) return result + async def set_my_commands(self, commands: typing.List[types.BotCommand]) -> base.Boolean: + """ + Use this method to change the list of the bot's commands. + + Source: https://core.telegram.org/bots/api#setmycommands + + :param commands: A JSON-serialized list of bot commands to be set as the list of the bot's commands. + At most 100 commands can be specified. + :type commands: :obj: `typing.List[types.BotCommand]` + :return: Returns True on success. + :rtype: base.Boolean + """ + commands = prepare_arg(commands) + payload = generate_payload(**locals()) + + result = await self.request(api.Methods.SET_MY_COMMANDS, payload) + return result + async def edit_message_text(self, text: base.String, chat_id: typing.Union[base.Integer, base.String, None] = None, message_id: typing.Union[base.Integer, None] = None, diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index 7d201ede..693bd7d5 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -3,6 +3,7 @@ from . import fields from .animation import Animation from .audio import Audio from .auth_widget_data import AuthWidgetData +from .bot_command import BotCommand from .callback_game import CallbackGame from .callback_query import CallbackQuery from .chat import Chat, ChatActions, ChatType diff --git a/aiogram/types/bot_command.py b/aiogram/types/bot_command.py new file mode 100644 index 00000000..a97238e5 --- /dev/null +++ b/aiogram/types/bot_command.py @@ -0,0 +1,12 @@ +from . import base +from . import fields + + +class BotCommand(base.TelegramObject): + """ + This object represents a bot command. + + https://core.telegram.org/bots/api#botcommand + """ + command: base.String = fields.Field() + description: base.String = fields.Field() From c8f126b8ea5e08a38a8a61e32c57339372f0fcbd Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 16:42:19 +0300 Subject: [PATCH 06/19] #289 added getMyCommands method --- aiogram/bot/api.py | 1 + aiogram/bot/bot.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index 6f3dc951..5b760d07 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -207,6 +207,7 @@ class Methods(Helper): DELETE_CHAT_STICKER_SET = Item() # deleteChatStickerSet ANSWER_CALLBACK_QUERY = Item() # answerCallbackQuery SET_MY_COMMANDS = Item() # setMyCommands + GET_MY_COMMANDS = Item() # getMyCommands # Updating messages EDIT_MESSAGE_TEXT = Item() # editMessageText diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index 1b1e8615..e8de768d 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1530,7 +1530,7 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): At most 100 commands can be specified. :type commands: :obj: `typing.List[types.BotCommand]` :return: Returns True on success. - :rtype: base.Boolean + :rtype: :obj:`base.Boolean` """ commands = prepare_arg(commands) payload = generate_payload(**locals()) @@ -1538,6 +1538,19 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.SET_MY_COMMANDS, payload) return result + async def get_my_commands(self) -> typing.List[types.BotCommand]: + """ + Use this method to get the current list of the bot's commands. + + Source: https://core.telegram.org/bots/api#getmycommands + :return: Returns Array of BotCommand on success. + :rtype: :obj:`typing.List[types.BotCommand]` + """ + payload = generate_payload(**locals()) + + result = await self.request(api.Methods.GET_MY_COMMANDS, payload) + return result + async def edit_message_text(self, text: base.String, chat_id: typing.Union[base.Integer, base.String, None] = None, message_id: typing.Union[base.Integer, None] = None, From 38a2309c32579e11d131995ad337fb24f296f2f8 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 16:48:34 +0300 Subject: [PATCH 07/19] #289 added setMyCommands test --- tests/test_bot.py | 11 +++++++++++ tests/types/dataset.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 0fa23df4..b2289c8f 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -473,6 +473,17 @@ async def test_answer_callback_query(bot: Bot, event_loop): assert result is True +async def test_set_my_commands(bot: Bot, event_loop): + """ setMyCommands method test """ + from .types.dataset import BOT_COMMAND + + async with FakeTelegram(message_dict=True, loop=event_loop): + commands = [types.BotCommand(**BOT_COMMAND), types.BotCommand(**BOT_COMMAND)] + result = await bot.set_my_commands(commands) + assert isinstance(result, bool) + assert result is True + + async def test_edit_message_text_by_bot(bot: Bot, event_loop): """ editMessageText method test """ from .types.dataset import EDITED_MESSAGE diff --git a/tests/types/dataset.py b/tests/types/dataset.py index 8950344c..310024cb 100644 --- a/tests/types/dataset.py +++ b/tests/types/dataset.py @@ -35,6 +35,11 @@ AUDIO = { "file_size": 9507774, } +BOT_COMMAND = { + "command": "start", + "description": "Start bot", +} + CHAT_MEMBER = { "user": USER, "status": "administrator", From a5f9373381d5cd4bdea458fe8f2e0a3f9244f047 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:02:43 +0300 Subject: [PATCH 08/19] refactored message_dict to message_data in FakeTelegram --- tests/__init__.py | 6 ++-- tests/test_bot.py | 84 +++++++++++++++++++++---------------------- tests/test_message.py | 2 +- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 262c9395..71ed023b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,9 +6,11 @@ TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' class FakeTelegram(aresponses.ResponsesMockServer): - def __init__(self, message_dict, bot=None, **kwargs): + def __init__(self, message_data, bot=None, **kwargs): + from aiogram.utils.payload import _normalize super().__init__(**kwargs) - self._body, self._headers = self.parse_data(message_dict) + message_data = _normalize(message_data) + self._body, self._headers = self.parse_data(message_data) if isinstance(bot, Bot): Bot.set_current(bot) diff --git a/tests/test_bot.py b/tests/test_bot.py index b2289c8f..c33b07d7 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -19,7 +19,7 @@ async def test_get_me(bot: Bot, event_loop): from .types.dataset import USER user = types.User(**USER) - async with FakeTelegram(message_dict=USER, loop=event_loop): + async with FakeTelegram(message_data=USER, loop=event_loop): result = await bot.me assert result == user @@ -29,7 +29,7 @@ async def test_send_message(bot: Bot, event_loop): from .types.dataset import MESSAGE msg = types.Message(**MESSAGE) - async with FakeTelegram(message_dict=MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE, loop=event_loop): result = await bot.send_message(chat_id=msg.chat.id, text=msg.text) assert result == msg @@ -39,7 +39,7 @@ async def test_forward_message(bot: Bot, event_loop): from .types.dataset import FORWARDED_MESSAGE msg = types.Message(**FORWARDED_MESSAGE) - async with FakeTelegram(message_dict=FORWARDED_MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=FORWARDED_MESSAGE, loop=event_loop): result = await bot.forward_message(chat_id=msg.chat.id, from_chat_id=msg.forward_from_chat.id, message_id=msg.forward_from_message_id) assert result == msg @@ -51,7 +51,7 @@ async def test_send_photo(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_PHOTO) photo = types.PhotoSize(**PHOTO) - async with FakeTelegram(message_dict=MESSAGE_WITH_PHOTO, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_PHOTO, loop=event_loop): result = await bot.send_photo(msg.chat.id, photo=photo.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, disable_notification=False) assert result == msg @@ -62,7 +62,7 @@ async def test_send_audio(bot: Bot, event_loop): from .types.dataset import MESSAGE_WITH_AUDIO msg = types.Message(**MESSAGE_WITH_AUDIO) - async with FakeTelegram(message_dict=MESSAGE_WITH_AUDIO, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_AUDIO, loop=event_loop): result = await bot.send_audio(chat_id=msg.chat.id, audio=msg.audio.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, duration=msg.audio.duration, performer=msg.audio.performer, title=msg.audio.title, disable_notification=False) @@ -74,7 +74,7 @@ async def test_send_document(bot: Bot, event_loop): from .types.dataset import MESSAGE_WITH_DOCUMENT msg = types.Message(**MESSAGE_WITH_DOCUMENT) - async with FakeTelegram(message_dict=MESSAGE_WITH_DOCUMENT, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_DOCUMENT, loop=event_loop): result = await bot.send_document(chat_id=msg.chat.id, document=msg.document.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, disable_notification=False) assert result == msg @@ -86,7 +86,7 @@ async def test_send_video(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_VIDEO) video = types.Video(**VIDEO) - async with FakeTelegram(message_dict=MESSAGE_WITH_VIDEO, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VIDEO, loop=event_loop): result = await bot.send_video(chat_id=msg.chat.id, video=video.file_id, duration=video.duration, width=video.width, height=video.height, caption=msg.caption, parse_mode=types.ParseMode.HTML, supports_streaming=True, @@ -100,7 +100,7 @@ async def test_send_voice(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_VOICE) voice = types.Voice(**VOICE) - async with FakeTelegram(message_dict=MESSAGE_WITH_VOICE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VOICE, loop=event_loop): result = await bot.send_voice(chat_id=msg.chat.id, voice=voice.file_id, caption=msg.caption, parse_mode=types.ParseMode.HTML, duration=voice.duration, disable_notification=False) @@ -113,7 +113,7 @@ async def test_send_video_note(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_VIDEO_NOTE) video_note = types.VideoNote(**VIDEO_NOTE) - async with FakeTelegram(message_dict=MESSAGE_WITH_VIDEO_NOTE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VIDEO_NOTE, loop=event_loop): result = await bot.send_video_note(chat_id=msg.chat.id, video_note=video_note.file_id, duration=video_note.duration, length=video_note.length, disable_notification=False) @@ -127,7 +127,7 @@ async def test_send_media_group(bot: Bot, event_loop): photo = types.PhotoSize(**PHOTO) media = [types.InputMediaPhoto(media=photo.file_id), types.InputMediaPhoto(media=photo.file_id)] - async with FakeTelegram(message_dict=[MESSAGE_WITH_MEDIA_GROUP, MESSAGE_WITH_MEDIA_GROUP], loop=event_loop): + async with FakeTelegram(message_data=[MESSAGE_WITH_MEDIA_GROUP, MESSAGE_WITH_MEDIA_GROUP], loop=event_loop): result = await bot.send_media_group(msg.chat.id, media=media, disable_notification=False) assert len(result) == len(media) assert result.pop().media_group_id @@ -139,7 +139,7 @@ async def test_send_location(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_LOCATION) location = types.Location(**LOCATION) - async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_LOCATION, loop=event_loop): result = await bot.send_location(msg.chat.id, latitude=location.latitude, longitude=location.longitude, live_period=10, disable_notification=False) assert result == msg @@ -152,7 +152,7 @@ async def test_edit_message_live_location_by_bot(bot: Bot, event_loop): location = types.Location(**LOCATION) # editing bot message - async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_LOCATION, loop=event_loop): result = await bot.edit_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id, latitude=location.latitude, longitude=location.longitude) assert result == msg @@ -165,7 +165,7 @@ async def test_edit_message_live_location_by_user(bot: Bot, event_loop): location = types.Location(**LOCATION) # editing user's message - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.edit_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id, latitude=location.latitude, longitude=location.longitude) assert isinstance(result, bool) and result is True @@ -177,7 +177,7 @@ async def test_stop_message_live_location_by_bot(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_LOCATION) # stopping bot message - async with FakeTelegram(message_dict=MESSAGE_WITH_LOCATION, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_LOCATION, loop=event_loop): result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) assert result == msg @@ -188,7 +188,7 @@ async def test_stop_message_live_location_by_user(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_LOCATION) # stopping user's message - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.stop_message_live_location(chat_id=msg.chat.id, message_id=msg.message_id) assert isinstance(result, bool) assert result is True @@ -201,7 +201,7 @@ async def test_send_venue(bot: Bot, event_loop): location = types.Location(**LOCATION) venue = types.Venue(**VENUE) - async with FakeTelegram(message_dict=MESSAGE_WITH_VENUE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_VENUE, loop=event_loop): result = await bot.send_venue(msg.chat.id, latitude=location.latitude, longitude=location.longitude, title=venue.title, address=venue.address, foursquare_id=venue.foursquare_id, disable_notification=False) @@ -214,7 +214,7 @@ async def test_send_contact(bot: Bot, event_loop): msg = types.Message(**MESSAGE_WITH_CONTACT) contact = types.Contact(**CONTACT) - async with FakeTelegram(message_dict=MESSAGE_WITH_CONTACT, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_CONTACT, loop=event_loop): result = await bot.send_contact(msg.chat.id, phone_number=contact.phone_number, first_name=contact.first_name, last_name=contact.last_name, disable_notification=False) assert result == msg @@ -225,7 +225,7 @@ async def test_send_dice(bot: Bot, event_loop): from .types.dataset import MESSAGE_WITH_DICE msg = types.Message(**MESSAGE_WITH_DICE) - async with FakeTelegram(message_dict=MESSAGE_WITH_DICE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE_WITH_DICE, loop=event_loop): result = await bot.send_dice(msg.chat.id, disable_notification=False) assert result == msg @@ -235,7 +235,7 @@ async def test_send_chat_action(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.send_chat_action(chat_id=chat.id, action=types.ChatActions.TYPING) assert isinstance(result, bool) assert result is True @@ -246,7 +246,7 @@ async def test_get_user_profile_photo(bot: Bot, event_loop): from .types.dataset import USER_PROFILE_PHOTOS, USER user = types.User(**USER) - async with FakeTelegram(message_dict=USER_PROFILE_PHOTOS, loop=event_loop): + async with FakeTelegram(message_data=USER_PROFILE_PHOTOS, loop=event_loop): result = await bot.get_user_profile_photos(user_id=user.id, offset=1, limit=1) assert isinstance(result, types.UserProfilePhotos) @@ -256,7 +256,7 @@ async def test_get_file(bot: Bot, event_loop): from .types.dataset import FILE file = types.File(**FILE) - async with FakeTelegram(message_dict=FILE, loop=event_loop): + async with FakeTelegram(message_data=FILE, loop=event_loop): result = await bot.get_file(file_id=file.file_id) assert isinstance(result, types.File) @@ -267,7 +267,7 @@ async def test_kick_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.kick_chat_member(chat_id=chat.id, user_id=user.id, until_date=123) assert isinstance(result, bool) assert result is True @@ -279,7 +279,7 @@ async def test_unban_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.unban_chat_member(chat_id=chat.id, user_id=user.id) assert isinstance(result, bool) assert result is True @@ -291,7 +291,7 @@ async def test_restrict_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.restrict_chat_member( chat_id=chat.id, user_id=user.id, @@ -311,7 +311,7 @@ async def test_promote_chat_member(bot: Bot, event_loop): user = types.User(**USER) chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.promote_chat_member(chat_id=chat.id, user_id=user.id, can_change_info=True, can_delete_messages=True, can_edit_messages=True, can_invite_users=True, can_pin_messages=True, can_post_messages=True, @@ -325,7 +325,7 @@ async def test_export_chat_invite_link(bot: Bot, event_loop): from .types.dataset import CHAT, INVITE_LINK chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=INVITE_LINK, loop=event_loop): + async with FakeTelegram(message_data=INVITE_LINK, loop=event_loop): result = await bot.export_chat_invite_link(chat_id=chat.id) assert result == INVITE_LINK @@ -335,7 +335,7 @@ async def test_delete_chat_photo(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.delete_chat_photo(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -346,7 +346,7 @@ async def test_set_chat_title(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.set_chat_title(chat_id=chat.id, title='Test title') assert isinstance(result, bool) assert result is True @@ -357,7 +357,7 @@ async def test_set_chat_description(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.set_chat_description(chat_id=chat.id, description='Test description') assert isinstance(result, bool) assert result is True @@ -368,7 +368,7 @@ async def test_pin_chat_message(bot: Bot, event_loop): from .types.dataset import MESSAGE message = types.Message(**MESSAGE) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.pin_chat_message(chat_id=message.chat.id, message_id=message.message_id, disable_notification=False) assert isinstance(result, bool) @@ -380,7 +380,7 @@ async def test_unpin_chat_message(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.unpin_chat_message(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -391,7 +391,7 @@ async def test_leave_chat(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.leave_chat(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -402,7 +402,7 @@ async def test_get_chat(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=CHAT, loop=event_loop): + async with FakeTelegram(message_data=CHAT, loop=event_loop): result = await bot.get_chat(chat_id=chat.id) assert result == chat @@ -413,7 +413,7 @@ async def test_get_chat_administrators(bot: Bot, event_loop): chat = types.Chat(**CHAT) member = types.ChatMember(**CHAT_MEMBER) - async with FakeTelegram(message_dict=[CHAT_MEMBER, CHAT_MEMBER], loop=event_loop): + async with FakeTelegram(message_data=[CHAT_MEMBER, CHAT_MEMBER], loop=event_loop): result = await bot.get_chat_administrators(chat_id=chat.id) assert result[0] == member assert len(result) == 2 @@ -425,7 +425,7 @@ async def test_get_chat_members_count(bot: Bot, event_loop): chat = types.Chat(**CHAT) count = 5 - async with FakeTelegram(message_dict=count, loop=event_loop): + async with FakeTelegram(message_data=count, loop=event_loop): result = await bot.get_chat_members_count(chat_id=chat.id) assert result == count @@ -436,7 +436,7 @@ async def test_get_chat_member(bot: Bot, event_loop): chat = types.Chat(**CHAT) member = types.ChatMember(**CHAT_MEMBER) - async with FakeTelegram(message_dict=CHAT_MEMBER, loop=event_loop): + async with FakeTelegram(message_data=CHAT_MEMBER, loop=event_loop): result = await bot.get_chat_member(chat_id=chat.id, user_id=member.user.id) assert isinstance(result, types.ChatMember) assert result == member @@ -447,7 +447,7 @@ async def test_set_chat_sticker_set(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.set_chat_sticker_set(chat_id=chat.id, sticker_set_name='aiogram_stickers') assert isinstance(result, bool) assert result is True @@ -458,7 +458,7 @@ async def test_delete_chat_sticker_set(bot: Bot, event_loop): from .types.dataset import CHAT chat = types.Chat(**CHAT) - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.delete_chat_sticker_set(chat_id=chat.id) assert isinstance(result, bool) assert result is True @@ -467,7 +467,7 @@ async def test_delete_chat_sticker_set(bot: Bot, event_loop): async def test_answer_callback_query(bot: Bot, event_loop): """ answerCallbackQuery method test """ - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.answer_callback_query(callback_query_id='QuERyId', text='Test Answer') assert isinstance(result, bool) assert result is True @@ -477,7 +477,7 @@ async def test_set_my_commands(bot: Bot, event_loop): """ setMyCommands method test """ from .types.dataset import BOT_COMMAND - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): commands = [types.BotCommand(**BOT_COMMAND), types.BotCommand(**BOT_COMMAND)] result = await bot.set_my_commands(commands) assert isinstance(result, bool) @@ -490,7 +490,7 @@ async def test_edit_message_text_by_bot(bot: Bot, event_loop): msg = types.Message(**EDITED_MESSAGE) # message by bot - async with FakeTelegram(message_dict=EDITED_MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=EDITED_MESSAGE, loop=event_loop): result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) assert result == msg @@ -501,7 +501,7 @@ async def test_edit_message_text_by_user(bot: Bot, event_loop): msg = types.Message(**EDITED_MESSAGE) # message by user - async with FakeTelegram(message_dict=True, loop=event_loop): + async with FakeTelegram(message_data=True, loop=event_loop): result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) assert isinstance(result, bool) assert result is True diff --git a/tests/test_message.py b/tests/test_message.py index 996529f3..32168d57 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -28,7 +28,7 @@ async def message(bot, event_loop): from .types.dataset import MESSAGE msg = types.Message(**MESSAGE) - async with FakeTelegram(message_dict=MESSAGE, loop=event_loop): + async with FakeTelegram(message_data=MESSAGE, loop=event_loop): _message = await bot.send_message(chat_id=msg.chat.id, text=msg.text) yield _message From 93b60b6d75a2530e0ffb14c12cf104255c055b6c Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:25:16 +0300 Subject: [PATCH 09/19] #289 fixed getMyCommands output --- aiogram/bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index e8de768d..d677e060 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1549,7 +1549,7 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): payload = generate_payload(**locals()) result = await self.request(api.Methods.GET_MY_COMMANDS, payload) - return result + return [types.BotCommand(**bot_command_data) for bot_command_data in result] async def edit_message_text(self, text: base.String, chat_id: typing.Union[base.Integer, base.String, None] = None, From 505d6bf75bccc3aa9f519d8f808f0e01bdc6f631 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:26:18 +0300 Subject: [PATCH 10/19] normalized FakeTelegram passed data --- tests/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 71ed023b..3a85ce3f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -7,9 +7,7 @@ TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' class FakeTelegram(aresponses.ResponsesMockServer): def __init__(self, message_data, bot=None, **kwargs): - from aiogram.utils.payload import _normalize super().__init__(**kwargs) - message_data = _normalize(message_data) self._body, self._headers = self.parse_data(message_data) if isinstance(bot, Bot): @@ -26,10 +24,11 @@ class FakeTelegram(aresponses.ResponsesMockServer): await super().__aexit__(exc_type, exc_val, exc_tb) @staticmethod - def parse_data(message_dict): + def parse_data(message_data): import json + from aiogram.utils.payload import _normalize - _body = '{"ok":true,"result":' + json.dumps(message_dict) + '}' + _body = '{"ok":true,"result":' + json.dumps(_normalize(message_data)) + '}' _headers = {'Server': 'nginx/1.12.2', 'Date': 'Tue, 03 Apr 2018 16:59:54 GMT', 'Content-Type': 'application/json', From 29d767bd78cbed213ee72edbd376eccb575e4d26 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:26:53 +0300 Subject: [PATCH 11/19] #289 added getMyCommands test --- tests/test_bot.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index c33b07d7..70f8eb89 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -484,6 +484,17 @@ async def test_set_my_commands(bot: Bot, event_loop): assert result is True +async def test_get_my_commands(bot: Bot, event_loop): + """ getMyCommands method test """ + from .types.dataset import BOT_COMMAND + command = types.BotCommand(**BOT_COMMAND) + commands = [command, command] + async with FakeTelegram(message_data=commands, loop=event_loop): + result = await bot.get_my_commands() + assert isinstance(result, list) + assert all([isinstance(command, types.BotCommand) for command in result]) + + async def test_edit_message_text_by_bot(bot: Bot, event_loop): """ editMessageText method test """ from .types.dataset import EDITED_MESSAGE From 3ab5b80cafbf9bf7c4dc7b2e9eb6fd47d0365744 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:48:35 +0300 Subject: [PATCH 12/19] #289 Added the ability to create animated sticker sets by specifying the parameter tgs_sticker instead of png_sticker in the method createNewStickerSet --- aiogram/bot/bot.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index d677e060..01c66e03 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1837,8 +1837,13 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.UPLOAD_STICKER_FILE, payload, files) return types.File(**result) - async def create_new_sticker_set(self, user_id: base.Integer, name: base.String, title: base.String, - png_sticker: typing.Union[base.InputFile, base.String], emojis: base.String, + async def create_new_sticker_set(self, + user_id: base.Integer, + name: base.String, + title: base.String, + png_sticker: typing.Union[base.InputFile, base.String], + tgs_sticker: base.InputFile, + emojis: base.String, contains_masks: typing.Union[base.Boolean, None] = None, mask_position: typing.Union[types.MaskPosition, None] = None) -> base.Boolean: """ @@ -1855,6 +1860,9 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): :param png_sticker: Png image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. :type png_sticker: :obj:`typing.Union[base.InputFile, base.String]` + :param tgs_sticker: TGS animation with the sticker, uploaded using multipart/form-data. + See https://core.telegram.org/animated_stickers#technical-requirements for technical requirements + :type tgs_sticker: :obj:`base.InputFile` :param emojis: One or more emoji corresponding to the sticker :type emojis: :obj:`base.String` :param contains_masks: Pass True, if a set of mask stickers should be created @@ -1865,10 +1873,11 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): :rtype: :obj:`base.Boolean` """ mask_position = prepare_arg(mask_position) - payload = generate_payload(**locals(), exclude=['png_sticker']) + payload = generate_payload(**locals(), exclude=['png_sticker', 'tgs_sticker']) files = {} prepare_file(payload, files, 'png_sticker', png_sticker) + prepare_file(payload, files, 'tgs_sticker', tgs_sticker) result = await self.request(api.Methods.CREATE_NEW_STICKER_SET, payload, files) return result From 12faef50f81df80e7d26ef95151377fb8bc9f5df Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 17:55:00 +0300 Subject: [PATCH 13/19] #289 updated createNewStickerSet docs --- aiogram/bot/bot.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index 01c66e03..a63d4f00 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1847,18 +1847,26 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): contains_masks: typing.Union[base.Boolean, None] = None, mask_position: typing.Union[types.MaskPosition, None] = None) -> base.Boolean: """ - Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set. + Use this method to create a new sticker set owned by a user. + The bot will be able to edit the sticker set thus created. + You must use exactly one of the fields png_sticker or tgs_sticker. Source: https://core.telegram.org/bots/api#createnewstickerset :param user_id: User identifier of created sticker set owner :type user_id: :obj:`base.Integer` - :param name: Short name of sticker set, to be used in t.me/addstickers/ URLs (e.g., animals) + :param name: Short name of sticker set, to be used in t.me/addstickers/ URLs (e.g., animals). + Can contain only english letters, digits and underscores. + Must begin with a letter, can't contain consecutive underscores and must end in “_by_”. + is case insensitive. 1-64 characters. :type name: :obj:`base.String` :param title: Sticker set title, 1-64 characters :type title: :obj:`base.String` - :param png_sticker: Png image with the sticker, must be up to 512 kilobytes in size, + :param png_sticker: PNG image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. + Pass a file_id as a String to send a file that already exists on the Telegram servers, + pass an HTTP URL as a String for Telegram to get a file from the Internet, or + upload a new one using multipart/form-data. More info on https://core.telegram.org/bots/api#sending-files :type png_sticker: :obj:`typing.Union[base.InputFile, base.String]` :param tgs_sticker: TGS animation with the sticker, uploaded using multipart/form-data. See https://core.telegram.org/animated_stickers#technical-requirements for technical requirements From 181f1f0e6dbbd61f48c3ed451b8819117a02f57d Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 18:48:37 +0300 Subject: [PATCH 14/19] #289 Added the ability to add animated stickers to sets created by the bot by specifying the parameter tgs_sticker instead of png_sticker in the method addStickerToSet. --- aiogram/bot/bot.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index a63d4f00..9d242681 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1841,9 +1841,9 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): user_id: base.Integer, name: base.String, title: base.String, - png_sticker: typing.Union[base.InputFile, base.String], - tgs_sticker: base.InputFile, emojis: base.String, + png_sticker: typing.Union[base.InputFile, base.String] = None, + tgs_sticker: base.InputFile = None, contains_masks: typing.Union[base.Boolean, None] = None, mask_position: typing.Union[types.MaskPosition, None] = None) -> base.Boolean: """ @@ -1890,11 +1890,19 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.CREATE_NEW_STICKER_SET, payload, files) return result - async def add_sticker_to_set(self, user_id: base.Integer, name: base.String, - png_sticker: typing.Union[base.InputFile, base.String], emojis: base.String, + async def add_sticker_to_set(self, + user_id: base.Integer, + name: base.String, + emojis: base.String, + png_sticker: typing.Union[base.InputFile, base.String] = None, + tgs_sticker: base.InputFile = None, mask_position: typing.Union[types.MaskPosition, None] = None) -> base.Boolean: """ Use this method to add a new sticker to a set created by the bot. + You must use exactly one of the fields png_sticker or tgs_sticker. + Animated stickers can be added to animated sticker sets and only to them. + Animated sticker sets can have up to 50 stickers. + Static sticker sets can have up to 120 stickers. Source: https://core.telegram.org/bots/api#addstickertoset @@ -1902,9 +1910,15 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): :type user_id: :obj:`base.Integer` :param name: Sticker set name :type name: :obj:`base.String` - :param png_sticker: Png image with the sticker, must be up to 512 kilobytes in size, + :param png_sticker: PNG image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. + Pass a file_id as a String to send a file that already exists on the Telegram servers, + pass an HTTP URL as a String for Telegram to get a file from the Internet, or + upload a new one using multipart/form-data. More info on https://core.telegram.org/bots/api#sending-files :type png_sticker: :obj:`typing.Union[base.InputFile, base.String]` + :param tgs_sticker: TGS animation with the sticker, uploaded using multipart/form-data. + See https://core.telegram.org/animated_stickers#technical-requirements for technical requirements + :type tgs_sticker: :obj:`base.InputFile` :param emojis: One or more emoji corresponding to the sticker :type emojis: :obj:`base.String` :param mask_position: A JSON-serialized object for position where the mask should be placed on faces @@ -1913,10 +1927,11 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): :rtype: :obj:`base.Boolean` """ mask_position = prepare_arg(mask_position) - payload = generate_payload(**locals(), exclude=['png_sticker']) + payload = generate_payload(**locals(), exclude=['png_sticker', 'tgs_sticker']) files = {} prepare_file(payload, files, 'png_sticker', png_sticker) + prepare_file(payload, files, 'tgs_sticker', png_sticker) result = await self.request(api.Methods.ADD_STICKER_TO_SET, payload, files) return result From 3d632fea1c13ff1c1b6300c42ec877a31f0953a4 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 18:54:10 +0300 Subject: [PATCH 15/19] #289 Added the field thumb to the StickerSet object. --- aiogram/types/sticker_set.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aiogram/types/sticker_set.py b/aiogram/types/sticker_set.py index cb30abe6..3b5290c3 100644 --- a/aiogram/types/sticker_set.py +++ b/aiogram/types/sticker_set.py @@ -2,6 +2,7 @@ import typing from . import base from . import fields +from .photo_size import PhotoSize from .sticker import Sticker @@ -16,3 +17,4 @@ class StickerSet(base.TelegramObject): is_animated: base.Boolean = fields.Field() contains_masks: base.Boolean = fields.Field() stickers: typing.List[Sticker] = fields.ListField(base=Sticker) + thumb: PhotoSize = fields.Field(base=PhotoSize) From 9bbc7510f4dbd252847626df09023b21c080f894 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 19:04:48 +0300 Subject: [PATCH 16/19] #289 Added the ability to change thumbnails of sticker sets created by the bot using the method setStickerSetThumb. --- aiogram/bot/api.py | 1 + aiogram/bot/bot.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index 5b760d07..f0553644 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -225,6 +225,7 @@ class Methods(Helper): ADD_STICKER_TO_SET = Item() # addStickerToSet SET_STICKER_POSITION_IN_SET = Item() # setStickerPositionInSet DELETE_STICKER_FROM_SET = Item() # deleteStickerFromSet + SET_STICKER_SET_THUMB = Item() # setStickerSetThumb # Inline mode ANSWER_INLINE_QUERY = Item() # answerInlineQuery diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index 9d242681..d6946513 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1970,6 +1970,39 @@ class Bot(BaseBot, DataMixin, ContextInstanceMixin): result = await self.request(api.Methods.DELETE_STICKER_FROM_SET, payload) return result + async def set_sticker_set_thumb(self, + name: base.String, + user_id: base.Integer, + thumb: typing.Union[base.InputFile, base.String] = None) -> base.Boolean: + """ + Use this method to set the thumbnail of a sticker set. + Animated thumbnails can be set for animated sticker sets only. + + Source: https://core.telegram.org/bots/api#setstickersetthumb + + :param name: Sticker set name + :type name: :obj:`base.String` + :param user_id: User identifier of the sticker set owner + :type user_id: :obj:`base.Integer` + :param thumb: A PNG image with the thumbnail, must be up to 128 kilobytes in size and have width and height + exactly 100px, or a TGS animation with the thumbnail up to 32 kilobytes in size; + see https://core.telegram.org/animated_stickers#technical-requirements for animated sticker technical + requirements. Pass a file_id as a String to send a file that already exists on the Telegram servers, + pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using + multipart/form-data. More info on https://core.telegram.org/bots/api#sending-files. + Animated sticker set thumbnail can't be uploaded via HTTP URL. + :type thumb: :obj:`typing.Union[base.InputFile, base.String]` + :return: Returns True on success + :rtype: :obj:`base.Boolean` + """ + payload = generate_payload(**locals(), exclude=['thumb']) + + files = {} + prepare_file(payload, files, 'thumb', thumb) + + result = await self.request(api.Methods.SET_STICKER_SET_THUMB, payload, files) + return result + async def answer_inline_query(self, inline_query_id: base.String, results: typing.List[types.InlineQueryResult], cache_time: typing.Union[base.Integer, None] = None, From 6cc769ce4f22234dadd021f7f46228242b99a2d5 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 19:13:33 +0300 Subject: [PATCH 17/19] #289 Added setStickerSetThumb test --- tests/test_bot.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_bot.py b/tests/test_bot.py index 70f8eb89..92a2bd0f 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -516,3 +516,12 @@ async def test_edit_message_text_by_user(bot: Bot, event_loop): result = await bot.edit_message_text(text=msg.text, chat_id=msg.chat.id, message_id=msg.message_id) assert isinstance(result, bool) assert result is True + + +async def test_set_sticker_set_thumb(bot: Bot, event_loop): + """ setStickerSetThumb method test """ + + async with FakeTelegram(message_data=True, loop=event_loop): + result = await bot.set_sticker_set_thumb(name='test', user_id=123456789, thumb='file_id') + assert isinstance(result, bool) + assert result is True From 683e2befb560c9d490f733025510233f5fd74a1b Mon Sep 17 00:00:00 2001 From: Oleg A Date: Sun, 5 Apr 2020 19:55:53 +0300 Subject: [PATCH 18/19] #289 Added BotCammand and Dice to __all__ types --- aiogram/types/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index 693bd7d5..2cd15ac6 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -71,6 +71,7 @@ __all__ = ( 'Animation', 'Audio', 'AuthWidgetData', + 'BotCommand', 'CallbackGame', 'CallbackQuery', 'Chat', @@ -83,6 +84,7 @@ __all__ = ( 'Contact', 'ContentType', 'ContentTypes', + 'Dice', 'Document', 'EncryptedCredentials', 'EncryptedPassportElement', From 97bda44718397e050e68f9f64516d4d79ce032d0 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 6 Apr 2020 23:59:37 +0300 Subject: [PATCH 19/19] #289 BotCommand type init fix --- aiogram/types/bot_command.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aiogram/types/bot_command.py b/aiogram/types/bot_command.py index a97238e5..39e38e4f 100644 --- a/aiogram/types/bot_command.py +++ b/aiogram/types/bot_command.py @@ -10,3 +10,6 @@ class BotCommand(base.TelegramObject): """ command: base.String = fields.Field() description: base.String = fields.Field() + + def __init__(self, command: base.String, description: base.String): + super(BotCommand, self).__init__(command=command, description=description) \ No newline at end of file