class AlexaBot(object): ''' Simple class which moves the Flask Ask decorators to simple config ''' def __init__(self, config): # set up the Flask app self.app = Flask(__name__) self.ask = Ask(self.app, '/') # load config # the config is a list of lists (keeping w/ what the students know from Eliza): # 0: intent name # 1: intent method to call for intent_config in config: intent_name = intent_config[0] func = intent_config[1] if intent_name == 'launch': # special case the 'launch' method func = self.ask.launch(func) elif intent_name.lower().startswith('on_playback'): # special case 'on_playback' # get the method name from the string method = getattr(self.ask, intent_name) func = method(func) else: func = self.ask.intent(intent_name)(func) def start(self, debug=False): ''' start it up Note: debug=True does not play well with jupyter notebook ''' # start it up self.app.run(debug=debug)
class AlexaServer(object): def __init__(self, lights_433_server): self.server = lights_433_server self.ask = Ask(self.server.app, '/switch_alexa') self.ask.intent('LightSwitch')( lambda location, operation: self.perform_switch(location, operation) ) self.ask.intent('AMAZON.HelpIntent')( lambda: self.get_welcome_response() ) def match_location(self, location): """ We will mutate the score a bit to add +0.1 to the jaro distance for starting with the same letter. Alexa's speech processing system really sucks at this. location -- the location to match against """ if location: matches = [] for switch_id, switch_func in self.server.switches.items(): similarity = jaro(location, switch_id) if location[0].lower() == switch_id[0].lower(): similarity += 0.1 matches += [(similarity, switch_id, switch_func)] matches.sort(key=lambda x: x[0], reverse=True) if matches[0][0] >= _MATCH_THRESHOLD: return matches[0][1:] raise ActionParseError("I didn't understand the location. " "Could you please repeat?") def match_operation(self, operation): if operation.lower() in ('on', 'up', 'in'): return 'on' elif operation.lower() in ('off', 'down', 'out'): return 'off' raise ActionParseError("I didn't understand the operation. " "Could you please repeat?") def perform_switch(self, location, operation): try: resolved_location = self.match_location(location or "") resolved_operation = self.match_operation(operation or "") except ActionParseError as ape: return question(str(ape)).simple_card(_CARD_TITLE, str(ape)) switch_id, switch_func = resolved_location try: r = switch_func(resolved_operation) text = "%s %s!" % ( switch_id.replace('_', ' ').title(), resolved_operation) status = int(r.status.split(' ')[0]) if status < 200 or status > 299: raise Exception(status) except Exception: text = "I had problems communicating with your device!" return statement(text).simple_card(_CARD_TITLE, text) def get_welcome_response(self): text = "Ask me to turn your lights on and off!" return statement(text).simple_card(_CARD_TITLE, text)
msg = render_template('utter_booked') response = statement(msg) return response @ask.intent("deny") def received_deny(): msg = render_template('utter_ask_again') return statement(msg) ask.intent("ask_again") def ask_again(answer): update_dialog_history(session, request) print(request) if request == "yes": msg = render_template('utter_ask_book') return statement(msg) else: msg = render_template('utter_goodbye') return statement(msg) if __name__ == '__main__':
@ask.launch def launch(): return ## get_new_fact() #TO CHANGE @ask.intent(#'GetNewFactIntent') #TO CHANGE def get_new_fact(): #TO CHANGE # EDIT return #TO CHANGE @ask.intent('AMAZON.HelpIntent') def help(): help_text = render_template('help') return question(help_text).reprompt(help_text) @ask.intent('AMAZON.StopIntent') def stop(): bye_text = render_template('bye') return statement(bye_text) @ask.intent('AMAZON.CancelIntent') def cancel(): bye_text = render_template('bye') return statement(bye_text) @ask.session_ended def session_ended(): return "{}", 200 if __name__ == '__main__': if 'ASK_VERIFY_REQUESTS' in os.environ:
ENDPOINT = 'https://api.coinmarketcap.com/v1/ticker/' app = Flask(__name__) ask = Ask(app, '/') logger = logging.getLogger() @ask.session_ended def end(): speech = 'Goodbye!' logger.info('speech = {}'.format(speech)) return statement(speech) ask.intent('AMAZON.CancelIntent')(end) ask.intent('AMAZON.StopIntent')(end) @ask.intent('AMAZON.HelpIntent') def help(): help_message = 'You can say tell me the price of a cryptocurrency, for example, bitcoin or ethereum. You can also say exit... What can I help you with?' help_reprompt = 'Sorry, I didn\'t get that. What would you like me to do?' logger.info('question = {}, {}'.format(help_message, help_reprompt)) return question(help_message).reprompt(help_reprompt) ask.launch(help) @ask.intent('GetPriceIntent', default={'coin': 'NULL'})