Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import urllib2
- import json
- from operator import itemgetter
- from decimal import Decimal
- material_query = 'XXXYOUR_SERVICE_CALL&$format=json'
- customer_query = 'XXXYOUR_OTHER_SERVICE_CALL&$format=json'
- # --------------- Helpers that build all of the responses ----------------------
- 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
- }
- # --------------- Functions that control the skill's behavior ------------------
- def get_welcome_response():
- session_attributes = {}
- card_title = "Welcome"
- speech_output = "Welcome to the Mindset Dashboard. " \
- "Ask a question about your business, such as " \
- "'What are my best Materials?' Or, 'Which customers "\
- "are buying the most product?'"
- # If the user either does not reply to the welcome message or says something
- # that is not understood, they will be prompted again with this text.
- reprompt_text = "Ask about your business data, especially customers and materials."
- should_end_session = False
- return build_response(session_attributes, build_speechlet_response(
- card_title, speech_output, reprompt_text, should_end_session))
- def get_dashboard(intent, query_name, session):
- session_attributes = {}
- reprompt_text = None
- opener = urllib2.build_opener()
- opener.addheaders = [('Authorization', 'Basic DONT_USE_MINE_USE_YOUR_OWN'),
- ('Connection', 'keep-alive'),
- ('X-Requested-With', 'XMLHttpRequest'),
- ('Accept', 'application/json')]
- if query_name == 'materials':
- result = opener.open(material_query).read()
- else:
- result = opener.open(customer_query).read()
- result_json =json.loads(result)
- """Here's where we handle the response to the request.
- NOTE: this will DEFINITELY not work for your request. The
- particular format of the Mindset Analytics App OData service
- drives this code.
- key = ''
- value = ''
- unsorted_dict = {}
- for index, item in enumerate(result_json['d']['results']):
- if index % 2 == 0:
- key = item['Data']
- else:
- try:
- value = Decimal(item['Data'].replace(',',''))
- except:
- value = 0.0
- if key and value:
- unsorted_dict[key] = value
- key = ''
- value = ''
- sorted_full_result = sorted(unsorted_dict, key=unsorted_dict.get, reverse=True)
- sorted_result = sorted_full_result[:4]
- """
- #Build the response
- if len(sorted_result) > 0:
- should_end_session = True
- if query_name == 'materials':
- speech_output = 'The top 4 best-selling materials at your plant are: '
- else:
- speech_output = 'The top 4 customers by volume purchased are: '
- for index, result in enumerate(sorted_result):
- if index < 3:
- speech_output += result + ', '
- else:
- speech_output += 'and ' + result + '.'
- speech_output = speech_output.replace('"', '').replace('&', 'and')
- return build_response(session_attributes, build_speechlet_response(
- intent['name'], speech_output, reprompt_text, should_end_session))
- # --------------- Events ------------------
- def on_launch(launch_request, session):
- return get_welcome_response()
- def on_intent(intent_request, session):
- intent = intent_request['intent']
- intent_name = intent_request['intent']['name']
- query_name = intent['slots']['Query']['value']
- # Dispatch to your skill's intent handlers
- if intent_name == "GetDashboard":
- return get_dashboard(intent, query_name, session)
- elif intent_name == "AMAZON.HelpIntent":
- return get_welcome_response()
- elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
- return handle_session_end_request()
- else:
- raise ValueError("Invalid intent")
- # --------------- Main handler ------------------
- def lambda_handler(event, context):
- 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