Last active
November 18, 2017 15:01
-
-
Save chroju/97201c49a888533f3413a081013ea706 to your computer and use it in GitHub Desktop.
AlexaにAWSの請求金額を教えてもらう
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
| { | |
| "languageModel": { | |
| "types": [ | |
| { | |
| "name": "Service", | |
| "values": [ | |
| { | |
| "id": null, | |
| "name": { | |
| "value": "イーシーツー", | |
| "synonyms": [] | |
| } | |
| }, | |
| { | |
| "id": null, | |
| "name": { | |
| "value": "エススリー", | |
| "synonyms": [] | |
| } | |
| }, | |
| { | |
| "id": null, | |
| "name": { | |
| "value": "ラムダ", | |
| "synonyms": [] | |
| } | |
| } | |
| ] | |
| } | |
| ], | |
| "intents": [ | |
| { | |
| "name": "AMAZON.CancelIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "AMAZON.HelpIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "AMAZON.StopIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "GetBillingIntent", | |
| "samples": [ | |
| "支払い額を教えて", | |
| "請求額を教えて", | |
| "支払い額を確認して", | |
| "請求額を確認して", | |
| "支払いはいくら", | |
| "請求額はいくら", | |
| "{service} はいくら", | |
| "{service} の支払いはいくら", | |
| "{service} の請求額を教えて", | |
| "{service} の支払いを確認して" | |
| ], | |
| "slots": [ | |
| { | |
| "name": "service", | |
| "type": "Service" | |
| } | |
| ] | |
| } | |
| ], | |
| "invocationName": "ビリング" | |
| } | |
| } |
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
| # -*- coding: utf-8 -*- | |
| from __future__ import print_function | |
| import boto3 | |
| import datetime | |
| import os | |
| client = boto3.client('cloudwatch', region_name='us-east-1') | |
| application_id = os.getenv('APPLICATION_ID') | |
| service_list = { | |
| u"イーシーツー": "AmazonEC2", | |
| u"ラムダ": "AWSLambda", | |
| u"エススリー": "AmazonS3" | |
| } | |
| # --------------- 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': "Get Billing", | |
| 'content': 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_billing(dimensions): | |
| return client.get_metric_statistics( | |
| Namespace='AWS/Billing', | |
| MetricName='EstimatedCharges', | |
| Dimensions=dimensions, | |
| StartTime=datetime.datetime.now() - datetime.timedelta(hours=4), | |
| EndTime=datetime.datetime.now(), | |
| Period=3600, | |
| Statistics=['Maximum'] | |
| )[u'Datapoints'][0][u'Maximum'] | |
| def get_welcome_response(): | |
| card_title = "Welcome" | |
| should_end_session = False | |
| reprompt_text = "請求金額の合計を確認しますか。" \ | |
| "もしくはサービス別の請求金額を知りたい場合は、サービス名を教えてください。" | |
| speech_output = "ビリングを開きました。AWSの請求金額を合計か、サービスごとに確認できます。" + reprompt_text | |
| return build_response({}, build_speechlet_response( | |
| card_title, speech_output, reprompt_text, should_end_session)) | |
| def get_billing_response(intent, session): | |
| card_title = "Get billing" | |
| should_end_session = True | |
| dimensions = [ | |
| { | |
| 'Name': 'Currency', | |
| 'Value': 'USD' | |
| } | |
| ] | |
| if 'service' in intent['slots']: | |
| service_name = intent['slots']['service']['value'] | |
| if service_name in service_list: | |
| dimensions.append({'Name': 'ServiceName', 'Value': service_list[service_name]}) | |
| speech_output = u"{}の利用金額は{}だらーです".format(service_name, get_billing(dimensions)) | |
| else: | |
| speech_output = u"すみません。そのサービスには対応していません。" | |
| else: | |
| speech_output = u"合計利用金額は{}だらーです".format(get_billing(dimensions)) | |
| return build_response({}, build_speechlet_response( | |
| card_title, speech_output, None, should_end_session)) | |
| def handle_session_end_request(): | |
| card_title = "Session Ended" | |
| speech_output = "ビリングを終了します。" | |
| # 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)) | |
| # --------------- Events ------------------ | |
| def on_launch(launch_request, session): | |
| print("on_launch requestId=" + launch_request['requestId'] + | |
| ", sessionId=" + session['sessionId']) | |
| # Dispatch to your skill's launch | |
| return get_welcome_response() | |
| def on_help(launch_request, session): | |
| print("on_launch requestId=" + launch_request['requestId'] + | |
| ", sessionId=" + session['sessionId']) | |
| # Dispatch to your skill's launch | |
| return get_welcome_response() | |
| 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'] | |
| # Dispatch to your skill's intent handlers | |
| if intent_name == "GetBillingIntent": | |
| return get_billing_response(intent, 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") | |
| def on_session_started(session_started_request, session): | |
| print("on_session_started requestId=" + session_started_request['requestId'] | |
| + ", sessionId=" + session['sessionId']) | |
| def on_session_ended(session_ended_request, session): | |
| print("on_session_ended requestId=" + session_ended_request['requestId'] + | |
| ", sessionId=" + session['sessionId']) | |
| # add cleanup logic here | |
| # --------------- Main handler ------------------ | |
| def lambda_handler(event, context): | |
| print("event.session.application.applicationId=" + | |
| event['session']['application']['applicationId']) | |
| if (event['session']['application']['applicationId'] != application_id): | |
| raise ValueError("Invalid Application ID") | |
| 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']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment