shivasiddharth / GassistPi

Google Assistant for Single Board Computers
Other
1.02k stars 303 forks source link

[Feature Request] Repeat after me #724

Closed DewGew closed 5 years ago

DewGew commented 5 years ago

A function like " Repaet after me" or "say" would be useful. If I say " Repeat after me, Hi I'm new to google assistant " or similar Then GassistPi replays: " Hi I'm new to google assistant "

With this I can send out custom tts to my GassistPi from my domoticz via mqtt.

DewGew commented 5 years ago

Repeat after me is a native function already but I cant make this command with mqtt.

aandroide commented 5 years ago

I have already pointed out to Sid that this command does not work on mqtt

DewGew commented 5 years ago

I just need a function and send custom voice notifications true gassistpi. Should be possible to make a custom function for this.

DewGew commented 5 years ago

I created my own function. I added this: in actions.py:

def speak(phrase):
    voice_notify = phrase.replace("speak", "")
    print(voice_notify)
    say(voice_notify)

and in main.py: from actions import speak

if 'speak'.lower() in str(usrcmd).lower():
    self.assistant.stop_conversation()
    speak(str(usrcmd).lower())

with this I can send voice notifications with mqtt command:

custom speak I am new to google assistant

and Gassist Pi replies "Im new to google assistant"

Now only thing is that swedish characters not working via mqtt. I can't send å ä and ö. I only get those like '\xc3\x85', '\xc3\x84', '\xc3\x96'. Any ideas howto get this to work? @shivasiddharth

DewGew commented 5 years ago

I'm trying with say(voice_notify.decode("utf-8")) or say(str(voice_notify.decode("utf-8"))) none is working. I also tried with print instead of say. Dont even print out this.

But this works in def on_message(): print("Message from MQTT: "+str(msg.payload.decode("utf-8")))

jcksnmdy commented 5 years ago

I get segmentation error when running this in assistant. What might I have done wrong?

DewGew commented 5 years ago

@jcksnmdy My code had a typo fault. Test with new code above..

aandroide commented 5 years ago

@DewGew Now only thing is that swedish characters not working via mqtt. I can't send å ä and ö. I only get those like '\xc3\x85', '\xc3\x84', '\xc3\x96'. Any ideas howto get this to work?

it is not enough to insert utf-8 in mqtt the problem is in action.py in the "word translator" function is not declared unicode utf-8

Word translator

def trans(words,destlang,srclang): transword= translator.translate(words, dest=destlang, src=srclang) transword=transword.text transword=transword.replace("Text, ",'',1) transword=transword.strip() print(transword) return transword

aandroide commented 5 years ago

@shivasiddharth do you have any suggestions to solve the unicode problem in word translate

DewGew commented 5 years ago

Word translate is not the problem even print() does not work. Strange thing is that print("Message from MQTT: "+str(msg.payload.decode("utf-8"))) is working but print(str(mqtt_query.decode("utf-8"))) or print(mqtt_query.decode("utf-8")) does not work

aandroide commented 5 years ago

your print is wrong you have to set it up and you'll see that it works: print("Message from MQTT: "+str(msg.payload.decode("utf-8")))

to me it also prints the accents correctly. it is then the translator that goes haywire

the code correct for mqtt is: def on_message(self, client, userdata, msg): if self.can_start_conversation == True: print("Message from MQTT: "+str(msg.payload.decode("utf-8"))) mqtt_query=str(msg.payload)[1:] if 'custom' in mqtt_query.lower(): mqtt_query=mqtt_query.lower() mqtt_queryidx=mqtt_query.find('custom') mqtt_query=mqtt_query[mqtt_queryidx:] mqtt_query=mqtt_query.replace('custom',"",1) mqtt_query=mqtt_query.strip() self.custom_command(mqtt_query) else: self.assistant.send_text_query(mqtt_query)

aandroide commented 5 years ago

if I write in Italian custom speak "è vero" the message is printed correctly but pronounced with the errors x Message from MQTT: custom speak è vero \ xc3 \ xa8 vero ' Playing WAVE '/tmp/male-say.wav' : Signed 16 bit Little Endian, Rate 24000 Hz, Mono

shivasiddharth commented 5 years ago

Did you guys try adding the UTF-8 encoding at the top of the script ?

aandroide commented 5 years ago

yes, don't work

!/usr/bin/env python

"# -- coding: utf-8 -- "

shivasiddharth commented 5 years ago

