Last active
          December 2, 2016 17:11 
        
      - 
      
- 
        Save jamesbulpin/3a5dcd979402d31802896744b47dbd25 to your computer and use it in GitHub Desktop. 
    Alexa Skill AWS Lambda function for a talking fish
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from __future__ import print_function | |
| import urllib | |
| import urllib2 | |
| octoblu_trigger = "https://triggers.octoblu.com/v2/flows/01234567-89ab-cdef-0123-4567890abcdef/triggers/01234567-89ab-cdef-0123-4567890abcdef" | |
| # --------------- Helpers that build all of the responses ---------------------- | |
| def build_speechlet_response(output): | |
| return { | |
| 'outputSpeech': { | |
| 'type': 'PlainText', | |
| 'text': output | |
| } | |
| } | |
| def build_response(session_attributes, speechlet_response): | |
| return { | |
| 'version': '1.0', | |
| 'sessionAttributes': session_attributes, | |
| 'response': speechlet_response | |
| } | |
| # --------------- Events ------------------ | |
| def on_intent(intent_request, session): | |
| """ Called when the user specifies an intent for this skill """ | |
| print("on_intent requestId=" + intent_request['requestId'] + | |
| ", sessionId=" + session['sessionId']) | |
| intent = intent_request['intent'] | |
| intent_name = intent_request['intent']['name'] | |
| # Dispatch to your skill's intent handlers | |
| if intent_name == "BillySay": | |
| session_attributes = {} | |
| speech_output = None | |
| if ('Saying' in intent['slots']) and ('value' in intent['slots']['Saying']): | |
| obmsg = {"debug":intent} | |
| obmsg["text"] = intent['slots']['Saying']['value'] | |
| url = octoblu_trigger | |
| data = urllib.urlencode(obmsg) | |
| req = urllib2.Request(url, data) | |
| response = urllib2.urlopen(req) | |
| the_page = response.read() | |
| else: | |
| speech_output = "I don't know what to say" | |
| return build_response(session_attributes, build_speechlet_response(speech_output)) | |
| else: | |
| raise ValueError("Invalid intent") | |
| 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 | |
| # --------------- Main handler ------------------ | |
| def lambda_handler(event, context): | |
| """ Route the incoming request based on type (LaunchRequest, IntentRequest, | |
| etc.) The JSON body of the request is provided in the event parameter. | |
| """ | |
| print("event.session.application.applicationId=" + | |
| event['session']['application']['applicationId']) | |
| """ | |
| Uncomment this if statement and populate with your skill's application ID to | |
| prevent someone else from configuring a skill that sends requests to this | |
| function. | |
| """ | |
| if (event['session']['application']['applicationId'] != | |
| "amzn1.ask.skill.01234567-89ab-cdef-0123-4567890abcdef"): | |
| raise ValueError("Invalid Application ID") | |
| if event['request']['type'] == "IntentRequest": | |
| return on_intent(event['request'], event['session']) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment