Example #1
0
    def from_array(array):
        """
        Deserializes a new Update from a given dictionary.

        :return: new Update instance.
        :rtype: Update
        """
        if array is None or not array:
            return None
        # end if
        assert(isinstance(array, dict))
        
        from pytgbot.api_types.receivable.inline import ChosenInlineResult
        from pytgbot.api_types.receivable.inline import InlineQuery
        from pytgbot.api_types.receivable.updates import CallbackQuery
        from pytgbot.api_types.receivable.updates import Message
        
        data = {}
        data['update_id'] = int(array.get('update_id'))
        data['message'] = Message.from_array(array.get('message')) if array.get('message') is not None else None
        data['edited_message'] = Message.from_array(array.get('edited_message')) if array.get('edited_message') is not None else None
        data['channel_post'] = Message.from_array(array.get('channel_post')) if array.get('channel_post') is not None else None
        data['edited_channel_post'] = Message.from_array(array.get('edited_channel_post')) if array.get('edited_channel_post') is not None else None
        data['inline_query'] = InlineQuery.from_array(array.get('inline_query')) if array.get('inline_query') is not None else None
        data['chosen_inline_result'] = ChosenInlineResult.from_array(array.get('chosen_inline_result')) if array.get('chosen_inline_result') is not None else None
        data['callback_query'] = CallbackQuery.from_array(array.get('callback_query')) if array.get('callback_query') is not None else None
        return Update(**data)
Example #2
0
def submit_sticker_message(message: Message):
    if not isinstance(message, Message) or not isinstance(message.sticker, Sticker):
        return
    # end if
    try:
        requests.put(
            GETSTICKERS_DOMAIN + '/api/v3/submit/sticker',
            params={
                "key": GETSTICKERS_API_KEY,
                "bot_id": __sticker_crawl.user_id,
            },
            data={
                "message": message.to_array(),
            },
            timeout=TIMEOUT,
        )
    except requests.HTTPError as e:
        try:
            result = repr(e.response.json())
        except:
            result = e.response.text
        # end try
        logger.warning(f'Submitting sticker to getstickers.me failed with error code {e.response.status_code}: {result}')
    except:
        logger.warning('Submitting sticker to getstickers.me failed.', exc_info=True)
Example #3
0
    def from_array(array):
        """
        Deserializes a new CallbackQuery from a given dictionary.

        :return: new CallbackQuery instance.
        :rtype: CallbackQuery
        """
        if array is None or not array:
            return None
        # end if
        assert (isinstance(array, dict))

        from pytgbot.api_types.receivable.peer import User
        from pytgbot.api_types.receivable.updates import Message

        data = {}
        data['id'] = str(array.get('id'))
        data['from_peer'] = User.from_array(array.get('from'))
        data['chat_instance'] = str(array.get('chat_instance'))
        data['message'] = Message.from_array(
            array.get('message')) if array.get('message') is not None else None
        data['inline_message_id'] = str(
            array.get('inline_message_id')) if array.get(
                'inline_message_id') is not None else None
        data['data'] = str(
            array.get('data')) if array.get('data') is not None else None
        data['game_short_name'] = str(
            array.get('game_short_name')) if array.get(
                'game_short_name') is not None else None
        return CallbackQuery(**data)
Example #4
0
    def from_array(array):
        """
        Deserializes a new Update from a given dictionary.

        :return: new Update instance.
        :rtype: Update
        """
        if array is None or not array:
            return None
        # end if
        assert (isinstance(array, dict))

        from pytgbot.api_types.receivable.inline import ChosenInlineResult
        from pytgbot.api_types.receivable.inline import InlineQuery
        from pytgbot.api_types.receivable.updates import CallbackQuery
        from pytgbot.api_types.receivable.updates import Message

        data = {}
        data['update_id'] = int(array.get('update_id'))
        data['message'] = Message.from_array(
            array.get('message')) if array.get('message') is not None else None
        data['edited_message'] = Message.from_array(
            array.get('edited_message')) if array.get(
                'edited_message') is not None else None
        data['channel_post'] = Message.from_array(array.get(
            'channel_post')) if array.get('channel_post') is not None else None
        data['edited_channel_post'] = Message.from_array(
            array.get('edited_channel_post')) if array.get(
                'edited_channel_post') is not None else None
        data['inline_query'] = InlineQuery.from_array(array.get(
            'inline_query')) if array.get('inline_query') is not None else None
        data['chosen_inline_result'] = ChosenInlineResult.from_array(
            array.get('chosen_inline_result')) if array.get(
                'chosen_inline_result') is not None else None
        data['callback_query'] = CallbackQuery.from_array(
            array.get('callback_query')) if array.get(
                'callback_query') is not None else None
        return Update(**data)
