Exemple #1
0
def print_photo(photo: telegram.PhotoSize):
    new_path = f'./tmp/{photo.file_id.replace(" ", "_")}.jpg'

    open(new_path, 'wb').close()
    try:
        with open(new_path, 'wb') as f:
            photo.get_file().download(out=f)
    except Exception as e:
        print(e)
    try:
        orientation = 3 if settings[ORIENTATION] is Orientation.portrait else 4
        command = f'lpr -# {settings[COPIES]} ' \
                  f'-o orientaion-requested={orientation} ' \
                  f'-P CLP-320-Series ' \
                  f'{new_path}'

        res = subprocess.run(args=command.split(),
                             check=True,
                             stdout=subprocess.PIPE,
                             text=True)
        print(res)
    except Exception as e:
        print(e)

    printer_await()

    os.remove(new_path)
    def de_json(data):
        """
        Args:
            data (dict):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'))
        data['entities'] = MessageEntity.de_list(data.get('entities'))
        data['forward_from'] = User.de_json(data.get('forward_from'))
        data['forward_date'] = Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = \
            Message.de_json(data.get('reply_to_message'))
        data['audio'] = Audio.de_json(data.get('audio'))
        data['document'] = Document.de_json(data.get('document'))
        data['photo'] = PhotoSize.de_list(data.get('photo'))
        data['sticker'] = Sticker.de_json(data.get('sticker'))
        data['video'] = Video.de_json(data.get('video'))
        data['voice'] = Voice.de_json(data.get('voice'))
        data['contact'] = Contact.de_json(data.get('contact'))
        data['location'] = Location.de_json(data.get('location'))
        data['venue'] = Venue.de_json(data.get('venue'))
        data['new_chat_member'] = User.de_json(data.get('new_chat_member'))
        data['left_chat_member'] = User.de_json(data.get('left_chat_member'))
        data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'))
        data['pinned_message'] = Message.de_json(data.get('pinned_message'))

        return Message(**data)
    def test_equality(self):
        a = Game('title', 'description',
                 [PhotoSize('Blah', 'unique_id', 640, 360, file_size=0)])
        b = Game(
            'title',
            'description',
            [PhotoSize('Blah', 'unique_id', 640, 360, file_size=0)],
            text='Here is a text',
        )
        c = Game(
            'eltit',
            'description',
            [PhotoSize('Blah', 'unique_id', 640, 360, file_size=0)],
            animation=Animation('blah', 'unique_id', 320, 180, 1),
        )
        d = Animation('blah', 'unique_id', 320, 180, 1)

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)
    def test_equality(self):
        a = Game("title", "description",
                 [PhotoSize("Blah", "unique_id", 640, 360, file_size=0)])
        b = Game(
            "title",
            "description",
            [PhotoSize("Blah", "unique_id", 640, 360, file_size=0)],
            text="Here is a text",
        )
        c = Game(
            "eltit",
            "description",
            [PhotoSize("Blah", "unique_id", 640, 360, file_size=0)],
            animation=Animation("blah", "unique_id", 320, 180, 1),
        )
        d = Animation("blah", "unique_id", 320, 180, 1)

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)
Exemple #5
0
    def de_json(data):
        """
        Args:
            data (dict):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'))
        data['entities'] = MessageEntity.de_list(data.get('entities'))
        data['forward_from'] = User.de_json(data.get('forward_from'))
        data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'))
        data['forward_date'] = Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = Message.de_json(
            data.get('reply_to_message'))
        data['audio'] = Audio.de_json(data.get('audio'))
        data['document'] = Document.de_json(data.get('document'))
        data['photo'] = PhotoSize.de_list(data.get('photo'))
        data['sticker'] = Sticker.de_json(data.get('sticker'))
        data['video'] = Video.de_json(data.get('video'))
        data['voice'] = Voice.de_json(data.get('voice'))
        data['contact'] = Contact.de_json(data.get('contact'))
        data['location'] = Location.de_json(data.get('location'))
        data['venue'] = Venue.de_json(data.get('venue'))
        data['new_chat_member'] = User.de_json(data.get('new_chat_member'))
        data['left_chat_member'] = User.de_json(data.get('left_chat_member'))
        data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'))
        data['pinned_message'] = Message.de_json(data.get('pinned_message'))

        return Message(**data)
