Search code examples
pythonalexa-skills-kit

Alexa fetching user email using Python backend


I am trying to make an Alexa skill using a Python backend. I am using Amazon developer console to create a model and code the backend.

I want to retrieve a user email address. I tried many methods but none were working.

Everything mentioned online is for Node, and I want to make my backend in Python.

I already gave permission for email in this skill, but I'm unable to fetch email. How can I solve this issue?

import logging
import ask_sdk_core.utils as ask_utils

from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.dispatch_components import AbstractExceptionHandler
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model.services.ups import UpsServiceClient
from ask_sdk_core.api_client import DefaultApiClient
from ask_sdk_model import Response


class EmailIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("EmailIntent")(handler_input)

    def handle(self, handler_input):
        service_client_factory = handler_input.service_client_factory
        ups_service_client = UpsServiceClient(service_client_factory.get_ups_service())
        profile_email = ups_service_client.get_profile_email()
        speak_output = f"Your email is {profile_email}"
        
        return (
            handler_input.response_builder
                .speak(speak_output)
                .ask(speak_output)
                .response
        )

Solution

  • You could certainly take a look at the sample code for the Address API in python, which is a bit similar to Alexa Customer Profile API.

    And since this code is a snippet of lambda_funtion.py, I assume that you also including below code to skillBuilder object: sb.add_request_handler(EmailIntentHandler())

    It acts as the entry point for your skill, routing all request and response payloads to the handlers above. Make sure any new handlers or interceptors you've defined are included. The order matters - they're processed top to bottom.

    I would try to check any relevant cloud Watch logs for any potential runtime errors in your code.

    If you are using ASK SDK for python You can find additional information on how to implement the Customer Profile API on this page, including some code samples.

    And also assuming skill is properly configured to request customer contact information and permission as documented here