Example #5
0
    def test_commands(self):
        self.m.BEST_PONY = self.s
        self.assertEqual(self.m.CURRENT, self.m.DEFAULT)
        self.assertEqual(self.m.CURRENT.name, 'DEFAULT')

        update = Update(1,
                        message=Message(2,
                                        date=int(time.time()),
                                        from_peer=User(3, False, "Günter"),
                                        chat=Chat(4, 'supergroup',
                                                  'FAKE CHAT'),
                                        text='/start'))
        called = [False, False]

        def call_me(i):
            def call_me_inner(u, text):
                logger.info('called {i}.'.format(i=i))
                self.assertEqual(u, update)
                called[i] = True

            # end def
            return call_me_inner

        # end def

        self.m.DEFAULT.command('start')(call_me(0))
        self.m.BEST_PONY.command('start')(call_me(1))

        self.m.BEST_PONY.activate()
        self.assertEqual(self.m.CURRENT, self.s)
        self.assertEqual(self.m.CURRENT, self.m.BEST_PONY)
        self.assertEqual(self.m.CURRENT.name, 'BEST_PONY')

        self.m.process_update(update)
        self.assertEqual(
            self.m.CURRENT, self.m.DEFAULT,
            "load_state_for_chat_user should set DEFAULT (None) state again.")
        self.assertEqual(
            self.m.CURRENT.name, 'DEFAULT',
            "load_state_for_chat_user should set DEFAULT (None) state again.")
        self.assertTrue(
            called[0],
            'DEFAULT should have been called: load_state_for_chat_user set DEFAULT (None) state again.'
        )
        self.assertFalse(
            called[1],
            'BEST_PONY should not have been called: load_state_for_chat_user set DEFAULT (None) state again.'
        )
Example #6
0
    def from_array(array):
        """
        Deserialize a new Chat from a given dictionary.

        :return: new Chat instance.
        :rtype: Chat
        """
        if array is None or not array:
            return None
        # end if
        assert_type_or_raise(array, dict, parameter_name="array")
        from pytgbot.api_types.receivable.media import ChatPhoto
        from pytgbot.api_types.receivable.updates import Message

        data = {}
        data['id'] = int(array.get('id'))
        data['type'] = u(array.get('type'))
        data['title'] = u(
            array.get('title')) if array.get('title') is not None else None
        data['username'] = u(array.get('username')) if array.get(
            'username') is not None else None
        data['first_name'] = u(array.get('first_name')) if array.get(
            'first_name') is not None else None
        data['last_name'] = u(array.get('last_name')) if array.get(
            'last_name') is not None else None
        data['all_members_are_administrators'] = bool(
            array.get('all_members_are_administrators')) if array.get(
                'all_members_are_administrators') is not None else None
        data['photo'] = ChatPhoto.from_array(
            array.get('photo')) if array.get('photo') is not None else None
        data['description'] = u(array.get('description')) if array.get(
            'description') is not None else None
        data['invite_link'] = u(array.get('invite_link')) if array.get(
            'invite_link') is not None else None
        data['pinned_message'] = Message.from_array(
            array.get('pinned_message')) if array.get(
                'pinned_message') is not None else None
        data['sticker_set_name'] = u(
            array.get('sticker_set_name')) if array.get(
                'sticker_set_name') is not None else None
        data['can_set_sticker_set'] = bool(
            array.get('can_set_sticker_set')) if array.get(
                'can_set_sticker_set') is not None else None
        data['_raw'] = array
        return Chat(**data)
Example #7
0
    def from_array(array):
        """
        Deserializes a new CallbackQuery from a given dictionary.

        :return: new CallbackQuery instance.
        :rtype: CallbackQuery
        """
        if array is None or not array:
            return None
        # end if
        assert(isinstance(array, dict))
        
        from pytgbot.api_types.receivable.peer import User
        from pytgbot.api_types.receivable.updates import Message
        
        data = {}
        data['id'] = str(array.get('id'))
        data['from_peer'] = User.from_array(array.get('from'))
        data['chat_instance'] = str(array.get('chat_instance'))
        data['message'] = Message.from_array(array.get('message')) if array.get('message') is not None else None
        data['inline_message_id'] = str(array.get('inline_message_id')) if array.get('inline_message_id') is not None else None
        data['data'] = str(array.get('data')) if array.get('data') is not None else None
        data['game_short_name'] = str(array.get('game_short_name')) if array.get('game_short_name') is not None else None
        return CallbackQuery(**data)
