Closed Nazaninfarid closed 4 years ago
Hey @Nazaninfarid. Thanks for reaching out and I also did observe have you commented on one of our closed issues regarding questions on reminder API usage
You can follow the documentation of Reminders API for your use case, as you can see requestTime attribute is what you need to set.
If it helps you can use this code as reference
@Shreyas-vgr Thank you for your reply so I will try to find a way that can get the desired time from the user.
Hi @Nazaninfarid , were you able to get Reminders API working in python?
Hi @Shreyas-vgr unfortunatelly not. I have tried to change my program and add the reminder in a way that user can say remind me at {time} to {turn on/off} the device.
`from future import print_function import os import time import json import boto3
""" Functions to build Python dicts of a response message. """
def build_response(session_attributes, speechlet_response): """ Builds a Python dict of version, session, and speechlet reponse. This is the core dict (response message) to be returned by the lamabda function.
Args:
session_attributes: Python dict of session
speechlet_response: Python dict of speechlet response
Returns:
Python dict of response message
"""
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}
def build_speechlet_response(title, output, reprompt_text, should_end_session): """ Builds a Python dict of a speechlet response for the response message dict. Output speech will be read by Alexa. The card dict will be displayed if the Alexa device has a screen and can display cards. The reprompt message will be read if session remains open and there is no clear intent from the user. Should end session will close the session or allow it to remain open.
Args:
title: string of card title
output: string of output text
reprompt_text: string of reprompt text
should_end_session: boolean to end session
Returns:
Python dict of response speechlet
"""
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'Simple',
'title': title,
'content': output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
""" Handles interactions with AWS IoT Shadow through Boto. Boto is preinstalled in AWS Lambda. """
thingName = os.environ.get("AWS_IOT_MY_THING_NAME") host = os.environ.get("AWS_IOT_MQTT_HOST") port = os.environ.get("AWS_IOT_MQTT_PORT_UPDATE") topic = "$aws/things/{}/shadow/update".format(thingName)
def update_shadow(new_value_dict): """ Updates IoT shadow's "desired" state with values from new_value_dict. Logs current "desired" state after update.
Args:
new_value_dict: Python dict of values to update in shadow
"""
payload_dict = {
"state": {
"desired" : new_value_dict
}
}
JSON_payload = json.dumps(payload_dict)
shadow_client = boto3.client('iot-data', 'us-east-1')
response = shadow_client.update_thing_shadow(thingName=thingName,
payload=JSON_payload)
res_payload = json.loads(response['payload'].read().decode('utf-8'))
print("PowerState: {0}".format(res_payload.get("state").get("desired").get("power_state")))
print("sensitivity: {0}".format(res_payload.get("state").get("desired").get("sensitivity")))
print("temperature: {0}".format(res_payload.get("state").get("desired").get("temperature")))
""" Behaviors the skill will take based on the intent of the request. Calls on shadow updater to update IoT shadow accordingly and response builders to build a response message. If using sessions, session data should be handled here. """
session_attributes = {} should_end_session = True reprompt_text = None
def get_help_response(): """ Builds a help/welcome response.
Args:
None
Returns:
Python dict of response message
"""
card_title = "Welcome"
speech_output = "your security application is running"
response = build_response(session_attributes,
build_speechlet_response(card_title,
speech_output, reprompt_text, should_end_session))
return response
def update_power_state(intent): """ Updates power state of IoT shadow. Builds a response confirming the update or requesting the user repeat the query if state is not "ON" or "OFF".
Args:
intent: Python dict of intent
Returns:
Python dict of response message
"""
card_title = "Power"
power_state = intent.get('slots',{}).get('PowerState',{}).get('value')
if power_state and (power_state.upper() == 'ON' or \
power_state.upper() == 'OFF'):
speech_output = "ok the device state changed"
new_value_dict = {"power_state":power_state.upper()}
update_shadow(new_value_dict)
else:
speech_output = "I did not understand that. Please repeat your request."
response = build_response(session_attributes,
build_speechlet_response(card_title,
speech_output, reprompt_text, should_end_session))
return response
def update_sensitivity(intent): """ Updates sensitivity of IoT shadow. Builds a response confirming the update or requesting the user repeat the query if 0 <= sensitivity <= 100. If the requested sensitivity is zero, the power state of the shadow will be updated to "OFF" and the sensitivity will not be changed.
Args:
intent: Python dict of intent
Returns:
Python dict of response message
"""
card_title = "sensitivity"
sensitivity = intent.get('slots',{}).get('sensitivity',{}).get('value')
if sensitivity:
sensitivity = int(sensitivity)
if sensitivity > 0 and sensitivity <= 100:
speech_output = "Setting sensitivity to {}.".format(sensitivity)
new_value_dict = {"sensitivity":sensitivity}
update_shadow(new_value_dict)
elif sensitivity == 0:
speech_output = "Turning off."
new_value_dict = {"power_state":"OFF"}
update_shadow(new_value_dict)
else:
speech_output = "I'm sorry that value is not in the proper range. "\
"Please give me a number between 0 and 100."
else:
speech_output = "I did not understand that. Please repeat your request."
response = build_response(session_attributes,
build_speechlet_response(card_title,
speech_output, reprompt_text, should_end_session))
return response
....... ...... def handle_session_end_request(): """ Builds a response with a blank message and no session data. If using session data this function would specifically have session_attributes = {} and should_end_session = True.
Args:
None
Returns:
Python dict of response message
"""
speech_output = None
response = build_response(session_attributes,
build_speechlet_response(card_title,
speech_output, reprompt_text, should_end_session))
return response
""" Actions the skill will take based on event information, including the request and the session dicts. """ def on_session_started(session_started_request, session): """ Called when a user starts a session. Implement accordingly.
Args:
session_started_request: Python dict of request
session: Python dict of session
"""
print("on_session_started requestId=" + session_started_request['requestId']
+ ", sessionId=" + session['sessionId'])
# Update code to handle session information
def on_launch(launch_request, session): """ Called when the user launches the skill without specifying what they want.
Args:
launch_request: Python dict of request
session: Python dict of session
Returns:
Python dict of response message
"""
print("on_launch requestId=" + launch_request['requestId'] +
", sessionId=" + session['sessionId'])
return get_help_response()
def on_intent(intent_request, session): """ Called when the user specifies an intent for this skill. Calls a behavior method based on the intent type.
Args:
intent_request: Python dict of request
session: Python dict of session
Returns:
Python dict of response message
"""
print("on_intent requestId=" + intent_request['requestId'] +
", sessionId=" + session['sessionId'])
intent = intent_request['intent']
intent_name = intent['name']
if intent_name == "PowerStateIntent":
return update_power_state(intent)
elif intent_name == "sensitivityIntent":
return update_sensitivity(intent)
elif intent_name == "temperatureIntent":
return update_temperature(intent)
elif intent_name == "currentStateIntent":
return current_state()
elif intent_name == "sensitivityStatusIntent":
return current_sensitivity()
elif intent_name == "temperatureStatusIntent":
return current_temperature()
elif intent_name == "powerStatusIntent":
return current_powerState()
elif intent_name == "AMAZON.HelpIntent":
return get_help_response()
elif intent_name == "AMAZON.CancelIntent" or \
intent_name == "AMAZON.StopIntent":
return handle_session_end_request()
def on_session_ended(session_ended_request, session): """ Called when the user ends the session.
Is not called when the skill returns should_end_session=true
"""
print("on_session_ended requestId=" + session_ended_request['requestId'] +
", sessionId=" + session['sessionId'])
# Update code to handle session information
""" Main lambda function handler. The AWS Lambda function should point here to lambda_function.lambda_handler. Acts on request and session information through event_actions. Code for sessions is left for reference. """ alexa_id = os.environ.get('AWS_ALEXA_SKILLS_KIT_ID')
def lambda_handler(event, context): """ Handles event and request from Alexa Skill by using methods form the event_actions module.
Args:
event: Python dict of event, request, and session data
context: LambdaContext containing runtime data
Returns:
Python dict of response message
Rasies:
ValueError
"""
print("event.session.application.applicationId=" +
event['session']['application']['applicationId'])
# Ensure that request is from our skill
if (event['session']['application']['applicationId'] !=
alexa_id):
print(alexa_id)
raise ValueError("Invalid Application ID")
# Uncomment if storing information in sessions
# if event['session']['new']:
# event_actions.on_session_started({'requestId': event['request']['requestId']},
# event['session'])
request_type = event['request']['type']
if request_type == "LaunchRequest":
return on_launch(event['request'], event['session'])
elif request_type == "IntentRequest":
return on_intent(event['request'], event['session'])
# Uncomment if storing information in sessions
# elif request_type == "SessionEndedRequest":
# return event_actions.on_session_ended(event['request'], event['session'])`
this is actually part of my code currently it is able to turn off or on the device and do some other commands such as changing device sensitivity sensors .... but unfortunatelly I could not add the reminder to my code :/
Hi @Nazaninfarid, I see that you are not using our python ask-sdk-core libraries to implement the skill lambda code. I suggest you to try to implement your code following the Hello World sample.
I created a simple reminder API to set a one minute reminder using python sdk libraries, which you can use for reference as well. Reminders Skill.
Note: Please enable permissions for your skill before testing the reminders. :)
Let me know if you are able to successfully implement the changes and have any additional queries.
Hey @Nazaninfarid , closing this issue since there is no response. Please reopen if you have problems.
Hi I was looking for a reminder API for my custom Alexa skill in a way that for example user can say alexa remind me {subject} at {time} . most of the examples were related to node.js and I can not really find a helping code in this case. I would be appreciate if some one can help. Thank you