[Python] How can I send an alexa notification via proactive events ask_sdk_core python?

Discussão em 'Python' iniciado por Stack, Fevereiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to send a notification from my skill to Alexa using proactive events through the ask_sdk_model python interface.

    At the moment I'm trying to test with the Alexa developer console. I've retrieved my client id/secret from the Alexa developer console, and managed to determine the user_id of the developer console.

    I have the following, but it's giving me an ambiguous "Bad Reqeust" error:

    from ask_sdk_core.api_client import DefaultApiClient
    from ask_sdk_core.serialize import DefaultSerializer
    from ask_sdk_model.services import ApiConfiguration, AuthenticationConfiguration
    from ask_sdk_model.services.proactive_events import CreateProactiveEventRequest, RelevantAudience, RelevantAudienceType, SkillStage

    ALEXA_CLIENT_ID = "XXX"
    ALEXA_CLIENT_SECRET = "YYY"


    def notify_alexa_device(user_id: int):
    api_configuration = ApiConfiguration(serializer=DefaultSerializer(), api_client=DefaultApiClient())
    authentication_configuration = AuthenticationConfiguration(
    client_id=ALEXA_CLIENT_ID,
    client_secret=ALEXA_CLIENT_SECRET,
    )
    client = ProactiveEventsServiceClient(
    api_configuration,
    authentication_configuration
    )
    notification_event = Event(
    name="AMAZON.Occasion.Updated",
    payload={
    "state": {"confirmationStatus": "CONFIRMED"},
    "occasion": {
    "occasionType": "APPOINTMENT",
    "subject": "localizedattribute:subject",
    "provider": {"name": "localizedattribute:providerName"},
    "bookingTime": "2018-11-20T19:16:31Z",
    "broker": {"name": "localizedattribute:brokerName"},
    },
    }
    )
    localized_attributes = [
    {"locale": "en-US", "providerName": "Provider Name", "subject": "Subject", "brokerName": "Broker Name"},
    {"locale": "ja-JP", "providerName": "プロバイダー名", "subject": "件名", "brokerName": "ブローカー名"},
    ]
    event_request = CreateProactiveEventRequest(
    event=notification_event,
    localized_attributes=localized_attributes,
    relevant_audience=RelevantAudience(object_type=RelevantAudienceType.Unicast, payload=[str(user_id)]),
    )
    client.create_proactive_event(
    create_proactive_event_request=event_request,
    stage=SkillStage.DEVELOPMENT,
    )



    Any idea what is causing the "Bad Request", and how I can initiate a proactive event with ask_sdk_model?

    Exception:

    File "visitors/functions.py", line 62, in notify_alexa_device
    client.create_proactive_event(
    File "7qDsB67C/lib/python3.12/site-packages/ask_sdk_model/services/proactive_events/proactive_events_service_client.py", line 118, in create_proactive_event
    access_token = self._lwa_service_client.get_access_token_for_scope(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "7qDsB67C/lib/python3.12/site-packages/ask_sdk_model/services/lwa/lwa_client.py", line 111, in get_access_token_for_scope
    return self._get_access_token(scope)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "7qDsB67C/lib/python3.12/site-packages/ask_sdk_model/services/lwa/lwa_client.py", line 155, in _get_access_token
    lwa_response = self._generate_access_token(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "7qDsB67C/lib/python3.12/site-packages/ask_sdk_model/services/lwa/lwa_client.py", line 234, in _generate_access_token
    api_response = self.invoke(method="POST", endpoint=endpoint,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/lib/python3.12/site-packages/ask_sdk_model/services/base_service_client.py", line 151, in invoke
    raise ServiceException(message=exception_metadata.message,
    ask_sdk_model.services.service_exception.ServiceException: Bad Reques

    Continue reading...

Compartilhe esta Página