Example #8
0
    def from_array(array):
        """
        Deserializes a new Message from a given dictionary.

        :return: new Message instance.
        :rtype: Message
        """
        if array is None or not array:
            return None
        # end if
        assert (isinstance(array, dict))

        from pytgbot.api_types.receivable.media import Audio
        from pytgbot.api_types.receivable.media import Contact
        from pytgbot.api_types.receivable.media import Document
        from pytgbot.api_types.receivable.media import Game
        from pytgbot.api_types.receivable.media import Location
        from pytgbot.api_types.receivable.media import MessageEntity
        from pytgbot.api_types.receivable.media import PhotoSize
        from pytgbot.api_types.receivable.media import Sticker
        from pytgbot.api_types.receivable.media import Venue
        from pytgbot.api_types.receivable.media import Video
        from pytgbot.api_types.receivable.media import Voice
        from pytgbot.api_types.receivable.peer import Chat
        from pytgbot.api_types.receivable.peer import User
        from pytgbot.api_types.receivable.updates import Message

        data = {}
        data['message_id'] = int(array.get('message_id'))
        data['date'] = int(array.get('date'))
        data['chat'] = Chat.from_array(array.get('chat'))
        data['from_peer'] = User.from_array(
            array.get('from')) if array.get('from') is not None else None
        data['forward_from'] = User.from_array(array.get(
            'forward_from')) if array.get('forward_from') is not None else None
        data['forward_from_chat'] = Chat.from_array(
            array.get('forward_from_chat')) if array.get(
                'forward_from_chat') is not None else None
        data['forward_from_message_id'] = int(
            array.get('forward_from_message_id')) if array.get(
                'forward_from_message_id') is not None else None
        data['forward_date'] = int(array.get('forward_date')) if array.get(
            'forward_date') is not None else None
        data['reply_to_message'] = Message.from_array(
            array.get('reply_to_message')) if array.get(
                'reply_to_message') is not None else None
        data['edit_date'] = int(array.get('edit_date')) if array.get(
            'edit_date') is not None else None
        data['text'] = str(
            array.get('text')) if array.get('text') is not None else None
        data['entities'] = MessageEntity.from_array_list(
            array.get('entities'),
            list_level=1) if array.get('entities') is not None else None
        data['audio'] = Audio.from_array(
            array.get('audio')) if array.get('audio') is not None else None
        data['document'] = Document.from_array(array.get(
            'document')) if array.get('document') is not None else None
        data['game'] = Game.from_array(
            array.get('game')) if array.get('game') is not None else None
        data['photo'] = PhotoSize.from_array_list(
            array.get('photo'),
            list_level=1) if array.get('photo') is not None else None
        data['sticker'] = Sticker.from_array(
            array.get('sticker')) if array.get('sticker') is not None else None
        data['video'] = Video.from_array(
            array.get('video')) if array.get('video') is not None else None
        data['voice'] = Voice.from_array(
            array.get('voice')) if array.get('voice') is not None else None
        data['caption'] = str(
            array.get('caption')) if array.get('caption') is not None else None
        data['contact'] = Contact.from_array(
            array.get('contact')) if array.get('contact') is not None else None
        data['location'] = Location.from_array(array.get(
            'location')) if array.get('location') is not None else None
        data['venue'] = Venue.from_array(
            array.get('venue')) if array.get('venue') is not None else None
        data['new_chat_member'] = User.from_array(
            array.get('new_chat_member')) if array.get(
                'new_chat_member') is not None else None
        data['left_chat_member'] = User.from_array(
            array.get('left_chat_member')) if array.get(
                'left_chat_member') is not None else None
        data['new_chat_title'] = str(array.get('new_chat_title')) if array.get(
            'new_chat_title') is not None else None
        data['new_chat_photo'] = PhotoSize.from_array_list(
            array.get('new_chat_photo'),
            list_level=1) if array.get('new_chat_photo') is not None else None
        data['delete_chat_photo'] = bool(
            array.get('delete_chat_photo')) if array.get(
                'delete_chat_photo') is not None else None
        data['group_chat_created'] = bool(
            array.get('group_chat_created')) if array.get(
                'group_chat_created') is not None else None
        data['supergroup_chat_created'] = bool(
            array.get('supergroup_chat_created')) if array.get(
                'supergroup_chat_created') is not None else None
        data['channel_chat_created'] = bool(
            array.get('channel_chat_created')) if array.get(
                'channel_chat_created') is not None else None
        data['migrate_to_chat_id'] = int(
            array.get('migrate_to_chat_id')) if array.get(
                'migrate_to_chat_id') is not None else None
        data['migrate_from_chat_id'] = int(
            array.get('migrate_from_chat_id')) if array.get(
                'migrate_from_chat_id') is not None else None
        data['pinned_message'] = Message.from_array(
            array.get('pinned_message')) if array.get(
                'pinned_message') is not None else None
        return Message(**data)
Example #9
0
    def from_array(array):
        """
        Deserializes a new Message from a given dictionary.

        :return: new Message instance.
        :rtype: Message
        """
        if array is None or not array:
            return None
        # end if
        assert(isinstance(array, dict))
        
        from pytgbot.api_types.receivable.media import Audio
        from pytgbot.api_types.receivable.media import Contact
        from pytgbot.api_types.receivable.media import Document
        from pytgbot.api_types.receivable.media import Game
        from pytgbot.api_types.receivable.media import Location
        from pytgbot.api_types.receivable.media import MessageEntity
        from pytgbot.api_types.receivable.media import PhotoSize
        from pytgbot.api_types.receivable.media import Sticker
        from pytgbot.api_types.receivable.media import Venue
        from pytgbot.api_types.receivable.media import Video
        from pytgbot.api_types.receivable.media import Voice
        from pytgbot.api_types.receivable.peer import Chat
        from pytgbot.api_types.receivable.peer import User
        from pytgbot.api_types.receivable.updates import Message
        
        data = {}
        data['message_id'] = int(array.get('message_id'))
        data['date'] = int(array.get('date'))
        data['chat'] = Chat.from_array(array.get('chat'))
        data['from_peer'] = User.from_array(array.get('from')) if array.get('from') is not None else None
        data['forward_from'] = User.from_array(array.get('forward_from')) if array.get('forward_from') is not None else None
        data['forward_from_chat'] = Chat.from_array(array.get('forward_from_chat')) if array.get('forward_from_chat') is not None else None
        data['forward_from_message_id'] = int(array.get('forward_from_message_id')) if array.get('forward_from_message_id') is not None else None
        data['forward_date'] = int(array.get('forward_date')) if array.get('forward_date') is not None else None
        data['reply_to_message'] = Message.from_array(array.get('reply_to_message')) if array.get('reply_to_message') is not None else None
        data['edit_date'] = int(array.get('edit_date')) if array.get('edit_date') is not None else None
        data['text'] = str(array.get('text')) if array.get('text') is not None else None
        data['entities'] = MessageEntity.from_array_list(array.get('entities'), list_level=1) if array.get('entities') is not None else None
        data['audio'] = Audio.from_array(array.get('audio')) if array.get('audio') is not None else None
        data['document'] = Document.from_array(array.get('document')) if array.get('document') is not None else None
        data['game'] = Game.from_array(array.get('game')) if array.get('game') is not None else None
        data['photo'] = PhotoSize.from_array_list(array.get('photo'), list_level=1) if array.get('photo') is not None else None
        data['sticker'] = Sticker.from_array(array.get('sticker')) if array.get('sticker') is not None else None
        data['video'] = Video.from_array(array.get('video')) if array.get('video') is not None else None
        data['voice'] = Voice.from_array(array.get('voice')) if array.get('voice') is not None else None
        data['caption'] = str(array.get('caption')) if array.get('caption') is not None else None
        data['contact'] = Contact.from_array(array.get('contact')) if array.get('contact') is not None else None
        data['location'] = Location.from_array(array.get('location')) if array.get('location') is not None else None
        data['venue'] = Venue.from_array(array.get('venue')) if array.get('venue') is not None else None
        data['new_chat_member'] = User.from_array(array.get('new_chat_member')) if array.get('new_chat_member') is not None else None
        data['left_chat_member'] = User.from_array(array.get('left_chat_member')) if array.get('left_chat_member') is not None else None
        data['new_chat_title'] = str(array.get('new_chat_title')) if array.get('new_chat_title') is not None else None
        data['new_chat_photo'] = PhotoSize.from_array_list(array.get('new_chat_photo'), list_level=1) if array.get('new_chat_photo') is not None else None
        data['delete_chat_photo'] = bool(array.get('delete_chat_photo')) if array.get('delete_chat_photo') is not None else None
        data['group_chat_created'] = bool(array.get('group_chat_created')) if array.get('group_chat_created') is not None else None
        data['supergroup_chat_created'] = bool(array.get('supergroup_chat_created')) if array.get('supergroup_chat_created') is not None else None
        data['channel_chat_created'] = bool(array.get('channel_chat_created')) if array.get('channel_chat_created') is not None else None
        data['migrate_to_chat_id'] = int(array.get('migrate_to_chat_id')) if array.get('migrate_to_chat_id') is not None else None
        data['migrate_from_chat_id'] = int(array.get('migrate_from_chat_id')) if array.get('migrate_from_chat_id') is not None else None
        data['pinned_message'] = Message.from_array(array.get('pinned_message')) if array.get('pinned_message') is not None else None
        return Message(**data)