예제 #1
0
  'forward_from_message_id': 101,
  'forward_date': datetime.now()},
 {'reply_to_message': Message(50, None, None, None)},
 {'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'},
 {'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)]},
예제 #2
0
     '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'
 }, {
     'sticker': Sticker('sticker_id', 50, 50, True)
 }, {
     '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)]
예제 #3
0
class TestGame(object):
    title = 'Python-telegram-bot Test Game'
    description = 'description'
    photo = [PhotoSize('Blah', 'ElseBlah', 640, 360, file_size=0)]
    text = (b'\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467'
            b'\\u200d\\U0001f467\\U0001f431http://google.com'
            ).decode('unicode-escape')
    text_entities = [MessageEntity(13, 17, MessageEntity.URL)]
    animation = Animation('blah', 'unique_id', 320, 180, 1)

    def test_de_json_required(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo

    def test_de_json_all(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
            'text': self.text,
            'text_entities': [self.text_entities[0].to_dict()],
            'animation': self.animation.to_dict()
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo
        assert game.text == self.text
        assert game.text_entities == self.text_entities
        assert game.animation == self.animation

    def test_to_dict(self, game):
        game_dict = game.to_dict()

        assert isinstance(game_dict, dict)
        assert game_dict['title'] == game.title
        assert game_dict['description'] == game.description
        assert game_dict['photo'] == [game.photo[0].to_dict()]
        assert game_dict['text'] == game.text
        assert game_dict['text_entities'] == [game.text_entities[0].to_dict()]
        assert game_dict['animation'] == game.animation.to_dict()

    def test_parse_entity(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        game.text_entities = [entity]

        assert game.parse_text_entity(entity) == 'http://google.com'

    def test_parse_entities(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        entity_2 = MessageEntity(type=MessageEntity.BOLD, offset=13, length=1)
        game.text_entities = [entity_2, entity]

        assert game.parse_text_entities(MessageEntity.URL) == {
            entity: 'http://google.com'
        }
        assert game.parse_text_entities() == {
            entity: 'http://google.com',
            entity_2: 'h'
        }
예제 #4
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)
class TestGame:
    title = "Python-telegram-bot Test Game"
    description = "description"
    photo = [PhotoSize("Blah", "ElseBlah", 640, 360, file_size=0)]
    text = (b"\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467"
            b"\\u200d\\U0001f467\\U0001f431http://google.com"
            ).decode("unicode-escape")
    text_entities = [MessageEntity(13, 17, MessageEntity.URL)]
    animation = Animation("blah", "unique_id", 320, 180, 1)

    def test_slot_behaviour(self, game, mro_slots):
        for attr in game.__slots__:
            assert getattr(game, attr,
                           "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(game)) == len(set(
            mro_slots(game))), "duplicate slot"

    def test_de_json_required(self, bot):
        json_dict = {
            "title": self.title,
            "description": self.description,
            "photo": [self.photo[0].to_dict()],
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo

    def test_de_json_all(self, bot):
        json_dict = {
            "title": self.title,
            "description": self.description,
            "photo": [self.photo[0].to_dict()],
            "text": self.text,
            "text_entities": [self.text_entities[0].to_dict()],
            "animation": self.animation.to_dict(),
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo
        assert game.text == self.text
        assert game.text_entities == self.text_entities
        assert game.animation == self.animation

    def test_to_dict(self, game):
        game_dict = game.to_dict()

        assert isinstance(game_dict, dict)
        assert game_dict["title"] == game.title
        assert game_dict["description"] == game.description
        assert game_dict["photo"] == [game.photo[0].to_dict()]
        assert game_dict["text"] == game.text
        assert game_dict["text_entities"] == [game.text_entities[0].to_dict()]
        assert game_dict["animation"] == game.animation.to_dict()

    def test_parse_entity(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        game.text_entities = [entity]

        assert game.parse_text_entity(entity) == "http://google.com"

    def test_parse_entities(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        entity_2 = MessageEntity(type=MessageEntity.BOLD, offset=13, length=1)
        game.text_entities = [entity_2, entity]

        assert game.parse_text_entities(MessageEntity.URL) == {
            entity: "http://google.com"
        }
        assert game.parse_text_entities() == {
            entity: "http://google.com",
            entity_2: "h"
        }

    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)
예제 #6
0
def thumb():
    return PhotoSize(height=50, file_size=1613, file_id='AAQEABPQUWQZAAT7gZuQAAH0bd93VwACAg',
                     width=90)
class TestGame:
    title = 'Python-telegram-bot Test Game'
    description = 'description'
    photo = [PhotoSize('Blah', 'ElseBlah', 640, 360, file_size=0)]
    text = (b'\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467'
            b'\\u200d\\U0001f467\\U0001f431http://google.com'
            ).decode('unicode-escape')
    text_entities = [MessageEntity(13, 17, MessageEntity.URL)]
    animation = Animation('blah', 'unique_id', 320, 180, 1)

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

    def test_de_json_required(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo

    def test_de_json_all(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
            'text': self.text,
            'text_entities': [self.text_entities[0].to_dict()],
            'animation': self.animation.to_dict(),
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo
        assert game.text == self.text
        assert game.text_entities == self.text_entities
        assert game.animation == self.animation

    def test_to_dict(self, game):
        game_dict = game.to_dict()

        assert isinstance(game_dict, dict)
        assert game_dict['title'] == game.title
        assert game_dict['description'] == game.description
        assert game_dict['photo'] == [game.photo[0].to_dict()]
        assert game_dict['text'] == game.text
        assert game_dict['text_entities'] == [game.text_entities[0].to_dict()]
        assert game_dict['animation'] == game.animation.to_dict()

    def test_parse_entity(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        game.text_entities = [entity]

        assert game.parse_text_entity(entity) == 'http://google.com'

    def test_parse_entities(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        entity_2 = MessageEntity(type=MessageEntity.BOLD, offset=13, length=1)
        game.text_entities = [entity_2, entity]

        assert game.parse_text_entities(MessageEntity.URL) == {
            entity: 'http://google.com'
        }
        assert game.parse_text_entities() == {
            entity: 'http://google.com',
            entity_2: 'h'
        }

    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)