Exemple #6
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Message, cls).de_json(data, bot)

        data['from_user'] = User.de_json(data.get('from'), bot)
        data['date'] = from_timestamp(data['date'])
        data['chat'] = Chat.de_json(data.get('chat'), bot)
        data['entities'] = MessageEntity.de_list(data.get('entities'), bot)
        data['forward_from'] = User.de_json(data.get('forward_from'), bot)
        data['forward_from_chat'] = Chat.de_json(data.get('forward_from_chat'), bot)
        data['forward_date'] = from_timestamp(data.get('forward_date'))
        data['reply_to_message'] = Message.de_json(data.get('reply_to_message'), bot)
        data['edit_date'] = from_timestamp(data.get('edit_date'))
        data['audio'] = Audio.de_json(data.get('audio'), bot)
        data['document'] = Document.de_json(data.get('document'), bot)
        data['game'] = Game.de_json(data.get('game'), bot)
        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['sticker'] = Sticker.de_json(data.get('sticker'), bot)
        data['video'] = Video.de_json(data.get('video'), bot)
        data['voice'] = Voice.de_json(data.get('voice'), bot)
        data['video_note'] = VideoNote.de_json(data.get('video_note'), bot)
        data['contact'] = Contact.de_json(data.get('contact'), bot)
        data['location'] = Location.de_json(data.get('location'), bot)
        data['venue'] = Venue.de_json(data.get('venue'), bot)
        data['new_chat_member'] = User.de_json(data.get('new_chat_member'), bot)
        data['new_chat_members'] = User.de_list(data.get('new_chat_members'), bot)
        data['left_chat_member'] = User.de_json(data.get('left_chat_member'), bot)
        data['new_chat_photo'] = PhotoSize.de_list(data.get('new_chat_photo'), bot)
        data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot)
        data['invoice'] = Invoice.de_json(data.get('invoice'), bot)
        data['successful_payment'] = SuccessfulPayment.de_json(data.get('successful_payment'), bot)

        return cls(bot=bot, **data)
class TestUserProfilePhotos(object):
    total_count = 2
    photos = [
        [
            PhotoSize('file_id1', 512, 512),
            PhotoSize('file_id2', 512, 512)
        ],
        [
            PhotoSize('file_id3', 512, 512),
            PhotoSize('file_id4', 512, 512)
        ]
    ]

    def test_de_json(self, bot):
        json_dict = {
            'total_count': 2,
            'photos': [[y.to_dict() for y in x] for x in self.photos]
        }
        user_profile_photos = UserProfilePhotos.de_json(json_dict, bot)
        assert user_profile_photos.total_count == self.total_count
        assert user_profile_photos.photos == self.photos

    def test_to_dict(self):
        user_profile_photos = UserProfilePhotos(self.total_count, self.photos)
        user_profile_photos_dict = user_profile_photos.to_dict()
        assert user_profile_photos_dict['total_count'] == user_profile_photos.total_count
        for ix, x in enumerate(user_profile_photos_dict['photos']):
            for iy, y in enumerate(x):
                assert y == user_profile_photos.photos[ix][iy].to_dict()