above this line https://github.com/shivasiddharth/GassistPi/blob/b737c460f86b80da059df573796514db00212ad7/src/actions.py#L315 add print(sayword) And above this line https://github.com/shivasiddharth/GassistPi/blob/b737c460f86b80da059df573796514db00212ad7/src/actions.py#L307 add print(words)

Now give the command and post as to what those lines are printing.

aandroide commented 5 years ago

Message from MQTT: custom speak è vero \xc3\xa8 vero ' \ xc3 \ xa8 vero ' \ xc3 \ xa8 vero ' Playing WAVE '/tmp/male-say.wav' : Signed 16 bit Little Endian, Rate 24000 Hz, Mono

shivasiddharth commented 5 years ago

After this line https://github.com/shivasiddharth/GassistPi/blob/b737c460f86b80da059df573796514db00212ad7/src/main.py#L453
add mqtt_query = mqtt_query.decode("utf-8")

aandroide commented 5 years ago

adding that line hangs and writes: Message from MQTT: <built-in method decode of bytes object at 0x6f43bc68>

aandroide commented 5 years ago

mqtt does his job well and prints the accents correctly. the problem is something else

print ("Messaggio da MQTT:" + str (msg.payload.decode ("utf-8") ))

shivasiddharth commented 5 years ago

Guess you have added it at wrong place. Adding mqtt_query = mqtt_query.decode("utf-8") after mqtt_query=str(msg.payload)[1:] will not affect the print("Message from MQTT: "+str(msg.payload.decode("utf-8")))

aandroide commented 5 years ago

great work, thanks for solving @shivasiddharth

DewGew commented 5 years ago

This does not work for me.. If I ask what time it is in swedish. function. this mqtt_query = mqtt_query.decode("utf-8") stops my functions.

Script:

def on_message(self, client, userdata, msg):
        if self.can_start_conversation == True:
            print("Message from MQTT: "+str(msg.payload.decode("utf-8")))
            mqtt_query=str(msg.payload)[1:]
            print(mqtt_query)
            if 'custom' in mqtt_query.lower():
                mqtt_query=mqtt_query.lower()
                mqtt_queryidx=mqtt_query.find('custom')
                mqtt_query=mqtt_query[mqtt_queryidx:]
                mqtt_query=mqtt_query.replace('custom',"",1)
                mqtt_query=mqtt_query.strip()
                self.custom_command(mqtt_query)
            else:
                self.assistant.send_text_query(mqtt_query)

terminal:

ON_MUTED_CHANGED:
  {"is_muted": false}

ON_START_FINISHED

ON_MEDIA_STATE_IDLE

Connected with result code 0
Message from MQTT: Vad är klockan
'Vad \xc3\xa4r klockan'
ON_CONVERSATION_TURN_STARTED

E0114 09:02:06.253649187    3782 fork_posix.cc:63]           Fork support is only compatible with the epoll1 and poll polling strategies
ON_RESPONDING_STARTED:
  {"is_error_response": false}

ON_RESPONDING_FINISHED

modified script:

def on_message(self, client, userdata, msg):
        if self.can_start_conversation == True:
            print("Message from MQTT: "+str(msg.payload.decode("utf-8")))
            mqtt_query=str(msg.payload)[1:]
            print(mqtt_query.decode("utf-8"))
            if 'custom' in mqtt_query.lower():
                mqtt_query=mqtt_query.lower()
                mqtt_queryidx=mqtt_query.find('custom')
                mqtt_query=mqtt_query[mqtt_queryidx:]
                mqtt_query=mqtt_query.replace('custom',"",1)
                mqtt_query=mqtt_query.strip()
                self.custom_command(mqtt_query)
            else:
                self.assistant.send_text_query(mqtt_query)

terminal:

ON_MUTED_CHANGED:
  {"is_muted": false}

ON_START_FINISHED

ON_MEDIA_STATE_IDLE

Connected with result code 0
Message from MQTT: Vad är klockan
shivasiddharth commented 5 years ago

Please look at the commit referenced above.

DewGew commented 5 years ago

Now its working with custom actions... native functions only work with english commands (swedish also but without å ä ö).

DewGew commented 5 years ago

When I use Google cloud. It speak fast. Can I add "speakingRate" here somewhere? https://github.com/shivasiddharth/GassistPi/blob/a2cbf57ca0c40c43c17a0b4b5e2f393c5a38fc70/src/actions.py#L279