예제 #1
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)
예제 #2
0
    def test_equality(self):
        a = StickerSet(
            self.name,
            self.title,
            self.is_animated,
            self.contains_masks,
            self.stickers,
            self.is_video,
        )
        b = StickerSet(
            self.name,
            self.title,
            self.is_animated,
            self.contains_masks,
            self.stickers,
            self.is_video,
        )
        c = StickerSet(self.name, None, None, None, None, None)
        d = StickerSet(
            'blah', self.title, self.is_animated, self.contains_masks, self.stickers, self.is_video
        )
        e = Audio(self.name, '', 0, None, None)

        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)
예제 #3
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)
예제 #4
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_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)
예제 #5
0
    def test_equality(self, audio):
        a = Audio(audio.file_id, audio.file_unique_id, audio.duration)
        b = Audio('', audio.file_unique_id, audio.duration)
        c = Audio(audio.file_id, audio.file_unique_id, 0)
        d = Audio('', '', audio.duration)
        e = Voice(audio.file_id, audio.file_unique_id, audio.duration)

        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 _get_audio(self):
        import uuid
        from random import randint

        return Audio(
            file_id=str(uuid.uuid4()),
            file_unique_id=str(uuid.uuid4()),
            duration=randint(1, 120),
            title="Some song",
        )
예제 #7
0
    def test_audio(self):
        audio = Audio("idyouknow", 23)
        u = self.mg.get_message(audio=audio)
        self.assertEqual(audio.file_id, u.message.audio.file_id)

        cap = "audio file"
        u = self.mg.get_message(audio=audio, caption=cap)
        self.assertEqual(u.message.caption, cap)

        u = self.mg.get_message(audio=True)
        self.assertIsInstance(u.message.audio, Audio)

        with self.assertRaisesRegexp(BadMessageException, r"telegram\.Audio"):
            self.mg.get_message(audio="audio")
예제 #8
0
    def test_de_json(self, bot):
        json_dict = {'file_id': 'not a file id',
                     'duration': self.duration,
                     'performer': self.performer,
                     'title': self.title,
                     'caption': self.caption,
                     'mime_type': self.mime_type,
                     'file_size': self.file_size}
        json_audio = Audio.de_json(json_dict, bot)

        assert json_audio.file_id == 'not a file id'
        assert json_audio.duration == self.duration
        assert json_audio.performer == self.performer
        assert json_audio.title == self.title
        assert json_audio.mime_type == self.mime_type
        assert json_audio.file_size == self.file_size
예제 #9
0
    def test_de_json(self, bot):
        json_dict = {'file_id': 'not a file id',
                     'duration': self.duration,
                     'performer': self.performer,
                     'title': self.title,
                     'caption': self.caption,
                     'mime_type': self.mime_type,
                     'file_size': self.file_size}
        json_audio = Audio.de_json(json_dict, bot)

        assert json_audio.file_id == 'not a file id'
        assert json_audio.duration == self.duration
        assert json_audio.performer == self.performer
        assert json_audio.title == self.title
        assert json_audio.mime_type == self.mime_type
        assert json_audio.file_size == self.file_size
예제 #10
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)
예제 #11
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)
예제 #12
0
    def test_equality(self):
        a = CallbackQuery(self.id_, self.from_user, "chat")
        b = CallbackQuery(self.id_, self.from_user, "chat")
        c = CallbackQuery(self.id_, None, "")
        d = CallbackQuery("", None, "chat")
        e = Audio(self.id_, "unique_id", 1)

        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)
예제 #13
0
    def test_equality(self):
        a = MaskPosition(self.point, self.x_shift, self.y_shift, self.scale)
        b = MaskPosition(self.point, self.x_shift, self.y_shift, self.scale)
        c = MaskPosition(MaskPosition.FOREHEAD, self.x_shift, self.y_shift, self.scale)
        d = MaskPosition(self.point, 0, 0, self.scale)
        e = Audio('', '', 0, None, None)

        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)
예제 #14
0
    def test_equality(self, voice):
        a = Voice(voice.file_id, voice.file_unique_id, self.duration)
        b = Voice('', voice.file_unique_id, self.duration)
        c = Voice(voice.file_id, voice.file_unique_id, 0)
        d = Voice('', '', self.duration)
        e = Audio(voice.file_id, voice.file_unique_id, self.duration)

        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 test_equality(self):
        a = CallbackQuery(self.id_, self.from_user, 'chat')
        b = CallbackQuery(self.id_, self.from_user, 'chat')
        c = CallbackQuery(self.id_, None, '')
        d = CallbackQuery('', None, 'chat')
        e = Audio(self.id_, 'unique_id', 1)

        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)
예제 #16
0
    def test_de_json(self, bot, audio):
        json_dict = {
            'file_id': self.audio_file_id,
            'file_unique_id': self.audio_file_unique_id,
            'duration': self.duration,
            'performer': self.performer,
            'title': self.title,
            'caption': self.caption,
            'mime_type': self.mime_type,
            'file_size': self.file_size,
            'thumb': audio.thumb.to_dict()
        }
        json_audio = Audio.de_json(json_dict, bot)

        assert json_audio.file_id == self.audio_file_id
        assert json_audio.file_unique_id == self.audio_file_unique_id
        assert json_audio.duration == self.duration
        assert json_audio.performer == self.performer
        assert json_audio.title == self.title
        assert json_audio.mime_type == self.mime_type
        assert json_audio.file_size == self.file_size
        assert json_audio.thumb == audio.thumb
예제 #17
0
def buttonCallback(update, context):
    query = update.callback_query
    chatid = update.effective_chat.id
    user = update.effective_user
    if not str(user.id) == '1360440667':
        update.message.reply_text("Your not Noah!")
        return
    if query.data == 'f:audio':
        query.answer("Audio ready!")
        audio = Audio(
            "CQACAgEAAxkBAAINiWAaBHFyvXmrVCE4t1LNi0rJEgj8AAI-AQAC3tnRRBN0iOaGz4KqHgQ",
            "AgADPgEAAt7Z0UQ", "230", "None", "None", "audio/mpeg3", "3685063")
        query.edit_message_media(audio, reply_markup=kb)
    elif query.data == 'f:forward':
        msg.pop(0)
        for k in msg:
            query.edit_message_text(k, reply_markup=kb)
            break
    elif query.data == 'f:skip':
        query.edit_message_text(
            "Question: Y a-t-il un ou une humoriste que tu aimes particulièrement?",
            reply_markup=kb)
    def test_de_json(self, bot, audio):
        json_dict = {
            "file_id": self.audio_file_id,
            "file_unique_id": self.audio_file_unique_id,
            "duration": self.duration,
            "performer": self.performer,
            "title": self.title,
            "file_name": self.file_name,
            "caption": self.caption,
            "mime_type": self.mime_type,
            "file_size": self.file_size,
            "thumb": audio.thumb.to_dict(),
        }
        json_audio = Audio.de_json(json_dict, bot)

        assert json_audio.file_id == self.audio_file_id
        assert json_audio.file_unique_id == self.audio_file_unique_id
        assert json_audio.duration == self.duration
        assert json_audio.performer == self.performer
        assert json_audio.title == self.title
        assert json_audio.file_name == self.file_name
        assert json_audio.mime_type == self.mime_type
        assert json_audio.file_size == self.file_size
        assert json_audio.thumb == audio.thumb
예제 #19
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))
예제 #20
0
 {
     'text':
     'a text message',
     'enitites':
     [MessageEntity('bold', 10, 4),
      MessageEntity('italic', 16, 7)],
 },
 {
     'caption':
     'A message caption',
     'caption_entities':
     [MessageEntity('bold', 1, 1),
      MessageEntity('text_link', 4, 3)],
 },
 {
     'audio': Audio('audio_id', 'unique_id', 12),
     'caption': 'audio_file'
 },
 {
     'document': Document('document_id', 'unique_id'),
     'caption': 'document_file'
 },
 {
     'animation': Animation('animation_id', 'unique_id', 30, 30, 1),
     'caption': 'animation_file',
 },
 {
     'game':
     Game(
         'my_game',
         'just my game',
예제 #21
0
import pytest

from telegram import User, Chat, Video, Audio, Message


@pytest.fixture(scope="function",
                params=[{
                    'video': Video("my_id", 12, 12, 12)
                }, {
                    'audio': Audio("audio_id", 12)
                }],
                ids=['video', 'audio'])
def tester(request):
    return Message(1, User(1, "aa"), None, Chat(2, 'private'), **request.param)


def test_something(tester, request):
    print(request.param)
    test = "try it"
    assert -0
예제 #22
0
 def _get_audio(self):
     import uuid
     from random import randint
     return Audio(str(uuid.uuid4()), randint(1, 120), title="Some song")
예제 #23
0
def read_file(update, context):
    if update.message.reply_to_message:
        if update.message.reply_to_message.animation:
            file_id = str(update.message.reply_to_message.animation.file_id)
            unique_id = str(
                update.message.reply_to_message.animation.file_unique_id)
            width = str(update.message.reply_to_message.animation.width)
            height = str(update.message.reply_to_message.animation.height)
            duration = str(update.message.reply_to_message.animation.duration)
            # context.bot.send_message(update.effective_user.id,text=f"file_id = '{file_id}', \nfile_unique_id = '{unique_id}', \nwidth = {width}, \nheight = {height}, \nduration = {duration}")
            animation = Animation(file_id, unique_id, width, height, duration)
            update.message.reply_animation(
                animation,
                caption=
                f"This is an animation. \n\n\nfile_id = '{file_id}', \n\nfile_unique_id = '{unique_id}', \n\nwidth = {width}, \n\nheight = {height}, \n\nduration = {duration}"
            )
        if update.message.reply_to_message.photo:
            file_id = str(update.message.reply_to_message.photo[2]['file_id'])
            unique_id = str(
                update.message.reply_to_message.photo[2]['file_unique_id'])
            width = str(update.message.reply_to_message.photo[2]['width'])
            height = str(update.message.reply_to_message.photo[2]['height'])
            file_size = str(
                update.message.reply_to_message.photo[2]['file_size'])
            photo = PhotoSize(file_id, unique_id, width, height, file_size)
            update.message.reply_photo(
                photo,
                caption=
                f"This is a photo. \n\n\nfile_id = '{file_id}', \n\nfile_unique_id = '{unique_id}', \n\nwidth = {width},\n\nheight = {height},\n\nfile_size = {file_size}\n\n"
            )
        if update.message.reply_to_message.voice:
            file_id = str(update.message.reply_to_message.voice.file_id)
            unique_id = str(
                update.message.reply_to_message.voice.file_unique_id)
            duration = str(update.message.reply_to_message.voice.duration)
            mime_type = str(update.message.reply_to_message.voice.mime_type)
            file_size = str(update.message.reply_to_message.voice.file_size)
            voice = Voice(file_id, unique_id, duration, mime_type, file_size)
            update.message.reply_voice(
                voice,
                caption=
                f"This is a voice message. \n\n\nfile_id = '{file_id}', \n\nfile_unique_id = '{unique_id}', \n\nduration = {duration},\n\nmime_type = {mime_type},\n\nfile_size = {file_size}\n\n"
            )
        if update.message.reply_to_message.audio:
            file_id = str(update.message.reply_to_message.audio.file_id)
            unique_id = str(
                update.message.reply_to_message.audio.file_unique_id)
            duration = str(update.message.reply_to_message.audio.duration)
            performer = str(update.message.reply_to_message.audio.performer)
            title = str(update.message.reply_to_message.audio.title)
            mime_type = str(update.message.reply_to_message.audio.mime_type)
            file_size = str(update.message.reply_to_message.audio.file_size)
            audio = Audio(file_id, unique_id, duration, performer, title,
                          mime_type, file_size)
            update.message.reply_audio(
                audio,
                caption=
                f"This is an audio. \n\n\nfile_id = '{file_id}', \n\nfile_unique_id = '{unique_id}', \n\nduration = {duration},\n\nperformer = {performer} \n\ntitle = {title}, \n\nmime_type = {mime_type},\n\nfile_size = {file_size}"
            )
        if update.message.reply_to_message.document:
            file_id = str(update.message.reply_to_message.document.file_id)
            unique_id = str(
                update.message.reply_to_message.document.file_unique_id)
            document = Document(file_id, unique_id)
            update.message.reply_document(
                document,
                caption=
                f"This is a document. \n\n\nfile_id = '{file_id}',\n\nfile_unique_id = '{unique_id}'"
            )
        if update.message.reply_to_message.video:
            file_id = str(update.message.reply_to_message.video.file_id)
            unique_id = str(
                update.message.reply_to_message.video.file_unique_id)
            width = str(update.message.reply_to_message.video.width)
            height = str(update.message.reply_to_message.video.height)
            duration = str(update.message.reply_to_message.video.duration)
            video = Video(file_id, unique_id, width, height, duration)
            update.message.reply_video(
                video,
                caption=
                f"This is a video. \n\n\nfile_id = '{file_id}',\n\nfile_unique_id = '{unique_id}', \n\nwidth = {width}, \n\nheight = {height},\n\nduration = {duration}"
            )
        if update.message.reply_to_message.sticker:
            file_id = str(update.message.reply_to_message.sticker.file_id)
            unique_id = str(
                update.message.reply_to_message.sticker.file_unique_id)
            width = str(update.message.reply_to_message.sticker.width)
            height = str(update.message.reply_to_message.sticker.height)
            is_animated = str(
                update.message.reply_to_message.sticker.is_animated)
            sticker = Sticker(file_id, unique_id, width, height, is_animated)
            update.message.reply_sticker(sticker, caption='')
            context.bot.send_message(
                update.effective_chat.id,
                text=
                f"This is a sticker. \n\n\nfile_id = '{file_id}',\n\nfile_unique_id = '{unique_id}', \n\nwidth = {width}, \n\nheight = {height},\n\nis_animated = {is_animated}"
            )
        if update.message.reply_to_message.game:
            title = str(update.message.reply_to_message.game.title)
            description = str(update.message.reply_to_message.game.description)
            photo = str(update.message.reply_to_message.game.photo)
            text = str(update.message.reply_to_message.game.text)
            text_entities = str(
                update.message.reply_to_message.game.text_entities)
            animation = str(update.message.reply_to_message.game.animation)
            game = Game(title, description, photo, text, text_entities,
                        animation)
            update.message.reply_game(
                game,
                caption=
                f'This is a game.\n\n\ntitle = {title}, \n\ndescription = {description}, \n\nphoto = {photo}, \n\ntext = {text}, \n\ntext_entities = {text_entities}, \n\nanimation = {animation}'
            )
        if update.message.reply_to_message.video_note:
            file_id = str(update.message.reply_to_message.video_note.file_id)
            unique_id = str(
                update.message.reply_to_message.video_note.file_unique_id)
            length = str(update.message.reply_to_message.video_note.length)
            duration = str(update.message.reply_to_message.video_note.duration)
            videoNote = VideoNote(file_id, unique_id, length, duration)
            update.message.reply_video_note(videoNote, caption='')
            context.bot.send_message(
                update.effective_chat.id,
                text=
                f"This is a video message.\n\n\nfile_id = '{file_id}',\n\nfile_unique_id = '{unique_id}',\n\nlength = {length}, \n\nduration = {duration}"
            )
        if update.message.reply_to_message.text:
            ttext = str(update.message.reply_to_message.text)
            update.message.reply_text(f'{ttext}\n\nThis is a text.')
    else:
        u = str(update)
        u = dumps(eval(u), indent=2)
        update.message.reply_text(u)
예제 #24
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 '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 'reply_to_message' in data:
            reply_to_message = Message.de_json(data['reply_to_message'])
        else:
            reply_to_message = None

        if 'text' in data:
            text = data['text']
        else:
            text = 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 '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

        return Message(message_id=data.get('message_id', None),
                       from_user=from_user,
                       date=data.get('date', None),
                       chat=chat,
                       forward_from=forward_from,
                       forward_date=data.get('forward_date', None),
                       reply_to_message=reply_to_message,
                       text=text,
                       audio=audio,
                       document=document,
                       photo=photo,
                       sticker=sticker,
                       video=video,
                       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=data.get('new_chat_photo', None),
                       delete_chat_photo=data.get('delete_chat_photo', None),
                       group_chat_created=data.get('group_chat_created', None))
예제 #25
0
 }, {
     'edit_date': datetime.now()
 }, {
     'text':
     'a text message',
     'enitites':
     [MessageEntity('bold', 10, 4),
      MessageEntity('italic', 16, 7)]
 }, {
     'caption':
     'A message caption',
     'caption_entities':
     [MessageEntity('bold', 1, 1),
      MessageEntity('text_link', 4, 3)]
 }, {
     'audio': Audio('audio_id', 12),
     'caption': 'audio_file'
 }, {
     'document': Document('document_id'),
     'caption': 'document_file'
 }, {
     'animation': Animation('animation_id', 30, 30, 1),
     'caption': 'animation_file'
 }, {
     'game':
     Game('my_game', 'just my game', [
         PhotoSize('game_photo_id', 30, 30),
     ])
 }, {
     'photo': [PhotoSize('photo_id', 50, 50)],
     'caption': 'photo_file'
 def _handle_attachments(
         self,
         audio,
         contact,
         document,
         location,
         photo,
         sticker,
         user,
         venue,
         video,
         voice,
         caption,
 ):
     attachments = [
         x
         for x in [
             photo,
             venue,
             location,
             contact,
             voice,
             video,
             sticker,
             document,
             audio,
         ]
         if x
     ]
     if caption and not attachments:
         raise BadMessageException("Can't have a caption without attachment")
     if len(attachments) > 1:
         raise BadMessageException("can't add more than one attachment")
     if photo:
         if isinstance(photo, list):
             if all([isinstance(x, PhotoSize) for x in photo]):
                 pass
             else:
                 raise BadMessageException(
                     "photo must either be True or list(telegram.PhotoSize)"
                 )
         elif isinstance(photo, bool):
             photo = self._get_photosize()
         else:
             raise BadMessageException(
                 "photo must either be True or list(telegram.PhotoSize)"
             )
     if location:
         if isinstance(location, Location):
             pass
         elif isinstance(location, dict):
             location = Location(**location)
         elif isinstance(location, bool):
             location = self._get_location()
         else:
             raise BadMessageException(
                 "location must either be True or telegram.Location"
             )
     if venue:
         if isinstance(venue, Venue):
             pass
         elif isinstance(venue, bool):
             venue = self._get_venue()
         elif isinstance(venue, dict):
             venue["location"] = Location(**venue)
             venue = Venue(**venue)
         else:
             raise BadMessageException("venue must either be True or telegram.Venue")
     if contact:
         if isinstance(contact, Contact):
             pass
         elif isinstance(contact, dict):
             contact = Contact(**contact)
         elif isinstance(contact, bool):
             contact = self._get_contact(user)
         else:
             raise BadMessageException(
                 "contact must either be True or telegram.Contact"
             )
     if voice:
         if isinstance(voice, Voice):
             pass
         elif isinstance(voice, bool):
             voice = self._get_voice()
         elif isinstance(voice, dict):
             voice = Voice(**voice)
         else:
             raise BadMessageException("voice must either be True or telegram.Voice")
     if video:
         if isinstance(video, Video):
             pass
         elif isinstance(video, bool):
             video = self._get_video()
         elif isinstance(video, dict):
             video = self._get_video(data=video)
         else:
             raise BadMessageException("video must either be True or telegram.Video")
     if sticker:
         if isinstance(sticker, Sticker):
             pass
         elif isinstance(sticker, bool):
             sticker = self._get_sticker()
         elif isinstance(sticker, dict):
             sticker = self._get_sticker(sticker)
         else:
             raise BadMessageException(
                 "sticker must either be True or telegram.Sticker"
             )
     if document:
         if isinstance(document, Document):
             pass
         elif isinstance(document, dict):
             document = Document(**document)
         elif isinstance(document, bool):
             document = self._get_document()
         else:
             raise BadMessageException(
                 "document must either be True or telegram.Document"
             )
     if audio:
         if isinstance(audio, Audio):
             pass
         elif isinstance(audio, bool):
             audio = self._get_audio()
         elif isinstance(audio, dict):
             audio = Audio(**audio)
         else:
             raise BadMessageException("audio must either be True or telegram.Audio")
     return audio, contact, document, location, photo, sticker, venue, video, voice