def test_equality(self, document):
        a = Document(document.file_id)
        b = Document(document.file_id)
        d = Document('')
        e = Voice(document.file_id, 0)

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

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

        assert a != e
        assert hash(a) != hash(e)
Beispiel #2
0
def show_information(bot: Bot, update):
    user = update.message.from_user
    file = get_user_related_info(user.id)

    if file:
        file_id = file.file_id
        doc = Document(file_id=file_id)
        try:
            bot.send_document(user.id, document=doc)
            update_is_sent_status(file_id)

            reply_keyboard = [['next information', 'main menu']]
            bot.send_message(
                user.id,
                text='choose one option',
                reply_markup=ReplyKeyboardMarkup(keyboard=reply_keyboard))

            return BotState.next_information

        except BadRequest:
            logger.info(LogMessage.bot_send_message_error)

    else:
        bot.send_message(chat_id=user.id,
                         text="You have finished the course please send "
                         "your location, so that we can"
                         " send your certification.")
        return BotState.get_location
    def _get_document(self):
        import uuid

        return Document(
            file_id=str(uuid.uuid4()),
            file_unique_id=str(uuid.uuid4()),
            file_name="somedoc.pdf",
        )
Beispiel #4
0
    def test_document(self):
        document = Document("idyouknow", file_name="test.pdf")
        u = self.mg.get_message(document=document)
        self.assertEqual(document.file_id, u.message.document.file_id)

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

        u = self.mg.get_message(document=True)
        self.assertIsInstance(u.message.document, Document)

        with self.assertRaisesRegexp(BadMessageException,
                                     r"telegram\.Document"):
            self.mg.get_message(document="document")
     [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',
         [
             PhotoSize('game_photo_id', 'unique_id', 30, 30),
         ],
     )
    def test_filters_document_type(self, update):
        update.message.document = Document(
            "file_id",
            'unique_id',
            mime_type="application/vnd.android.package-archive")
        assert Filters.document.apk(update)
        assert Filters.document.application(update)
        assert not Filters.document.doc(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "application/msword"
        assert Filters.document.doc(update)
        assert Filters.document.application(update)
        assert not Filters.document.docx(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "application/vnd.openxmlformats-officedocument." \
                                            "wordprocessingml.document"
        assert Filters.document.docx(update)
        assert Filters.document.application(update)
        assert not Filters.document.exe(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "application/x-ms-dos-executable"
        assert Filters.document.exe(update)
        assert Filters.document.application(update)
        assert not Filters.document.docx(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "video/mp4"
        assert Filters.document.gif(update)
        assert Filters.document.video(update)
        assert not Filters.document.jpg(update)
        assert not Filters.document.text(update)

        update.message.document.mime_type = "image/jpeg"
        assert Filters.document.jpg(update)
        assert Filters.document.image(update)
        assert not Filters.document.mp3(update)
        assert not Filters.document.video(update)

        update.message.document.mime_type = "audio/mpeg"
        assert Filters.document.mp3(update)
        assert Filters.document.audio(update)
        assert not Filters.document.pdf(update)
        assert not Filters.document.image(update)

        update.message.document.mime_type = "application/pdf"
        assert Filters.document.pdf(update)
        assert Filters.document.application(update)
        assert not Filters.document.py(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "text/x-python"
        assert Filters.document.py(update)
        assert Filters.document.text(update)
        assert not Filters.document.svg(update)
        assert not Filters.document.application(update)

        update.message.document.mime_type = "image/svg+xml"
        assert Filters.document.svg(update)
        assert Filters.document.image(update)
        assert not Filters.document.txt(update)
        assert not Filters.document.video(update)

        update.message.document.mime_type = "text/plain"
        assert Filters.document.txt(update)
        assert Filters.document.text(update)
        assert not Filters.document.targz(update)
        assert not Filters.document.application(update)

        update.message.document.mime_type = "application/x-compressed-tar"
        assert Filters.document.targz(update)
        assert Filters.document.application(update)
        assert not Filters.document.wav(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "audio/x-wav"
        assert Filters.document.wav(update)
        assert Filters.document.audio(update)
        assert not Filters.document.xml(update)
        assert not Filters.document.image(update)

        update.message.document.mime_type = "application/xml"
        assert Filters.document.xml(update)
        assert Filters.document.application(update)
        assert not Filters.document.zip(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "application/zip"
        assert Filters.document.zip(update)
        assert Filters.document.application(update)
        assert not Filters.document.apk(update)
        assert not Filters.document.audio(update)

        update.message.document.mime_type = "image/x-rgb"
        assert not Filters.document.category("application/")(update)
        assert not Filters.document.mime_type("application/x-sh")(update)
        update.message.document.mime_type = "application/x-sh"
        assert Filters.document.category("application/")(update)
        assert Filters.document.mime_type("application/x-sh")(update)
Beispiel #7
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', 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)
 }, {
 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
Beispiel #9
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)