Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- USER_MAPPING = {'brad' : 'bradley'}
- def build_speechlet_response(title, output, reprompt_text, should_end_session):
- return {
- 'outputSpeech': {
- 'type': 'PlainText',
- 'text': output
- },
- 'card': {
- 'type': 'Simple',
- 'title': "SessionSpeechlet - " + title,
- 'content': "SessionSpeechlet - " + output
- },
- 'reprompt': {
- 'outputSpeech': {
- 'type': 'PlainText',
- 'text': reprompt_text
- }
- },
- 'shouldEndSession': should_end_session
- }
- def build_response(session_attributes, speechlet_response):
- return {
- 'version': '1.0',
- 'sessionAttributes': session_attributes,
- 'response': speechlet_response
- }
- def get_welcome_response():
- """ If we wanted to initialize the session to have some attributes we could
- add those here
- """
- session_attributes = {}
- card_title = "Welcome"
- speech_output = "Welcome to the factory," + \
- "Please Ask me for a task."
- should_end_session = False
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, None, should_end_session))
- def handle_session_end_request():
- card_title = "Session Ended"
- speech_output = "Have a nice day!"
- # Setting this to true ends the session and exits the skill.
- should_end_session = True
- return build_response({}, build_speechlet_response(
- card_title, speech_output, None, should_end_session))
- def on_intent(intent_request, session):
- print("on_intent requestId=" + intent_request['requestId'] +
- ", sessionId=" + session['sessionId'])
- intent = intent_request['intent']
- intent_name = intent_request['intent']['name']
- #if session_attributes != {}:
- #if intent_name == AMAZON.y
- #print(SpecificUserName)
- #if intent_name == "MyColorIsIntent":
- # return set_color_in_session(intent, session)
- #elif intent_name == "WhatsMyColorIntent":
- # return get_color_from_session(intent, session)
- if intent_name == "GatherRequests":
- return get_welcome_response()
- elif intent_name == "SpecificUser":
- SpecificUserName = intent_request['intent']['slots']['User']['value']
- return SpecificUser(SpecificUserName, session)
- elif intent_name == "next_tasks" and session['attributes']['hasGottenTasks']:
- print("TODO: next_Tasks")
- elif intent_name == "TaskList":
- return Gather_Requests()
- elif intent_name == "AMAZON.HelpIntent":
- return HelpMe(session)
- elif intent_name == "AMAZON.CancelIntent" or intent_name == \
- "AMAZON.StopIntent":
- return handle_session_end_request()
- elif intent_name == "MorningShift":
- ShiftTime = intent_request['intent']['slots']['EarlyMorning']['value']
- return SelectShift(ShiftTime)
- elif intent_name == "DayShift":
- ShiftTime = intent_request['intent']['slots']['Day']['value']
- print(ShiftTime)
- return SelectShift(ShiftTime)
- elif intent_name == "NightShift":
- ShiftTime = intent_request['intent']['slots']['name']['value']
- return SelectShift(ShiftTime)
- elif intent_name == "DidNotUnderstand":
- return DidNotUnderstand()
- elif intent_name == "Test":
- return UseThisToTest()
- else:
- raise ValueError("This is not a Valid Command.")
- def UseThisToTest():
- session_attributes = {}
- card_title = "Test"
- speech_output = "Test Code"
- should_end_session = False
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, None, should_end_session))
- def DidNotUnderstand():
- session_attributes = {}
- card_title = "Didn't Understand"
- speech_output = "Sorry i did not understand," + \
- "what you said, or i dont know how to" + \
- "use that phrase, please ask me again"
- should_end_session = False
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, None, should_end_session))
- def HelpMe(session):
- session_attributes = session.get('attributes', {})
- print(session_attributes['usersTasks'])
- card_title = "Help"
- speech_output = "Ask me for a task," + \
- "Commands Include, Tasks."
- should_end_session = False
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, None, should_end_session))
- def SpecificUser(Username, session):
- AuthURL = 'http://paperless-stg.nukonapps.com/api/admin/auth/user/' + \
- 'default/login/welcome?username=bradley&password=paperbrad123'
- AllTaskURL = 'http://paperless-stg.nukonapps.com/api/engine/engine/' + \
- 'default/filter/181/list?firstResult=0&maxResults=555'
- HEADERS = {
- "Content-Type": "application/x-www-form-urlencoded",
- "Accept": "application/json"
- }
- s = requests.Session()
- print(Username)
- Auth = s.post(AuthURL, headers=HEADERS)
- response = s.get(AllTaskURL, headers=HEADERS)
- Tasks = response.json()
- #print(Tray.text)
- #print(Tray.json)
- #print("Name of Task ", obj['name'], ", Assignee Name = ", obj['assignee'])
- #NameOfTask = obj['name']
- print("Task List Length", len(Tasks))
- #NameOfTask0 = Tasks[0]['name']
- NameOfTask = Tasks
- CurrentAssignedTask = []
- Task = []
- for task in Tasks:
- if (task['assignee'] == USER_MAPPING[Username]):
- CurrentAssignedTask += [task]
- #print(len(CurrentAssignedTask))
- print(CurrentAssignedTask)
- #print(CurrentAssignedTask[0]['name'])
- #print(CurrentAssignedTask[1]['name'])
- #print(CurrentAssignedTask[2]['name'])
- session_attributes = session.get('attributes',{})
- session_attributes['usersTasks'] = CurrentAssignedTask
- card_title = "Gather Requests"
- Username = Username.title()
- LengthOfTasks = len(CurrentAssignedTask) - 5
- TasksLeft = str(LengthOfTasks)
- # check where we are up to in asking for the current tasks asking
- session_attributes['usersTasklistPointer'] = 0
- session_attributes['hasGottenTasks'] = True
- # Build the speech output
- if len(CurrentAssignedTask) == 1:
- speech_output = "Current Tasks for " + Username + " Include:, " + \
- CurrentAssignedTask[0]['name']
- elif len(CurrentAssignedTask) == 2:
- speech_output = "Current Tasks for " + Username + " Include:, " + \
- CurrentAssignedTask[0]['name'] + ", " + \
- CurrentAssignedTask[1]['name']
- elif len(CurrentAssignedTask) == 3:
- speech_output = "Current Tasks for " + Username + " Include:, " + \
- CurrentAssignedTask[0]['name'] + ", " + \
- CurrentAssignedTask[1]['name'] + ", " + \
- CurrentAssignedTask[2]['name']
- elif len(CurrentAssignedTask) == 4:
- speech_output = "Current Tasks for " + Username + " Include:, " + \
- CurrentAssignedTask[0]['name'] + ", " + \
- CurrentAssignedTask[1]['name'] + ", " + \
- CurrentAssignedTask[2]['name'] + ", " + \
- CurrentAssignedTask[3]['name']
- elif len(CurrentAssignedTask) >= 5 and LengthOfTasks == 1:
- speech_output = "Current Tasks for " + Username + " Include:, " + \
- CurrentAssignedTask[0]['name'] + ", " + \
- CurrentAssignedTask[1]['name'] + ", " + \
- CurrentAssignedTask[2]['name'] + ", " + \
- CurrentAssignedTask[3]['name'] + ", " + \
- CurrentAssignedTask[4]['name'] + \
- " there is only, " + TasksLeft + ", More task."
- elif len(CurrentAssignedTask) >= 5 and LengthOfTasks >= 2:
- speech_output = "Current Tasks for " + Username + " Include:, " + \
- CurrentAssignedTask[0]['name'] + ", " + \
- CurrentAssignedTask[1]['name'] + ", " + \
- CurrentAssignedTask[2]['name'] + ", " + \
- CurrentAssignedTask[3]['name'] + ", " + \
- CurrentAssignedTask[4]['name'] + " there are, " + \
- TasksLeft + ", More tasks."
- else:
- speech_output = Username + ", currently has no tasks assigned to them."
- should_end_session = False
- reprompt_text = None
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, reprompt_text, should_end_session))
- def Gather_Requests():
- """
- If we wanted to initialize the session to have some attributes we could
- add those here
- """
- #USERNAME = intent_request['intent']['name']['slots']['USERNAME']
- #PASSWORD = intent_request['intent']['name']['slots']['PASSWORD']
- AuthURL = 'http://paperless-stg.nukonapps.com/api/admin/auth/user/' + \
- 'default/login/welcome?username=bradley&password=paperbrad123'
- #AuthURL = 'http://paperless-stg.nukonapps.com/api/admin/auth/user/default/login/welcome?username='+ USERNAME + '&password=' + PASSWORD
- TaskURL = 'http://paperless-stg.nukonapps.com/api/engine/engine/' + \
- 'default/filter/2183/list?firstResult=0&maxResults=15'
- HEADERS = {
- "Content-Type": "application/x-www-form-urlencoded",
- "Accept": "application/json"
- }
- s = requests.Session()
- #session_attributes = create_favorite(favorite_color)
- Auth = s.post(AuthURL, headers=HEADERS)
- Tray = s.get(TaskURL, headers=HEADERS)
- obj = Tray.json()
- #print("Name of Task ", obj['name'], ", Assignee Name = ", obj['assignee'])
- #NameOfTask = obj['name']
- print("Task List Length", len(obj))
- #NameOfTask0 = obj[0]['name']
- len(obj)
- NameOfTask = obj
- i = 0
- while i != len(obj) and i != 3:
- NameOfTask[i] = obj[i]
- i = i + 1
- session_attributes = {}
- card_title = "Gather Requests"
- # Build the speech output
- if len(obj) == 1:
- speech_output = 'Current Tasks Include:, ' + NameOfTask[0]['name']
- elif len(obj) == 2:
- speech_output = 'Current Tasks Include:, ' + NameOfTask[0]['name'] \
- + ", " + NameOfTask[1]['name']
- elif len(obj) == 3:
- speech_output = 'Current Tasks Include:, ' + \
- NameOfTask[0]['name'] + ", " + \
- NameOfTask[1]['name'] + ", and " + \
- NameOfTask[2]['name']
- else:
- speech_output = 'Current Task List Contains more than 3 tasks,' \
- + ' listing highest priority tasks, Current Tasks ' \
- + 'Include:, ' + \
- NameOfTask[0]['name'] + ", " + \
- NameOfTask[1]['name'] + ", and " + \
- NameOfTask[2]['name']
- should_end_session = True
- reprompt_text = None
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, reprompt_text, should_end_session))
- def SelectShift(ShiftTime):
- if ShiftTime == "Morning":
- pass
- #code to select the Form for the MorningShift
- if ShiftTime == "day":
- pass
- #code to select the Form for the DaytimeShift
- if ShiftTime == "Night":
- pass
- #code to select the Form for the DaytimeShift
- def on_session_started(session_started_request, session):
- print("on_session_started requestId=" + session_started_request
- ['requestId'] + ", sessionId=" + session['sessionId'])
- def on_launch(launch_request, session):
- """ Called when the user launches the skill without specifying what they
- want
- """
- print("on_launch requestId=" + launch_request['requestId'] +
- ", sessionId=" + session['sessionId'])
- # Dispatch to your skill's launch
- return get_welcome_response()
- 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'])
- # add cleanup logic here
- # Lambda Function
- def handler(event, context):
- print("event.session.application.applicationId=" +
- event['session']['application']['applicationId'])
- #print(Gather_Requests())
- #print(SpecificUser())
- if event['session']['new']:
- on_session_started({'requestId': event['request']['requestId']},
- event['session'])
- if event['request']['type'] == "LaunchRequest":
- return on_launch(event['request'], event['session'])
- elif event['request']['type'] == "IntentRequest":
- return on_intent(event['request'], event['session'])
- elif event['request']['type'] == "SessionEndedRequest":
- return on_session_ended(event['request'], event['session'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement