text
stringlengths
1
93.6k
def process_directive_speech_recognizer(self, content, attachment):
""" Process a directive that belongs to the SpeechRecognizer namespace. Attachment not used, but included
to keep the same arguments as other process_directive functions.
: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 SpeechRecognizer.ExpectSpeech directive
if name == 'ExpectSpeech':
# Get specific fields for expect speech
dialog_request_id = header['dialogRequestId']
timeout = payload['timeoutInMilliseconds']/1000
# Get audio, as requested by Alexa (using the specified timeout)
raw_audio = self.alexa_audio_instance.get_audio(timeout)
# If raw_audio is none, the user did not respond or speak
if raw_audio is None:
# TODO add sounds to prompt the user to do something, rather than text
print("Speech timeout.")
# Send an event to let Alexa know that the user did not respond
stream_id = self.alexa.send_event_expect_speech_timed_out()
self.alexa.get_and_process_response(stream_id)
return
# Send audio captured (start_recognize_event) using old dialog_request_id and then process reponse
stream_id = self.alexa.start_recognize_event(raw_audio, dialog_request_id=dialog_request_id)
self.alexa.get_and_process_response(stream_id)
elif name == 'StopCapture':
# TODO find out what this means, it is not in the API.
pass
# 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)
def process_directive_alerts(self, content, attachment):
""" Process a directive that belongs to the Alert namespace. Attachment not used, but included
to keep the same arguments as other process_directive functions.
: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']
token = payload['token']
if name == 'SetAlert':
is_set = self.alarm_manager.set_alert(
token,
payload['type'],
payload['scheduledTime']
)
# TODO move event sending to Alert object
if is_set:
stream_id = self.alexa.send_event_alert_name('SetAlertSucceeded', token)
else:
stream_id = self.alexa.send_event_alert_name('SetAlertFailed', token)
self.alexa.get_and_process_response(stream_id)
elif name == 'DeleteAlert':
is_deleted = self.alarm_manager.delete_alert(token)
if is_deleted:
stream_id = self.alexa.send_event_alert_name('DeleteAlertSucceeded', token)
else:
stream_id = self.alexa.send_event_alert_name('DeleteAlertFailed', token)
self.alexa.get_and_process_response(stream_id)
def close(self):
""" Closes the AlexaDevice. Should be called before the program terminates.
"""
self.device_stop_event.set()
self.alexa_audio_instance.close()
def wait_until_close(self):
""" Waits until the user stops the AlexaDevice threads. This uses thread.join() to wait until the thread is
terminated.
"""
self.device_thread.join()
# <FILESEP>
import re
from os import environ
id_pattern = re.compile(r'^.\d+$')
def is_enabled(value, default):
if value.lower() in ["true", "yes", "1", "enable", "y"]:
return True
elif value.lower() in ["false", "no", "0", "disable", "n"]:
return False
else:
return default