text
stringlengths
1
93.6k
# Get context for the device (basically a status)
context_audio = {
"header": {
"namespace": "AudioPlayer",
"name": "PlaybackState"
},
"payload": {
"token": "audio_token",
"offsetInMilliseconds": 0,
"playerActivity": "IDLE"
}
}
context_alerts = self.alarm_manager.get_alarm_context()
context_speaker = {
"header": {
"namespace": "Speaker",
"name": "VolumeState"
},
"payload": {
"volume": 100,
"muted": False
}
}
return [context_audio, context_alerts, context_speaker]
def process_response(self, message):
""" Called when a message is received from Alexa (either on the downchannel or as a response). This
function will take actions based on the message recieved.
:param message: message received from Alexa
"""
# Loop through each message
print("%d messages received" % len(message['content']))
# If there is no content in the message, throw error (nothing to parse)
if 'content' not in message:
raise KeyError("Content is not available.")
# If there are more than one attachments, throw error (code currently can't handle this.
# Not sure if this is even possible based on the AVS API.
if len(message['attachment']) > 1:
raise IndexError("Too many attachments (%d)" % len(message['attachment']))
if message['attachment']:
attachment = message['attachment'][0]
else:
attachment = None
# print("%d messages received" % len(message['content']))
# Loop through all content received
for content in message['content']:
header = content['directive']['header']
# Get the namespace from the header and call the correct process directive function
namespace = header['namespace']
if namespace == 'SpeechSynthesizer':
self.process_directive_speech_synthesizer(content, attachment)
elif namespace == 'SpeechRecognizer':
self.process_directive_speech_recognizer(content, attachment)
elif namespace == 'Alerts':
self.process_directive_alerts(content, attachment)
# Throw an error in case the namespace is not recognized.
# This indicates new a process directive function needs to be added
else:
raise NameError("Namespace not recognized (%s)." % namespace)
def process_directive_speech_synthesizer(self, content, attachment):
""" Process a directive that belongs to the SpeechSynthesizer namespace.
:param content: content dictionary (contains header and payload)
:param attachment: attachment included with the content
"""
header = content['directive']['header']
payload = content['directive']['payload']
# Get the name from the header
name = header['name']
# Process the SpeechSynthesizer.Speak directive
if name == 'Speak':
# Get token for current TTS object
token = payload['token']
audio_response = attachment
# Set SpeechSynthesizer context state to "playing"
# TODO capture state so that it can be used in context
# Send SpeechStarted Event (with token)
stream_id = self.alexa.send_event_speech_started(token)
self.alexa.get_and_process_response(stream_id)
# Play the mp3 file
self.alexa_audio_instance.play_mp3(audio_response)
# Send SpeechFinished Event (with token)
stream_id = self.alexa.send_event_speech_finished(token)
self.alexa.get_and_process_response(stream_id)
# Set SpeechSynthesizer context state to "finished"
# TODO capture state so that it can be used in context
# Throw an error if the name is not recognized.
# This indicates new a case needs to be added
else:
raise NameError("Name not recognized (%s)." % name)