def get_winner_intent_handler(request): winner =PickAWinner() print("[debug] Called the PickWinnerIntent Intent") card = r.create_card(title="Picking Winners!", subtitle=None, content="The winner is " + winner) return r.create_response(message="The winner is " + winner, end_session=True, card_obj=card)
def get_recipe_intent_handler(request): """ Use the 'intent' field in the VoiceHandler to map to the respective intent. You can insert arbitrary business logic code here """ # Get variables like userId, slots, intent name etc from the 'Request' object ingredient = request.get_slot_value("Ingredient") ingredient = ingredient if ingredient else "" #Use ResponseBuilder object to build responses and UI cards card = r.create_card(title="GetRecipeIntent activated", subtitle=None, content="asked alexa to find a recipe using {}" .format(ingredient)) return r.create_response(message="Finding a recipe with the ingredient {}".format(ingredient), end_session=False, card_obj=card)
def get_help_intent_handler(request): """ Use the 'intent' field in the VoiceHandler to map to the respective intent. You can insert arbitrary business logic code here """ # Get variables like userId, slots, intent name etc from the 'Request' object # ingredient = request.get_slot_value("Ingredient") # ingredient = ingredient if ingredient else "" #Use ResponseBuilder object to build responses and UI cards print("[debug] Called the HelpIntent Intent") card = r.create_card(title="Handy Helper", subtitle=None, content="Ok, you need some help") return r.create_response(message="Ok, you need some help", end_session=True, card_obj=card)
def get_rapper_intent_handler(request): """ Use the 'intent' field in the VoiceHandler to map to the respective intent. You can insert arbitrary business logic code here """ # Get variables like userId, slots, intent name etc from the 'Request' object rapper = request.get_slot_value("Rapper") rapper = rapper if rapper else "" with open("models/intros.json") as file: intros = json.load(file) try: intro = intros[rapper] except KeyError: intro = "" try: rap = get_rhyme(chains[rapper], 8) except KeyError: return r.create_response(message="I heard, %s, but I don't know that rapper." % rapper, end_session=False) rap = "<speak>Yo my name is {}. ".format(rapper) + intro + " " + rap + '<audio src="https://s3.amazonaws.com/danielgwilson.com/MLG+Horns+Sound+Effect.mp3" /></speak>' upload_rap(rap[7:-94]) # Use ResponseBuilder object to build responses and UI cards card = r.create_card(title="Rapping", subtitle=None, content=rap) return r.create_response(message=rap, is_ssml=True, end_session=False, card_obj=card)
def get_lyrical_intent_handler(request): ''' Handler when the user prompts alexa with lyrics ''' if request.verify_application(request.get_app_id()) == False: return httplib.responses[400] ''' The request_stage determines where we are currently at in the conversation ''' request_stage = request.get_req_attributes("request stage") if request_stage is None: request_stage = "Start" lyrics = request.get_slot_value("Lyrics") confirmation = request.get_slot_value("Confirmation") negation = request.get_slot_value("Negation") ''' Deals with the possibility that the user is thinking of lyrics that have the words yes or no and the beginning''' if lyrics is None and request_stage == "Start": return build_response(message=default_message_reprompt, end_session=False, reprompt_message=default_message_reprompt, sessionAttributes={"request stage" : "Start"}) if lyrics is None: if confirmation: lyrics = confirmation elif negation: lyrics = negation ''' Setting default values that will be changed in each stage ''' message = default_message end_session = False card_obj = None response_attributes = None reprompt_message = None if request_stage == "Start": message = knowledge_artist_name_message response_attributes = {"request stage" : "Add Artist Question", "lyrics" : lyrics} reprompt_message = knowledge_artist_name_reprompt elif request_stage == "Add Artist Question": ''' If the user knows the artists name, get the name and attach it to search query string ''' if confirmation: message = artist_name_message response_attributes = {"request stage" : "Artist Name Question", "lyrics" : request.get_req_attributes("lyrics")} reprompt_message = artist_name_reprompt elif negation: response_dict = build_song(request, request.get_req_attributes('lyrics'), '', 0) if response_dict: message = response_dict["message"] reprompt_message = response_dict["reprompt"] response_attributes = response_dict["response attributes"] else: return build_response(message=no_song_message, end_session=True, card_obj=card_obj) elif lyrics: ''' In the event the user does not give a confirmation or negation but instead gives the actual artist name''' response_dict = build_song(request, request.get_req_attributes('lyrics'), lyrics, 0) if response_dict: message = response_dict["message"] reprompt_message = response_dict["reprompt"] response_attributes = response_dict["response attributes"] else: return build_response(message=no_song_message, end_session=True, card_obj=card_obj) elif request_stage == "Artist Name Question": response_dict = build_song(request, request.get_req_attributes('lyrics'), lyrics, 0) if response_dict: message = response_dict["message"] reprompt_message = response_dict["reprompt"] response_attributes = response_dict["response attributes"] else: return build_response(message=no_song_message, end_session=True, card_obj=card_obj) elif request_stage == "Songs List": if negation: songs = request.get_req_attributes("songs") indexOfSong = request.get_req_attributes("index of song") indexOfSong = indexOfSong + 1 if indexOfSong > len(songs) - 1: message = out_of_song_message end_session = True else: song = songs[indexOfSong] artist = song[song.find('%') + 1:] song = song[:song.find('%')] message = potential_song_message % (song, artist) reprompt_message = potential_song_reprompt response_attributes = {"request stage" : "Songs List", "songs" : songs, "index of song" : indexOfSong, "lyrics" : request.get_req_attributes("lyrics"), "artist" : request.get_req_attributes("artist") } elif confirmation: end_session = True message = success_message songs = request.get_req_attributes("songs") indexOfSong = request.get_req_attributes("index of song") song = songs[indexOfSong] artist = song[song.find('%') + 1:] song = song[:song.find('%')] spoken_artist = request.get_req_attributes("artist") ''' If the user told Alexa the artists name ''' if spoken_artist == "": card_content = "Alexa heard: %s" % (request.get_req_attributes("lyrics")) else: card_content = "Alexa heard: %s by %s" % (request.get_req_attributes("lyrics"), request.get_req_attributes("artist")) card_obj = r.create_card(title="%s by %s" % (song, artist), subtitle=artist, content=card_content) else: message = invalid_utterance_message reprompt_message = potential_song_reprompt end_session = False card_obj = None response_attributes = {"request stage": "Songs List", "songs" : request.get_req_attributes("songs"), "index of song" : request.get_req_attributes("index of song"), "lyrics" : request.get_req_attributes("lyrics"), "artist": request.get_req_attributes("artist")} ''' After all manipulation, build the response and return it to Alexa ''' return build_response(message=message, end_session=end_session, card_obj=card_obj, sessionAttributes=response_attributes, reprompt_message=reprompt_message)