def handle(self, handler_input): language_prompts = handler_input.attributes_manager.request_attributes[ "_"] skill_name = language_prompts["SKILL_NAME"] account_linking_token = get_account_linking_access_token(handler_input) if account_linking_token is not None: url = "https://api.amazon.com/user/profile?access_token={}".format( account_linking_token) user_data = requests.get(url).json() user_name = user_data['name'] speech_output = random.choice( language_prompts["GREETING"]).format(user_name) reprompt = random.choice(language_prompts["GREETING_REPROMPT"]) card_data = SimpleCard(skill_name, speech_output) else: speech_output = random.choice( language_prompts["GREETING_UNKNOWN_USER"]).format(skill_name) reprompt = random.choice( language_prompts["GREETING_UNKNOWN_USER_REPROMPT"]) card_data = LinkAccountCard() return (handler_input.response_builder.speak(speech_output).ask( reprompt).set_card(card_data).response)
def handle(self, handler_input, with_auth=True): try: self.handler_input = handler_input self.response_builder = handler_input.response_builder self.req_envelope = handler_input.request_envelope self.device_id = self.req_envelope.context.system.device.device_id with configure_scope() as scope: scope.user = {"id": self.req_envelope.session.user.user_id} scope.set_tag("handler", self.__class__.__name__[:-7]) scope.set_extra("context", self.req_envelope.context.to_dict()) scope.set_extra("request", self.req_envelope.request.to_dict()) if (with_auth and not self.req_envelope.session.user or not self.req_envelope.session.user.access_token): if isinstance(self.req_envelope.request, CanFulfillIntentRequest): # self.can_fulfill_intent(maybe=True) return None self.response_builder.speak(NOTIFY_LINK_ACCOUNT).set_card( LinkAccountCard()) return self.response_builder.response self.token = self.req_envelope.session.user.access_token return None except Exception as exc: logger.exception(exc) raise exc
def completed_interview_me_intent_handler(handler_input): # Grab the access token access_token = handler_input.request_envelope.session.user.access_token interview_text = "Interview with Family History Skill, " + datetime.datetime.now().strftime("%A, %x") + ":\n" slot_questions = {'grandmas_house_color': 'What color was your grandmother\'s house?', 'grandmas_favorite_game': 'What was your grandmother\'s favorite game?', 'grandmas_favorite_dessert': 'What was your grandma\'s favorite dessert?'} for slot in handler_input.request_envelope.request.intent.slots: interview_text += "Family History: " interview_text += slot_questions[slot] + "\n" interview_text += "User: "******"\n\n") FS = FSDecorator(access_token).getInstance() title = "Interview" + datetime.datetime.now().strftime(" %x") try: speech_text = FS.postMemory(interview_text, title) except httpError401Exception, e: # This is where we reauthenticate because we got a 401 response. speech_text = "Your session has expired. Please proceed to the Alexa app to sign in again using the Link Account button." return handler_input.response_builder.speak(speech_text).set_card( LinkAccountCard()).set_should_end_session(False).response
def process(self, handler_input, response): if handler_input.request_envelope.request.object_type != "CanFulfillIntentRequest": sess_attrs = handler_input.attributes_manager.session_attributes print(sess_attrs) if sess_attrs.get("LINK_ACCOUNT_CARD"): response.card = LinkAccountCard() else: response.card = SimpleCard(title=Constants.SKILL_NAME, content=convert_speech_to_text( response.output_speech.ssml))
def handle(self, handler_input): # type: (HandlerInput) -> Response resp = handler_input.response_builder token, speech_text, authproblem, sp = utils.login(handler_input) if authproblem: resp.set_card(LinkAccountCard()) else: speech_text = utils.get_current_context(sp) return resp.speak(speech_text).response
def handle(self, handler_input, exception): logger.exception(exception) capture_exception(exception) if exception.response.status_code in (401, 403): handler_input.response_builder.speak( NOTIFY_RELINK_ACCOUNT).set_card(LinkAccountCard()) else: handler_input.response_builder.speak(BLEND_FAILURE).ask( BLEND_FAILURE) return handler_input.response_builder.response
def handle(self, handler_input): # type: (HandlerInput) -> Response resp = handler_input.response_builder token, speech_text, authproblem, sp = utils.login(handler_input) if authproblem: resp.set_card(LinkAccountCard()) else: controlparams = {'country': None, 'album_type': None, 'limit': 20, 'offset': 0} controlpayload = None controlurl = 'https://api.spotify.com/v1/me/player/%s' sp._internal_call('POST', controlurl % "next", controlpayload, controlparams) return None
def handle(self, handler_input, exception): logger.exception(exception) capture_exception(exception) if isinstance(handler_input.request_envelope.request, CanFulfillIntentRequest): return handler_input.response_builder.response if exception.status_code == 403: handler_input.response_builder.speak(NOTIFY_LINK_ACCOUNT).set_card( LinkAccountCard()) else: handler_input.response_builder.speak(BLEND_FAILURE).ask( BLEND_FAILURE) return handler_input.response_builder.response
def before_starting_interview_me_intent_handler(handler_input): # Grab the access token access_token = handler_input.request_envelope.session.user.access_token FS = FSDecorator(access_token).getInstance() try: speech_text = FS.getMemory() except httpResponseCode204Exception: # Do nothing, a 204 means that there were simply no stories in Memories to return pass except httpError401Exception, e: # This is where we reauthenticate because we got a 401 response. speech_text = "Your session has expired. Please proceed to the Alexa app to sign in again using the Link Account button." return handler_input.response_builder.speak(speech_text).set_card( LinkAccountCard()).set_should_end_session(False).response
def handle(self, handler_input: HandlerInput, exception: Exception) -> Response: logger.error(exception, exc_info=True) if type(exception) in (AccountNotLinkedError, AuthenticationError): handler_input.response_builder.speak( 'Please log into your Tesla account in the Alexa app.' ).set_card(LinkAccountCard()).set_should_end_session(True) elif type(exception) == VehicleAsleepError: exception.car.wake_up() speech = '{} was sleeping. Wait a moment and try again.'.format( exception.car.display_name) handler_input.response_builder.speak( speech).set_should_end_session(True) else: speech = 'Sorry, something went wrong. Goodbye..' handler_input.response_builder.speak( speech).set_should_end_session(True) return handler_input.response_builder.response
def read_history_intent_handler(handler_input): """ Read a story from FamilySearch """ # Grab the session attributes # session_attributes = handler_input.attributes_manager.session_attributes # Grab the access token access_token = handler_input.request_envelope.session.user.access_token FS = FSDecorator(access_token).getInstance() try: speech_text = FS.getMemory() except httpResponseCode204Exception: # There were no memories on familysearch speech_text = "No stories were found on Family Search - try saying Interview Me." except httpError401Exception, e: # This is where we reauthenticate because we got a 401 response. speech_text = "Your session has expired. Please proceed to the Alexa app to sign in again using the Link Account button." return handler_input.response_builder.speak(speech_text).set_should_end_session(False).set_card( LinkAccountCard()).response
def completed_ask_for_a_memory_intent_handler(handler_input): """ Grab raw user text from AMAZON.custom_slot and write it to FS as a memory. """ # Grab the access token access_token = handler_input.request_envelope.session.user.access_token # Grab the intent intent = handler_input.request_envelope.request.intent # Grab the slots story = intent.slots['story'].value title = intent.slots['title'].value FS = FSDecorator(access_token).getInstance() try: speech_text = FS.postMemory(story,title) except httpError401Exception, e: # This is where we reauthenticate because we got a 401 response. speech_text = "Your session has expired. Please proceed to the Alexa app to sign in again using the Link Account button." return handler_input.response_builder.speak(speech_text).set_card( LinkAccountCard()).set_should_end_session(False).response
try: # We are just getting this memory to verify that the user is authenticated. Speech_text will be discarded speech_text = FS.getMemory() except httpResponseCode204Exception: # Do nothing, a 204 means that there were simply no stories in Memories to return pass except httpError401Exception, e: # This is where we reauthenticate because we got a 401 response. speech_text = "Your session has expired. Please proceed to the Alexa app to sign in again using the Link Account button." return handler_input.response_builder.speak(speech_text).set_card( LinkAccountCard()).set_should_end_session(False).response except httpError403Exception, e: # This is where we reauthenticate because we got a 403 response. speech_text = "Your session has expired. Please proceed to the Alexa app to sign in again using the Link Account button." return handler_input.response_builder.speak(speech_text).set_card( LinkAccountCard()).set_should_end_session(False).response except TypeError, e: # This is where we authenticate because a brand new user has no access token whatsoever, so he got a type error. speech_text = "Welcome to Family History! To get the most out of this Skill, please link your account." return handler_input.response_builder.speak(speech_text).set_card( LinkAccountCard()).set_should_end_session(False).response # We are intentionally not catching httpErrorUnhandledException my_delegate_directive = dialog.delegate_directive.DelegateDirective() return handler_input.response_builder.add_directive(my_delegate_directive).set_should_end_session(False).response @sb.request_handler( can_handle_func=lambda handler_input: handler_input.request_envelope.request.intent.name == "ask_for_a_memory" and handler_input.request_envelope.request.dialog_state.value == "IN_PROGRESS") def in_progress_ask_for_a_memory_intent_handler(handler_input):
def authorize(handler_input): resp = handler_input.response_builder token, speech_text, authproblem, sp = login(handler_input) if authproblem: resp.set_card(LinkAccountCard())