Exemplo n.º 1
0
 def get_file_url(self, file_id: str) -> str:
     # TODO make download and save on this server
     try:
         file = apihelper.get_file(self.token, file_id)
         url = f'https://api.telegram.org/file/bot{self.token}/{file["file_path"]}'
     except Exception as err:
         log.exception(err)
         return ''
     return url
Exemplo n.º 2
0
 def get_file(self, file_id):
     return types.File.de_json(apihelper.get_file(self.token, file_id))
Exemplo n.º 3
0
 def get_file(self, file_id):
     return types.File.de_json(apihelper.get_file(self.token, file_id))
Exemplo n.º 4
0
def listener(messages):
    for m in messages:
        try:
            oUser = m.from_user
            oChat = m.chat

            cid = oChat.id

            uid = oUser.id
            first_name = oUser.first_name
            last_name = oUser.last_name
            username = oUser.username

            # removing accent from text

            if m.content_type == "text":
                text = unidecode(m.text)

            answer = ""

            userSession = getSession(cid)
            session_id = userSession.session_id

            if m.content_type == 'text':
                if not reservedCommands(cid, text):
                    answer = userSession.mensagem(text)
                    answer = processSpecialAnswer(cid, answer)
                    if type(answer) == list:
                        for ans in answer:
                            sendAnswer(cid, ans)
                    else:
                        sendAnswer(cid, answer)

            elif m.content_type == 'location':
                location = m.location
                answer = userSession.mensagem('location')
                answer = processSpecialAnswer(cid, answer)
                sendAnswer(cid, answer)

            elif m.content_type == "voice":
                response = get_file(PRODUCTION_BOT_TOKEN, m.voice.file_id)

                audio = bot.download_file(response["file_path"])

                with open("tmp/audio" + str(session_id) + ".ogg", "wb") as f:
                    f.write(audio)

                ogg_file = AudioSegment.from_file("tmp/audio" +
                                                  str(session_id) + ".ogg",
                                                  format="ogg")
                ogg_file.export("tmp/audio" + str(session_id) + ".wav",
                                format="wav")

                r = sr.Recognizer()

                with sr.AudioFile("tmp/audio" + str(session_id) +
                                  ".wav") as source:
                    audio_source = r.record(source)

                try:
                    # Uses the default API key
                    # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
                    text = r.recognize_google(audio_source, language="pt-BR")

                    bot.send_message(cid, "Você disse: " + text)

                    answer = userSession.mensagem(text)

                    answer = processSpecialAnswer(cid, answer)

                    if type(answer) == list:
                        for ans in answer:
                            sendAnswer(cid, ans, is_voice=True)
                    else:
                        sendAnswer(cid, answer, is_voice=True)

                except sr.UnknownValueError:
                    print(
                        "Google Speech Recognition could not understand audio")
                except sr.RequestError as e:
                    print(
                        "Could not request results from Google Speech Recognition service; {0}"
                        .format(e))

            # logManager.logMessage(session_id, cid, uid, first_name, last_name, username, text, answer)

        except Exception as inst:
            print(type(inst))  # the exception instance
            print(inst.args)  # arguments stored in .args
            print(inst)