Advertisement
Guest User

memes

a guest
Dec 30th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.21 KB | None | 0 0
  1. import requests
  2.  
  3.  
  4. USER_MAPPING = {'brad' : 'bradley'}
  5.  
  6.  
  7. def build_speechlet_response(title, output, reprompt_text, should_end_session):
  8. return {
  9. 'outputSpeech': {
  10. 'type': 'PlainText',
  11. 'text': output
  12. },
  13. 'card': {
  14. 'type': 'Simple',
  15. 'title': "SessionSpeechlet - " + title,
  16. 'content': "SessionSpeechlet - " + output
  17. },
  18. 'reprompt': {
  19. 'outputSpeech': {
  20. 'type': 'PlainText',
  21. 'text': reprompt_text
  22. }
  23. },
  24. 'shouldEndSession': should_end_session
  25. }
  26.  
  27.  
  28. def build_response(session_attributes, speechlet_response):
  29. return {
  30. 'version': '1.0',
  31. 'sessionAttributes': session_attributes,
  32. 'response': speechlet_response
  33. }
  34.  
  35.  
  36. def get_welcome_response():
  37.  
  38. """ If we wanted to initialize the session to have some attributes we could
  39. add those here
  40. """
  41.  
  42. session_attributes = {}
  43. card_title = "Welcome"
  44. speech_output = "Welcome to the factory," + \
  45. "Please Ask me for a task."
  46. should_end_session = False
  47. return build_response(session_attributes, build_speechlet_response(
  48. card_title, speech_output, None, should_end_session))
  49.  
  50.  
  51. def handle_session_end_request():
  52. card_title = "Session Ended"
  53. speech_output = "Have a nice day!"
  54. # Setting this to true ends the session and exits the skill.
  55. should_end_session = True
  56. return build_response({}, build_speechlet_response(
  57. card_title, speech_output, None, should_end_session))
  58.  
  59.  
  60. def on_intent(intent_request, session):
  61.  
  62. print("on_intent requestId=" + intent_request['requestId'] +
  63. ", sessionId=" + session['sessionId'])
  64. intent = intent_request['intent']
  65. intent_name = intent_request['intent']['name']
  66.  
  67. #if session_attributes != {}:
  68. #if intent_name == AMAZON.y
  69.  
  70. #print(SpecificUserName)
  71. #if intent_name == "MyColorIsIntent":
  72. # return set_color_in_session(intent, session)
  73. #elif intent_name == "WhatsMyColorIntent":
  74. # return get_color_from_session(intent, session)
  75.  
  76. if intent_name == "GatherRequests":
  77. return get_welcome_response()
  78. elif intent_name == "SpecificUser":
  79. SpecificUserName = intent_request['intent']['slots']['User']['value']
  80. return SpecificUser(SpecificUserName, session)
  81. elif intent_name == "next_tasks" and session['attributes']['hasGottenTasks']:
  82. print("TODO: next_Tasks")
  83. elif intent_name == "TaskList":
  84. return Gather_Requests()
  85. elif intent_name == "AMAZON.HelpIntent":
  86. return HelpMe(session)
  87. elif intent_name == "AMAZON.CancelIntent" or intent_name == \
  88. "AMAZON.StopIntent":
  89. return handle_session_end_request()
  90. elif intent_name == "MorningShift":
  91. ShiftTime = intent_request['intent']['slots']['EarlyMorning']['value']
  92. return SelectShift(ShiftTime)
  93. elif intent_name == "DayShift":
  94. ShiftTime = intent_request['intent']['slots']['Day']['value']
  95. print(ShiftTime)
  96. return SelectShift(ShiftTime)
  97. elif intent_name == "NightShift":
  98. ShiftTime = intent_request['intent']['slots']['name']['value']
  99. return SelectShift(ShiftTime)
  100. elif intent_name == "DidNotUnderstand":
  101. return DidNotUnderstand()
  102. elif intent_name == "Test":
  103. return UseThisToTest()
  104. else:
  105. raise ValueError("This is not a Valid Command.")
  106.  
  107.  
  108. def UseThisToTest():
  109.  
  110. session_attributes = {}
  111. card_title = "Test"
  112. speech_output = "Test Code"
  113. should_end_session = False
  114. return build_response(session_attributes, build_speechlet_response(
  115. card_title, speech_output, None, should_end_session))
  116.  
  117.  
  118. def DidNotUnderstand():
  119.  
  120. session_attributes = {}
  121. card_title = "Didn't Understand"
  122. speech_output = "Sorry i did not understand," + \
  123. "what you said, or i dont know how to" + \
  124. "use that phrase, please ask me again"
  125. should_end_session = False
  126. return build_response(session_attributes, build_speechlet_response(
  127. card_title, speech_output, None, should_end_session))
  128.  
  129.  
  130. def HelpMe(session):
  131.  
  132. session_attributes = session.get('attributes', {})
  133. print(session_attributes['usersTasks'])
  134. card_title = "Help"
  135. speech_output = "Ask me for a task," + \
  136. "Commands Include, Tasks."
  137. should_end_session = False
  138. return build_response(session_attributes, build_speechlet_response(
  139. card_title, speech_output, None, should_end_session))
  140.  
  141.  
  142. def SpecificUser(Username, session):
  143.  
  144. AuthURL = 'http://paperless-stg.nukonapps.com/api/admin/auth/user/' + \
  145. 'default/login/welcome?username=bradley&password=paperbrad123'
  146. AllTaskURL = 'http://paperless-stg.nukonapps.com/api/engine/engine/' + \
  147. 'default/filter/181/list?firstResult=0&maxResults=555'
  148.  
  149. HEADERS = {
  150. "Content-Type": "application/x-www-form-urlencoded",
  151. "Accept": "application/json"
  152. }
  153.  
  154. s = requests.Session()
  155. print(Username)
  156.  
  157. Auth = s.post(AuthURL, headers=HEADERS)
  158. response = s.get(AllTaskURL, headers=HEADERS)
  159. Tasks = response.json()
  160. #print(Tray.text)
  161. #print(Tray.json)
  162. #print("Name of Task ", obj['name'], ", Assignee Name = ", obj['assignee'])
  163. #NameOfTask = obj['name']
  164. print("Task List Length", len(Tasks))
  165. #NameOfTask0 = Tasks[0]['name']
  166. NameOfTask = Tasks
  167. CurrentAssignedTask = []
  168. Task = []
  169.  
  170. for task in Tasks:
  171. if (task['assignee'] == USER_MAPPING[Username]):
  172. CurrentAssignedTask += [task]
  173.  
  174. #print(len(CurrentAssignedTask))
  175. print(CurrentAssignedTask)
  176. #print(CurrentAssignedTask[0]['name'])
  177. #print(CurrentAssignedTask[1]['name'])
  178. #print(CurrentAssignedTask[2]['name'])
  179.  
  180. session_attributes = session.get('attributes',{})
  181. session_attributes['usersTasks'] = CurrentAssignedTask
  182.  
  183. card_title = "Gather Requests"
  184.  
  185. Username = Username.title()
  186.  
  187. LengthOfTasks = len(CurrentAssignedTask) - 5
  188. TasksLeft = str(LengthOfTasks)
  189.  
  190. # check where we are up to in asking for the current tasks asking
  191. session_attributes['usersTasklistPointer'] = 0
  192. session_attributes['hasGottenTasks'] = True
  193.  
  194. # Build the speech output
  195. if len(CurrentAssignedTask) == 1:
  196. speech_output = "Current Tasks for " + Username + " Include:, " + \
  197. CurrentAssignedTask[0]['name']
  198. elif len(CurrentAssignedTask) == 2:
  199. speech_output = "Current Tasks for " + Username + " Include:, " + \
  200. CurrentAssignedTask[0]['name'] + ", " + \
  201. CurrentAssignedTask[1]['name']
  202. elif len(CurrentAssignedTask) == 3:
  203. speech_output = "Current Tasks for " + Username + " Include:, " + \
  204. CurrentAssignedTask[0]['name'] + ", " + \
  205. CurrentAssignedTask[1]['name'] + ", " + \
  206. CurrentAssignedTask[2]['name']
  207. elif len(CurrentAssignedTask) == 4:
  208. speech_output = "Current Tasks for " + Username + " Include:, " + \
  209. CurrentAssignedTask[0]['name'] + ", " + \
  210. CurrentAssignedTask[1]['name'] + ", " + \
  211. CurrentAssignedTask[2]['name'] + ", " + \
  212. CurrentAssignedTask[3]['name']
  213. elif len(CurrentAssignedTask) >= 5 and LengthOfTasks == 1:
  214. speech_output = "Current Tasks for " + Username + " Include:, " + \
  215. CurrentAssignedTask[0]['name'] + ", " + \
  216. CurrentAssignedTask[1]['name'] + ", " + \
  217. CurrentAssignedTask[2]['name'] + ", " + \
  218. CurrentAssignedTask[3]['name'] + ", " + \
  219. CurrentAssignedTask[4]['name'] + \
  220. " there is only, " + TasksLeft + ", More task."
  221. elif len(CurrentAssignedTask) >= 5 and LengthOfTasks >= 2:
  222. speech_output = "Current Tasks for " + Username + " Include:, " + \
  223. CurrentAssignedTask[0]['name'] + ", " + \
  224. CurrentAssignedTask[1]['name'] + ", " + \
  225. CurrentAssignedTask[2]['name'] + ", " + \
  226. CurrentAssignedTask[3]['name'] + ", " + \
  227. CurrentAssignedTask[4]['name'] + " there are, " + \
  228. TasksLeft + ", More tasks."
  229. else:
  230. speech_output = Username + ", currently has no tasks assigned to them."
  231.  
  232. should_end_session = False
  233. reprompt_text = None
  234.  
  235. return build_response(session_attributes, build_speechlet_response(
  236. card_title, speech_output, reprompt_text, should_end_session))
  237.  
  238.  
  239. def Gather_Requests():
  240.  
  241. """
  242. If we wanted to initialize the session to have some attributes we could
  243. add those here
  244. """
  245. #USERNAME = intent_request['intent']['name']['slots']['USERNAME']
  246. #PASSWORD = intent_request['intent']['name']['slots']['PASSWORD']
  247. AuthURL = 'http://paperless-stg.nukonapps.com/api/admin/auth/user/' + \
  248. 'default/login/welcome?username=bradley&password=paperbrad123'
  249. #AuthURL = 'http://paperless-stg.nukonapps.com/api/admin/auth/user/default/login/welcome?username='+ USERNAME + '&password=' + PASSWORD
  250. TaskURL = 'http://paperless-stg.nukonapps.com/api/engine/engine/' + \
  251. 'default/filter/2183/list?firstResult=0&maxResults=15'
  252.  
  253. HEADERS = {
  254. "Content-Type": "application/x-www-form-urlencoded",
  255. "Accept": "application/json"
  256. }
  257.  
  258. s = requests.Session()
  259. #session_attributes = create_favorite(favorite_color)
  260. Auth = s.post(AuthURL, headers=HEADERS)
  261. Tray = s.get(TaskURL, headers=HEADERS)
  262. obj = Tray.json()
  263. #print("Name of Task ", obj['name'], ", Assignee Name = ", obj['assignee'])
  264. #NameOfTask = obj['name']
  265. print("Task List Length", len(obj))
  266. #NameOfTask0 = obj[0]['name']
  267. len(obj)
  268. NameOfTask = obj
  269. i = 0
  270. while i != len(obj) and i != 3:
  271. NameOfTask[i] = obj[i]
  272. i = i + 1
  273.  
  274. session_attributes = {}
  275. card_title = "Gather Requests"
  276.  
  277. # Build the speech output
  278. if len(obj) == 1:
  279. speech_output = 'Current Tasks Include:, ' + NameOfTask[0]['name']
  280. elif len(obj) == 2:
  281. speech_output = 'Current Tasks Include:, ' + NameOfTask[0]['name'] \
  282. + ", " + NameOfTask[1]['name']
  283. elif len(obj) == 3:
  284. speech_output = 'Current Tasks Include:, ' + \
  285. NameOfTask[0]['name'] + ", " + \
  286. NameOfTask[1]['name'] + ", and " + \
  287. NameOfTask[2]['name']
  288. else:
  289. speech_output = 'Current Task List Contains more than 3 tasks,' \
  290. + ' listing highest priority tasks, Current Tasks ' \
  291. + 'Include:, ' + \
  292. NameOfTask[0]['name'] + ", " + \
  293. NameOfTask[1]['name'] + ", and " + \
  294. NameOfTask[2]['name']
  295. should_end_session = True
  296. reprompt_text = None
  297.  
  298. return build_response(session_attributes, build_speechlet_response(
  299. card_title, speech_output, reprompt_text, should_end_session))
  300.  
  301.  
  302. def SelectShift(ShiftTime):
  303.  
  304. if ShiftTime == "Morning":
  305. pass
  306. #code to select the Form for the MorningShift
  307. if ShiftTime == "day":
  308. pass
  309. #code to select the Form for the DaytimeShift
  310. if ShiftTime == "Night":
  311. pass
  312. #code to select the Form for the DaytimeShift
  313.  
  314.  
  315.  
  316. def on_session_started(session_started_request, session):
  317.  
  318. print("on_session_started requestId=" + session_started_request
  319. ['requestId'] + ", sessionId=" + session['sessionId'])
  320.  
  321.  
  322. def on_launch(launch_request, session):
  323. """ Called when the user launches the skill without specifying what they
  324. want
  325. """
  326.  
  327. print("on_launch requestId=" + launch_request['requestId'] +
  328. ", sessionId=" + session['sessionId'])
  329. # Dispatch to your skill's launch
  330. return get_welcome_response()
  331.  
  332.  
  333. def on_session_ended(session_ended_request, session):
  334. """ Called when the user ends the session.
  335.  
  336. Is not called when the skill returns should_end_session=true
  337. """
  338. print("on_session_ended requestId=" + session_ended_request['requestId'] +
  339. ", sessionId=" + session['sessionId'])
  340. # add cleanup logic here
  341.  
  342. # Lambda Function
  343.  
  344.  
  345. def handler(event, context):
  346.  
  347. print("event.session.application.applicationId=" +
  348. event['session']['application']['applicationId'])
  349. #print(Gather_Requests())
  350. #print(SpecificUser())
  351.  
  352. if event['session']['new']:
  353. on_session_started({'requestId': event['request']['requestId']},
  354. event['session'])
  355.  
  356. if event['request']['type'] == "LaunchRequest":
  357. return on_launch(event['request'], event['session'])
  358. elif event['request']['type'] == "IntentRequest":
  359. return on_intent(event['request'], event['session'])
  360. elif event['request']['type'] == "SessionEndedRequest":
  361. return on_session_ended(event['request'], event['session'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement