예제 #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 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)
예제 #3
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)
예제 #4
0
    def test_de_json(self, bot):
        json_dict = {
            'file_id': 'not a file id',
            'width': self.width,
            'height': self.height,
            'duration': self.duration,
            'mime_type': self.mime_type,
            'file_size': self.file_size
        }
        json_video = Video.de_json(json_dict, bot)

        assert json_video.file_id == 'not a file id'
        assert json_video.width == self.width
        assert json_video.height == self.height
        assert json_video.duration == self.duration
        assert json_video.mime_type == self.mime_type
        assert json_video.file_size == self.file_size
예제 #5
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))
예제 #6
0
               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',
               [PhotoSize('game_photo_id', 'unique_id', 30, 30), ])},
 {'photo': [PhotoSize('photo_id', 'unique_id', 50, 50)],
  'caption': 'photo_file'},
 {'sticker': Sticker('sticker_id', 'unique_id', 50, 50, True)},
 {'video': Video('video_id', 'unique_id', 12, 12, 12),
  'caption': 'video_file'},
 {'voice': Voice('voice_id', 'unique_id', 5)},
 {'video_note': VideoNote('video_note_id', 'unique_id', 20, 12)},
 {'new_chat_members': [User(55, 'new_user', False)]},
 {'contact': Contact('phone_numner', 'contact_name')},
 {'location': Location(-23.691288, 46.788279)},
 {'venue': Venue(Location(-23.691288, 46.788279),
                 'some place', 'right here')},
 {'left_chat_member': User(33, 'kicked', False)},
 {'new_chat_title': 'new title'},
 {'new_chat_photo': [PhotoSize('photo_id', 'unique_id', 50, 50)]},
 {'delete_chat_photo': True},
 {'group_chat_created': True},
 {'supergroup_chat_created': True},
 {'channel_chat_created': True},
예제 #7
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
예제 #8
0
  'forward_date': datetime.now()},
 {'reply_to_message': Message(50, None, None, None)},
 {'edit_date': datetime.now()},
 {'test': 'a text message',
  'enitites': [MessageEntity('bold', 10, 4),
               MessageEntity('italic', 16, 7)]},
 {'audio': Audio('audio_id', 12),
  'caption': 'audio_file'},
 {'document': Document('document_id'),
  'caption': 'document_file'},
 {'game': Game('my_game', 'just my game',
               [PhotoSize('game_photo_id', 30, 30), ])},
 {'photo': [PhotoSize('photo_id', 50, 50)],
  'caption': 'photo_file'},
 {'sticker': Sticker('sticker_id', 50, 50)},
 {'video': Video('video_id', 12, 12, 12),
  'caption': 'video_file'},
 {'voice': Voice('voice_id', 5)},
 {'video_note': VideoNote('video_note_id', 20, 12)},
 {'new_chat_members': [User(55, 'new_user', False)]},
 {'contact': Contact('phone_numner', 'contact_name')},
 {'location': Location(-23.691288, 46.788279)},
 {'venue': Venue(Location(-23.691288, 46.788279),
                 'some place', 'right here')},
 {'left_chat_member': User(33, 'kicked', False)},
 {'new_chat_title': 'new title'},
 {'new_chat_photo': [PhotoSize('photo_id', 50, 50)]},
 {'delete_chat_photo': True},
 {'group_chat_created': True},
 {'supergroup_chat_created': True},
 {'channel_chat_created': True},
예제 #9
0
파일: message.py 프로젝트: m0rg/dungeon_bot
    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 '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=data.get('text', None),
                       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))
예제 #10
0
  'forward_date': datetime.now()},
 {'reply_to_message': Message(50, None, None, None)},
 {'edit_date': datetime.now()},
 {'test': 'a text message',
  'enitites': [MessageEntity('bold', 10, 4),
               MessageEntity('italic', 16, 7)]},
 {'audio': Audio("audio_id", 12),
  'caption': 'audio_file'},
 {'document': Document('document_id'),
  'caption': 'document_file'},
 {'game': Game('my_game', 'just my game',
               [PhotoSize('game_photo_id', 30, 30), ])},
 {'photo': [PhotoSize('photo_id', 50, 50)],
  'caption': 'photo_file'},
 {'sticker': Sticker('sticker_id', 50, 50)},
 {'video': Video("video_id", 12, 12, 12),
  'caption': 'video_file'},
 {'voice': Voice('voice_id', 5)},
 {'video_note': VideoNote('video_note_id', 20, 12)},
 {'new_chat_members': [User(55, 'new_user')]},
 {'contact': Contact('phone_numner', 'contact_name')},
 {'location': Location(-23.691288, 46.788279)},
 {'venue': Venue(Location(-23.691288, 46.788279),
                 'some place', 'right here')},
 {'left_chat_member': User(33, 'kicked')},
 {'new_chat_title': 'new title'},
 {'new_chat_photo': [PhotoSize('photo_id', 50, 50)]},
 {'delete_chat_photo': True},
 {'group_chat_created': True},
 {'supergroup_chat_created': True},
 {'channel_chat_created': True},