Package: avs-client
Alexa Voice Service ClientPython Client for Alexa Voice Service (AVS)This package has been renamed toalexa-client. Onlyalexa-clientwill receive updates.Installationpipinstallavs_clientor if you want to run the demos:pipinstallavs_client[demo]UsageFile audiofromavs_clientimportAlexaVoiceServiceClientalexa_client=AlexaVoiceServiceClient(client_id='my-client-id',secret='my-secret',refresh_token='my-refresh-token',)alexa_client.connect()# authenticate and other handshaking stepswithopen('./tests/resources/alexa_what_time_is_it.wav','rb')asf:fori,directiveinenumerate(alexa_client.send_audio_file(f)):ifdirective.namein['Speak','Play']:withopen(f'./output_{i}.mp3','wb')asf:f.write(directive.audio_attachment)Now listen tooutput_0.wavand Alexa should tell you the time.Microphone audioimportiofromavs_clientimportAlexaVoiceServiceClientimportpyaudiodefcallback(in_data,frame_count,time_info,status):buffer.write(in_data)return(in_data,pyaudio.paContinue)p=pyaudio.PyAudio()stream=p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True,stream_callback=callback,)alexa_client=AlexaVoiceServiceClient(client_id='my-client-id',secret='my-secret',refresh_token='my-refresh-token',)buffer=io.BytesIO()try:stream.start_stream()print('listening. Press CTRL + C to exit.')alexa_client.connect()fori,directiveinenumerate(alexa_client.send_audio_file(buffer)):ifdirective.namein['Speak','Play']:withopen(f'./output_{i}.mp3','wb')asf:f.write(directive.audio_attachment)finally:stream.stop_stream()stream.close()p.terminate()Voice Request LifecycleAn Alexa command may relate to a previous command e.g,[you] "Alexa, play twenty questions"
[Alexa] "Is it a animal, mineral, or vegetable?"
[you] "Mineral"
[Alexa] "Is it valuable"
[you] "No"
[Alexa] "is it..."This can be achieved by passing the same dialog request ID to multiplesend_audio_filecalls:fromavs_client.avs_clientimporthelpersdialog_request_id=helpers.generate_unique_id()directives_one=alexa_client.send_audio_file(audio_one,dialog_request_id=dialog_request_id)directives_two=alexa_client.send_audio_file(audio_two,dialog_request_id=dialog_request_id)directives_three=alexa_client.send_audio_file(audio_three,dialog_request_id=dialog_request_id)Run the streaming microphone audio demo to use this feature:pipinstallavs_client[demo]python-mavs_client.demo.streaming_microphone\--client-id="{enter-client-id-here}"\--client-secret="{enter-client-secret-here"}\--refresh-token="{enter-refresh-token-here}"AuthenticationTo use AVS you must first have adeveloper account. Then register your producthere. Choose "Application" under "Is your product an app or a device"?The client requires yourclient_id,secretandrefresh_token:client kwargNotesclient_idRetrieve by clicking on the your product listedheresecretRetrieve by clicking on the your product listedhererefresh_tokenYou must generate this.See belowRefresh tokenYou will need to login to Amazon via a web browser to get your refresh token.To enable this first gohereand click on your product to set some security settings underSecurity Profile:settingvalueAllowed Originshttp://localhost:9000Allowed Return URLshttp://localhost:9000/callback/Note what you entered for Product ID under Product Information, as this will be used as the device-type-id (case sensitive!)Then run:python-mavs_client.refreshtoken.serve\--device-type-id="{enter-device-type-id-here}"\--client-id="{enter-client-id-here}"\--client-secret="{enter-client-secret-here}"Follow the on-screen instructions shown athttp://localhost:9000in your web browser.
On completion Amazon will return yourrefresh_token- which you will require tosend audioorrecorded voice.Steaming audio to AVSalexa_client.send_audio_filestreaming uploads a file-like object to AVS for great latency. The file-like object can be an actual file on your filesystem, an in-memory BytesIo buffer containing audio from your microphone, or even audio streaming fromyour browser over a websocket in real-time.AVS requires the audio data to be 16bit Linear PCM (LPCM16), 16kHz sample rate, single-channel, and little endian.Persistent AVS connectionCallingalexa_client.connect()creates a persistent connection to AVS. A thread runs that pings AVS after 4 minutes of no request being made to AVS. This prevents the connection getting forcefully closed due to inactivity.Unit testTo run the unit tests, call the following commands:gitclonegit@github.com:richtier/alexa-voice-service-client.git
pipinstall-e.[test]maketest_requirements
maketestOther projectsThis library is used byalexa-browser-client, which allows you to talk to Alexa from your browser.