class TestUserProfilePhotos:
    total_count = 2
    photos = [
        [
            PhotoSize('file_id1', 'file_un_id1', 512, 512),
            PhotoSize('file_id2', 'file_un_id1', 512, 512),
        ],
        [
            PhotoSize('file_id3', 'file_un_id3', 512, 512),
            PhotoSize('file_id4', 'file_un_id4', 512, 512),
        ],
    ]

    def test_slot_behaviour(self, recwarn, mro_slots):
        inst = UserProfilePhotos(self.total_count, self.photos)
        for attr in inst.__slots__:
            assert getattr(inst, attr,
                           'err') != 'err', f"got extra slot '{attr}'"
        assert not inst.__dict__, f"got missing slot(s): {inst.__dict__}"
        assert len(mro_slots(inst)) == len(set(
            mro_slots(inst))), "duplicate slot"
        inst.custom, inst.total_count = 'should give warning', self.total_count
        assert len(recwarn) == 1 and 'custom' in str(
            recwarn[0].message), recwarn.list

    def test_de_json(self, bot):
        json_dict = {
            'total_count': 2,
            'photos': [[y.to_dict() for y in x] for x in self.photos]
        }
        user_profile_photos = UserProfilePhotos.de_json(json_dict, bot)
        assert user_profile_photos.total_count == self.total_count
        assert user_profile_photos.photos == self.photos

    def test_to_dict(self):
        user_profile_photos = UserProfilePhotos(self.total_count, self.photos)
        user_profile_photos_dict = user_profile_photos.to_dict()
        assert user_profile_photos_dict[
            'total_count'] == user_profile_photos.total_count
        for ix, x in enumerate(user_profile_photos_dict['photos']):
            for iy, y in enumerate(x):
                assert y == user_profile_photos.photos[ix][iy].to_dict()

    def test_equality(self):
        a = UserProfilePhotos(2, self.photos)
        b = UserProfilePhotos(2, self.photos)
        c = UserProfilePhotos(1, [self.photos[0]])
        d = PhotoSize('file_id1', 'unique_id', 512, 512)

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)
Exemple #9
0
    def test_equality(self, sticker):
        a = Sticker(
            sticker.file_id,
            sticker.file_unique_id,
            self.width,
            self.height,
            self.is_animated,
            self.is_video,
        )
        b = Sticker(
            '', sticker.file_unique_id, self.width, self.height, self.is_animated, self.is_video
        )
        c = Sticker(sticker.file_id, sticker.file_unique_id, 0, 0, False, True)
        d = Sticker('', '', self.width, self.height, self.is_animated, self.is_video)
        e = PhotoSize(
            sticker.file_id, sticker.file_unique_id, self.width, self.height, self.is_animated
        )

        assert a == b
        assert hash(a) == hash(b)
        assert a is not b

        assert a == c
        assert hash(a) == hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)
Exemple #10
0
def handle_preview(update: Update, context: CallbackContext):
    with StateBase() as sb:
        response = sb[update.message.from_user.id]
        data = response['data']
        markup = data['button']

        if data['button']:
            text, url = map(strip, markup.split('-'))
            button = InlineKeyboardButton(text, url=url)
            markup = InlineKeyboardMarkup([[button]])

        if data['photo']:
            context.bot.send_photo(chat_id=update.message.chat_id,
                                   photo=PhotoSize(*data['photo']),
                                   caption=data['text'],
                                   parse_mode='HTML',
                                   reply_markup=markup,
                                   disable_web_page_preview=True)

        elif data['text']:
            context.bot.send_message(chat_id=update.message.chat_id,
                                     text=data['text'],
                                     parse_mode='HTML',
                                     reply_markup=markup,
                                     disable_web_page_preview=True)

        else:
            with DataBase() as db:
                if lang := db.get_value(user_id=update.message.from_user.id,
                                        item='lang'):
                    context.bot.send_message(
                        chat_id=update.message.chat_id,
                        text=messages['no_mailing_data'][lang],
                        parse_mode='HTML')
    def test_pickle(self, bot):
        chat = Chat(2, Chat.PRIVATE)
        user = User(3, "first_name", False)
        date = datetime.datetime.now()
        photo = PhotoSize("file_id", "unique", 21, 21, bot=bot)
        msg = Message(1,
                      date,
                      chat,
                      from_user=user,
                      text="foobar",
                      bot=bot,
                      photo=[photo])

        # Test pickling of TGObjects, we choose Message since it's contains the most subclasses.
        assert msg.get_bot()
        unpickled = pickle.loads(pickle.dumps(msg))

        with pytest.raises(RuntimeError):
            unpickled.get_bot(
            )  # There should be no bot when we pickle TGObjects

        assert unpickled.chat == chat
        assert unpickled.from_user == user
        assert unpickled.date == date
        assert unpickled.photo[0] == photo
Exemple #12
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get_products('thumb'), bot)

        return cls(bot=bot, **data)
