Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this is link to ec2 stop https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
- this is like to the github https://github.com/pyraven/alexa-admin-guide/blob/master/lambda_function.py
- and this is my code :)
- ====
- # -*- coding: utf-8 -*-
- # This is a simple Hello World Alexa Skill, built using
- # the implementation of handler classes approach in skill builder.
- import logging
- from ask_sdk_core.skill_builder import SkillBuilder
- from ask_sdk_core.dispatch_components import AbstractRequestHandler
- from ask_sdk_core.dispatch_components import AbstractExceptionHandler
- from ask_sdk_core.utils import is_request_type, is_intent_name
- from ask_sdk_core.handler_input import HandlerInput
- from ask_sdk_model.ui import SimpleCard
- from ask_sdk_model import Response
- sb = SkillBuilder()
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.INFO)
- class LaunchRequestHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> bool
- return is_request_type("LaunchRequest")(handler_input)
- def handle(self, handler_input):
- # type: (HandlerInput) -> Response
- speech_text = "Welcome to the Alexa Skills Kit, you can say hello!"
- handler_input.response_builder.speak(speech_text).set_card(
- SimpleCard("Hello World", speech_text)).set_should_end_session(
- False)
- return handler_input.response_builder.response
- class HelloWorldIntentHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> bool
- return is_intent_name("HelloWorldIntent")(handler_input)
- def handle(self, handler_input):
- # type: (HandlerInput) -> Response
- speech_text = "Hello Python Gurus from Classes!"
- handler_input.response_builder.speak(speech_text).set_card(
- SimpleCard("Hello World", speech_text)).set_should_end_session(
- True)
- return handler_input.response_builder.response
- class HelpIntentHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> bool
- return is_intent_name("AMAZON.HelpIntent")(handler_input)
- def handle(self, handler_input):
- # type: (HandlerInput) -> Response
- speech_text = "You can say hello to me!"
- handler_input.response_builder.speak(speech_text).ask(
- speech_text).set_card(SimpleCard(
- "Hello World", speech_text))
- return handler_input.response_builder.response
- class CancelOrStopIntentHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> bool
- return (is_intent_name("AMAZON.CancelIntent")(handler_input) or
- is_intent_name("AMAZON.StopIntent")(handler_input))
- def handle(self, handler_input):
- # type: (HandlerInput) -> Response
- speech_text = "Goodbye!"
- handler_input.response_builder.speak(speech_text).set_card(
- SimpleCard("Hello World", speech_text))
- return handler_input.response_builder.response
- class FallbackIntentHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> bool
- return is_intent_name("AMAZON.FallbackIntent")(handler_input)
- def handle(self, handler_input):
- # type: (HandlerInput) -> Response
- speech_text = (
- "The Hello World skill can't help you with that. "
- "You can say hello!!")
- reprompt = "You can say hello!!"
- handler_input.response_builder.speak(speech_text).ask(reprompt)
- return handler_input.response_builder.response
- class SessionEndedRequestHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> bool
- return is_request_type("SessionEndedRequest")(handler_input)
- def handle(self, handler_input):
- # type: (HandlerInput) -> Response
- return handler_input.response_builder.response
- class CatchAllExceptionHandler(AbstractExceptionHandler):
- def can_handle(self, handler_input, exception):
- # type: (HandlerInput, Exception) -> bool
- return True
- def handle(self, handler_input, exception):
- # type: (HandlerInput, Exception) -> Response
- logger.error(exception, exc_info=True)
- speech = "Sorry, there was some problem. Please try again!!"
- handler_input.response_builder.speak(speech).ask(speech)
- return handler_input.response_builder.response
- ### Custom Skill ####
- class TurnOffInstancesIntentHandler(AbstractRequestHandler):
- def can_handle(self, handler_input):
- # type: (HandlerInput) -> Response
- return is_intent_name("TurnOffInstancesIntent")(handler_input)
- import boto3
- region = 'eu-west-1'
- instances = ['i-09424b66024415486']
- ec2 = boto3.client('ec2', region_name=region)
- def lambda_handler(self, event, context):
- ec2.stop_instances(InstanceIds=instances)
- print('stopped your instances: ' + str(instances))
- speech_text = "Acknowledged. Turning off instances."
- handler_input.response_builder.speak(speech_text).set_card(
- SimpleCard("Hello World", speech_text)).set_should_end_session(
- True)
- return handler_input.response_builder.response
- sb.add_request_handler(LaunchRequestHandler())
- sb.add_request_handler(HelloWorldIntentHandler())
- sb.add_request_handler(HelpIntentHandler())
- sb.add_request_handler(CancelOrStopIntentHandler())
- sb.add_request_handler(FallbackIntentHandler())
- sb.add_request_handler(SessionEndedRequestHandler())
- sb.add_request_handler(TurnOffInstancesIntentHandler())
- sb.add_exception_handler(CatchAllExceptionHandler())
- lambda_handler = sb.lambda_handler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement