コード例 #1
0
 def test_filters_audio(self, update):
     assert not Filters.audio(update)
     update.message.audio = 'test'
     assert Filters.audio(update)
コード例 #2
0
 def test_filters_audio(self):
     self.message.audio = 'test'
     self.assertTrue(Filters.audio(self.message))
     self.message.audio = None
     self.assertFalse(Filters.audio(self.message))
コード例 #3
0
def sendToAll(bot, message, list_of_chats, user_chat_id):
    timeout = 10  #Timeout in seconds, though this might be a good idea, don't think this bot will be hitting this any time soon
    # This is the bulk of the work in this bot.

    if Filters.forwarded(message):
        message_id = message.message_id
        from_chat = message.forward_from_chat.id
        for chat in list_of_chats:
            try:
                bot.forward_message(chat_id=chat,
                                    from_chat_id=from_chat,
                                    message_id=message_id,
                                    timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)

        try:
            newMessage = bot.forward_message(chat_id=user_chat_id,
                                             from_chat_id=from_chat,
                                             message_id=message_id,
                                             timeout=timeout)
        # If the user has not responded. Message all of the admins.
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:  #error checking in error checking because bot.sendMessage doesn't work for people who haven't messaged the bot.
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to forward message to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.text(message):
        for chat in list_of_chats:
            try:
                bot.send_message(chat_id=chat,
                                 text=message.text,
                                 timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_message(chat_id=user_chat_id,
                                          text=message.text,
                                          timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send text message to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.audio(message):
        audio = message.audio.file_id
        for chat in list_of_chats:
            try:
                bot.send_audio(chat_id=chat, audio=audio, timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_audio(chat_id=user_chat_id,
                                        audio=audio,
                                        timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send audio message to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.document(message):
        document = message.document.file_id
        for chat in list_of_chats:
            try:
                bot.send_document(chat_id=chat,
                                  document=document,
                                  timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_document(chat_id=user_chat_id,
                                           document=document,
                                           timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send document to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.photo(message):
        photo = message.photo[0].file_id
        caption = ""
        if message.caption:
            caption = message.caption
        for chat in list_of_chats:
            try:
                bot.send_photo(chat_id=chat,
                               photo=photo,
                               caption=caption,
                               timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_photo(chat_id=user_chat_id,
                                        photo=photo,
                                        caption=caption,
                                        timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send photo to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.sticker(message):
        sticker = message.sticker.file_id
        for chat in list_of_chats:
            try:
                bot.send_sticker(chat_id=chat,
                                 sticker=sticker,
                                 timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send messages to admin in SendToAll: %s" % te)
        try:
            newMessage = bot.send_sticker(chat_id=user_chat_id,
                                          sticker=sticker,
                                          timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send sticker to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.voice(message):
        voice = message.voice.file_id
        for chat in list_of_chats:
            try:
                bot.send_voice(chat_id=chat, voice=voice, timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s " % te)
        try:
            newMessage = bot.send_voice(chat_id=user_chat_id,
                                        voice=voice,
                                        timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send voice message to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.video(message):
        video = message.video.file_id
        for chat in list_of_chats:
            try:
                bot.send_video(chat_id=chat, video=video, timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_video(chat_id=user_chat_id,
                                        video=video,
                                        timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send message to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.contact(message):
        phone_number = message.contact.phone_number
        first_name = message.contact.first_name
        last_name = message.contact.last_name
        for chat in list_of_chats:
            try:
                bot.send_contact(chat_id=chat,
                                 phone_number=phone_number,
                                 first_name=first_name,
                                 last_name=last_name,
                                 timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unbable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_contact(chat_id=user_chat_id,
                                          phone_number=phone_number,
                                          first_name=first_name,
                                          last_name=last_name,
                                          timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send contact to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    elif Filters.location(message):
        lat = message.location.latitude
        lon = message.location.longitude
        for chat in list_of_chats:
            try:
                bot.send_location(chat_id=chat,
                                  longitude=lon,
                                  latitude=lat,
                                  timeout=timeout)
            except TelegramError as te:
                logger.debug(
                    "Unable to send message to admin in sendToAll: %s" % te)
        try:
            newMessage = bot.send_location(chat_id=user_chat_id,
                                           longitude=lon,
                                           latitude=lat,
                                           timeout=timeout)
        except TelegramError as te:
            logger.debug("Unable to send message to user in sendToAll: %s" %
                         te)
            for chat in list_of_chats:
                try:
                    bot.sendMessage(
                        chat_id=chat,
                        text=
                        "Unable to send location to user, if this persists, resolve this thread, they may have stopped talking to the bot."
                    )
                except TelegramError:
                    pass

    else:
        logger.warning("Message %s not handled in SendToAll")
        raise TelegramError("No handler for forwarding.")

    MDB.active.update({'_id': user_chat_id},
                      {'$push': {
                          'log': newMessage.message_id
                      }})
コード例 #4
0
 def test_filters_audio(self, message):
     assert not Filters.audio(message)
     message.audio = 'test'
     assert Filters.audio(message)
コード例 #5
0
 def test_filters_audio(self, message):
     assert not Filters.audio(message)
     message.audio = 'test'
     assert Filters.audio(message)
コード例 #6
0
 def test_filters_audio(self):
     self.message.audio = 'test'
     self.assertTrue(Filters.audio(self.message))
     self.message.audio = None
     self.assertFalse(Filters.audio(self.message))