Exemple #13
0
    def de_json(data):
        """
        Args:
            data (str):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        if 'first_name' in data.get('chat', ''):
            data['chat'] = User.de_json(data.get('chat'))
        elif 'title' in data.get('chat', ''):
            data['chat'] = GroupChat.de_json(data.get('chat'))
        data['forward_from'] = \
            User.de_json(data.get('forward_from'))
        data['forward_date'] = \
            Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = \
            Message.de_json(data.get('reply_to_message'))
        data['audio'] = \
            Audio.de_json(data.get('audio'))
        data['document'] = \
            Document.de_json(data.get('document'))
        data['photo'] = \
            PhotoSize.de_list(data.get('photo'))
        data['sticker'] = \
            Sticker.de_json(data.get('sticker'))
        data['video'] = \
            Video.de_json(data.get('video'))
        data['voice'] = \
            Voice.de_json(data.get('voice'))
        data['contact'] = \
            Contact.de_json(data.get('contact'))
        data['location'] = \
            Location.de_json(data.get('location'))
        data['new_chat_participant'] = \
            User.de_json(data.get('new_chat_participant'))
        data['left_chat_participant'] = \
            User.de_json(data.get('left_chat_participant'))
        data['new_chat_photo'] = \
            PhotoSize.de_list(data.get('new_chat_photo'))

        return Message(**data)
Exemple #14
0
    def de_json(data):
        """
        Args:
            data (str):

        Returns:
            telegram.Message:
        """
        if not data:
            return None

        data['from_user'] = User.de_json(data.get('from'))
        data['date'] = datetime.fromtimestamp(data['date'])
        if 'first_name' in data.get('chat', ''):
            data['chat'] = User.de_json(data.get('chat'))
        elif 'title' in data.get('chat', ''):
            data['chat'] = GroupChat.de_json(data.get('chat'))
        data['forward_from'] = \
            User.de_json(data.get('forward_from'))
        data['forward_date'] = \
            Message._fromtimestamp(data.get('forward_date'))
        data['reply_to_message'] = \
            Message.de_json(data.get('reply_to_message'))
        data['audio'] = \
            Audio.de_json(data.get('audio'))
        data['document'] = \
            Document.de_json(data.get('document'))
        data['photo'] = \
            PhotoSize.de_list(data.get('photo'))
        data['sticker'] = \
            Sticker.de_json(data.get('sticker'))
        data['video'] = \
            Video.de_json(data.get('video'))
        data['voice'] = \
            Voice.de_json(data.get('voice'))
        data['contact'] = \
            Contact.de_json(data.get('contact'))
        data['location'] = \
            Location.de_json(data.get('location'))
        data['new_chat_participant'] = \
            User.de_json(data.get('new_chat_participant'))
        data['left_chat_participant'] = \
            User.de_json(data.get('left_chat_participant'))
        data['new_chat_photo'] = \
            PhotoSize.de_list(data.get('new_chat_photo'))

        return Message(**data)
Exemple #15
0
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['StickerSet']:
        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
        data['stickers'] = Sticker.de_list(data.get('stickers'), bot)

        return cls(bot=bot, **data)
class TestUserProfilePhotos:
    total_count = 2
    photos = [
        [
            PhotoSize('file_id1', 'file_un_id1', 512, 512),
            PhotoSize('file_id2', 'file_un_id1', 512, 512),
        ],
        [
            PhotoSize('file_id3', 'file_un_id3', 512, 512),
            PhotoSize('file_id4', 'file_un_id4', 512, 512),
        ],
    ]

    def test_de_json(self, bot):
        json_dict = {
            'total_count': 2,
            'photos': [[y.to_dict() for y in x] for x in self.photos]
        }
        user_profile_photos = UserProfilePhotos.de_json(json_dict, bot)
        assert user_profile_photos.total_count == self.total_count
        assert user_profile_photos.photos == self.photos

    def test_to_dict(self):
        user_profile_photos = UserProfilePhotos(self.total_count, self.photos)
        user_profile_photos_dict = user_profile_photos.to_dict()
        assert user_profile_photos_dict[
            'total_count'] == user_profile_photos.total_count
        for ix, x in enumerate(user_profile_photos_dict['photos']):
            for iy, y in enumerate(x):
                assert y == user_profile_photos.photos[ix][iy].to_dict()

    def test_equality(self):
        a = UserProfilePhotos(2, self.photos)
        b = UserProfilePhotos(2, self.photos)
        c = UserProfilePhotos(1, [self.photos[0]])
        d = PhotoSize('file_id1', 'unique_id', 512, 512)

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)
Exemple #17
0
 def _get_photosize(self):
     tmp = []
     import uuid
     from random import randint
     for _ in range(2):
         w, h = randint(40, 400), randint(40, 400)
         s = w * h * 0.3
         tmp.append(PhotoSize(str(uuid.uuid4()), w, h, file_size=s))
     return tmp
Exemple #18
0
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Video']:
        data = cls.parse_data(data)

        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(bot=bot, **data)
Exemple #19
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(VideoNote, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(bot=bot, **data)
Exemple #20
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Animation, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(**data)
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(UserProfilePhotos, cls).de_json(data, bot)

        data['photos'] = [PhotoSize.de_list(photo, bot) for photo in data['photos']]

        return cls(**data)
Exemple #22
0
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['StickerSet']:
        """See :meth:`telegram.TelegramObject.de_json`."""
        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
        data['stickers'] = Sticker.de_list(data.get('stickers'), bot)

        return cls(bot=bot, **data)
Exemple #23
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(VideoNote, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(**data)
Exemple #24
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Document, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(bot=bot, **data)
Exemple #25
0
    def test_equality(self, photo):
        a = PhotoSize(photo.file_id, photo.file_unique_id, self.width, self.height)
        b = PhotoSize('', photo.file_unique_id, self.width, self.height)
        c = PhotoSize(photo.file_id, photo.file_unique_id, 0, 0)
        d = PhotoSize('', '', self.width, self.height)
        e = Sticker(photo.file_id, photo.file_unique_id, self.width, self.height, False)

        assert a == b
        assert hash(a) == hash(b)
        assert a is not b

        assert a == c
        assert hash(a) == hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super().de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
        data['stickers'] = Sticker.de_list(data.get('stickers'), bot)

        return cls(bot=bot, **data)
Exemple #27
0
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Sticker']:
        data = cls.parse_data(data)

        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
        data['mask_position'] = MaskPosition.de_json(data.get('mask_position'), bot)

        return cls(bot=bot, **data)
Exemple #28
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Sticker, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
        data['mask_position'] = MaskPosition.de_json(data.get('mask_position'), bot)

        return cls(**data)
Exemple #29
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Sticker, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
        data['mask_position'] = MaskPosition.de_json(data.get('mask_position'), bot)

        return cls(**data)
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['UserProfilePhotos']:
        """See :meth:`telegram.TelegramObject.de_json`."""
        data = cls._parse_data(data)

        if not data:
            return None

        data['photos'] = [PhotoSize.de_list(photo, bot) for photo in data['photos']]

        return cls(**data)
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Document']:
        """See :meth:`telegram.TelegramObject.de_json`."""
        data = cls._parse_data(data)

        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(bot=bot, **data)
    def de_json(data):
        if 'photos' in data:
            from telegram import PhotoSize
            photos = []
            for photo in data['photos']:
                photos.append([PhotoSize.de_json(x) for x in photo])
        else:
            photos = None

        return UserProfilePhotos(total_count=data.get('total_count', None),
                                 photos=photos)
Exemple #33
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Game, cls).de_json(data, bot)

        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['text_entities'] = MessageEntity.de_list(data.get('text_entities'), bot)
        data['animation'] = Animation.de_json(data.get('animation'), bot)

        return cls(**data)
Exemple #34
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Game, cls).de_json(data, bot)

        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['text_entities'] = MessageEntity.de_list(data.get('text_entities'), bot)
        data['animation'] = Animation.de_json(data.get('animation'), bot)

        return cls(**data)
    def de_json(data):
        if 'photos' in data:
            from telegram import PhotoSize
            photos = []
            for photo in data['photos']:
                photos.append([PhotoSize.de_json(x) for x in photo])
        else:
            photos = None

        return UserProfilePhotos(total_count=data.get('total_count', None),
                                 photos=photos)
Exemple #36
0
    def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Game']:
        data = cls.parse_data(data)

        if not data:
            return None

        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['text_entities'] = MessageEntity.de_list(data.get('text_entities'), bot)
        data['animation'] = Animation.de_json(data.get('animation'), bot)

        return cls(**data)
Exemple #37
0
    def de_json(data):
        if 'thumb' in data:
            from telegram import PhotoSize
            thumb = PhotoSize.de_json(data['thumb'])
        else:
            thumb = None

        return Document(file_id=data.get('file_id', None),
                        thumb=thumb,
                        file_name=data.get('file_name', None),
                        mime_type=data.get('mime_type', None),
                        file_size=data.get('file_size', None))
Exemple #38
0
    def de_json(data):
        if 'thumb' in data:
            from telegram import PhotoSize
            thumb = PhotoSize.de_json(data['thumb'])
        else:
            thumb = None

        return Sticker(file_id=data.get('file_id', None),
                       width=data.get('width', None),
                       height=data.get('height', None),
                       thumb=thumb,
                       file_size=data.get('file_size', None))
Exemple #39
0
    def de_json(data):
        if 'thumb' in data:
            from telegram import PhotoSize
            thumb = PhotoSize.de_json(data['thumb'])
        else:
            thumb = None

        return Sticker(file_id=data.get('file_id', None),
                       width=data.get('width', None),
                       height=data.get('height', None),
                       thumb=thumb,
                       file_size=data.get('file_size', None))
    def test_de_json(self, bot, photo):
        json_dict = {
            'file_id': photo.file_id,
            'width': self.width,
            'height': self.height,
            'file_size': self.file_size
        }
        json_photo = PhotoSize.de_json(json_dict, bot)

        assert json_photo.file_id == photo.file_id
        assert json_photo.width == self.width
        assert json_photo.height == self.height
        assert json_photo.file_size == self.file_size
Exemple #41
0
    def de_json(data):
        if 'thumb' in data:
            from telegram import PhotoSize
            thumb = PhotoSize.de_json(data['thumb'])
        else:
            thumb = None

        return Video(file_id=data.get('file_id', None),
                     width=data.get('width', None),
                     height=data.get('height', None),
                     duration=data.get('duration', None),
                     thumb=thumb,
                     mime_type=data.get('mime_type', None),
                     file_size=data.get('file_size', None))
    def de_json(data):
        """
        Args:
            data (str):

        Returns:
            telegram.Video:
        """
        if not data:
            return None

        data["thumb"] = PhotoSize.de_json(data.get("thumb"))

        return Video(**data)
Exemple #43
0
    def de_json(data):
        """
        Args:
            data (str):

        Returns:
            telegram.Document:
        """
        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'))

        return Document(**data)
    def de_json(data):
        """
        Args:
            data (str):

        Returns:
            telegram.UserProfilePhotos:
        """
        if not data:
            return None

        data['photos'] = [PhotoSize.de_list(photo) for photo in data['photos']]

        return UserProfilePhotos(**data)
Exemple #45
0
    def de_json(data):
        """
        Args:
            data (str):

        Returns:
            telegram.Sticker:
        """
        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'))

        return Sticker(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Sticker:
        """
        if not data:
            return None

        data["thumb"] = PhotoSize.de_json(data.get("thumb"), bot)

        return Sticker(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Video:
        """
        if not data:
            return None

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return Video(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Game:
        """
        if not data:
            return None

        data = super(Animation, Animation).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return Animation(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Document:
        """
        if not data:
            return None

        data = super(Document, Document).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return Document(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.UserProfilePhotos:
        """
        if not data:
            return None

        data = super(UserProfilePhotos, UserProfilePhotos).de_json(data, bot)

        data['photos'] = [PhotoSize.de_list(photo, bot) for photo in data['photos']]

        return UserProfilePhotos(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Sticker:
        """
        if not data:
            return None

        data = super(Sticker, Sticker).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return Sticker(**data)
Exemple #52
0
    def de_json(data):
        if "thumb" in data:
            from telegram import PhotoSize

            thumb = PhotoSize.de_json(data["thumb"])
        else:
            thumb = None

        return Video(
            file_id=data.get("file_id", None),
            width=data.get("width", None),
            height=data.get("height", None),
            duration=data.get("duration", None),
            thumb=thumb,
            mime_type=data.get("mime_type", None),
            file_size=data.get("file_size", None),
            caption=data.get("caption", None),
        )
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Game:

        """
        if not data:
            return None

        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['text_entities'] = MessageEntity.de_list(data.get('text_entities'), bot)
        data['animation'] = Animation.de_json(data.get('animation'), bot)

        return Game(**data)
Exemple #54
0
    def de_json(data):
        if 'from' in data:  # from is a reserved word, use from_user instead.
            from telegram import User
            from_user = User.de_json(data['from'])
        else:
            from_user = None

        if 'date' in data:
            date = datetime.fromtimestamp(data['date'])
        else:
            date = None

        if 'chat' in data:
            if 'first_name' in data['chat']:
                from telegram import User
                chat = User.de_json(data['chat'])
            if 'title' in data['chat']:
                from telegram import GroupChat
                chat = GroupChat.de_json(data['chat'])
        else:
            chat = None

        if 'forward_from' in data:
            from telegram import User
            forward_from = User.de_json(data['forward_from'])
        else:
            forward_from = None

        if 'forward_date' in data:
            forward_date = datetime.fromtimestamp(data['forward_date'])
        else:
            forward_date = None

        if 'reply_to_message' in data:
            reply_to_message = Message.de_json(data['reply_to_message'])
        else:
            reply_to_message = None

        if 'audio' in data:
            from telegram import Audio
            audio = Audio.de_json(data['audio'])
        else:
            audio = None

        if 'document' in data:
            from telegram import Document
            document = Document.de_json(data['document'])
        else:
            document = None

        if 'photo' in data:
            from telegram import PhotoSize
            photo = [PhotoSize.de_json(x) for x in data['photo']]
        else:
            photo = None

        if 'sticker' in data:
            from telegram import Sticker
            sticker = Sticker.de_json(data['sticker'])
        else:
            sticker = None

        if 'video' in data:
            from telegram import Video
            video = Video.de_json(data['video'])
        else:
            video = None

        if 'voice' in data:
            from telegram import Voice
            voice = Voice.de_json(data['voice'])
        else:
            voice = None

        if 'contact' in data:
            from telegram import Contact
            contact = Contact.de_json(data['contact'])
        else:
            contact = None

        if 'location' in data:
            from telegram import Location
            location = Location.de_json(data['location'])
        else:
            location = None

        if 'new_chat_participant' in data:
            from telegram import User
            new_chat_participant = User.de_json(data['new_chat_participant'])
        else:
            new_chat_participant = None

        if 'left_chat_participant' in data:
            from telegram import User
            left_chat_participant = User.de_json(data['left_chat_participant'])
        else:
            left_chat_participant = None

        if 'new_chat_photo' in data:
            from telegram import PhotoSize
            new_chat_photo = \
                [PhotoSize.de_json(x) for x in data['new_chat_photo']]
        else:
            new_chat_photo = None

        return Message(message_id=data.get('message_id', None),
                       from_user=from_user,
                       date=date,
                       chat=chat,
                       forward_from=forward_from,
                       forward_date=forward_date,
                       reply_to_message=reply_to_message,
                       text=data.get('text', ''),
                       audio=audio,
                       document=document,
                       photo=photo,
                       sticker=sticker,
                       video=video,
                       voice=voice,
                       caption=data.get('caption', ''),
                       contact=contact,
                       location=location,
                       new_chat_participant=new_chat_participant,
                       left_chat_participant=left_chat_participant,
                       new_chat_title=data.get('new_chat_title', None),
                       new_chat_photo=new_chat_photo,
                       delete_chat_photo=data.get('delete_chat_photo', None),
                       group_chat_created=data.get('group_chat_created', None))