Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. this is link to ec2 stop https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
  2. this is like to the github https://github.com/pyraven/alexa-admin-guide/blob/master/lambda_function.py
  3.  
  4. and this is my code :)
  5.  
  6.  
  7. ====
  8. # -*- coding: utf-8 -*-
  9.  
  10. # This is a simple Hello World Alexa Skill, built using
  11. # the implementation of handler classes approach in skill builder.
  12. import logging
  13.  
  14. from ask_sdk_core.skill_builder import SkillBuilder
  15. from ask_sdk_core.dispatch_components import AbstractRequestHandler
  16. from ask_sdk_core.dispatch_components import AbstractExceptionHandler
  17. from ask_sdk_core.utils import is_request_type, is_intent_name
  18. from ask_sdk_core.handler_input import HandlerInput
  19.  
  20. from ask_sdk_model.ui import SimpleCard
  21. from ask_sdk_model import Response
  22.  
  23. sb = SkillBuilder()
  24.  
  25. logger = logging.getLogger(__name__)
  26. logger.setLevel(logging.INFO)
  27.  
  28.  
  29. class LaunchRequestHandler(AbstractRequestHandler):
  30. def can_handle(self, handler_input):
  31. # type: (HandlerInput) -> bool
  32. return is_request_type("LaunchRequest")(handler_input)
  33.  
  34. def handle(self, handler_input):
  35. # type: (HandlerInput) -> Response
  36. speech_text = "Welcome to the Alexa Skills Kit, you can say hello!"
  37.  
  38. handler_input.response_builder.speak(speech_text).set_card(
  39. SimpleCard("Hello World", speech_text)).set_should_end_session(
  40. False)
  41. return handler_input.response_builder.response
  42.  
  43.  
  44. class HelloWorldIntentHandler(AbstractRequestHandler):
  45. def can_handle(self, handler_input):
  46. # type: (HandlerInput) -> bool
  47. return is_intent_name("HelloWorldIntent")(handler_input)
  48.  
  49. def handle(self, handler_input):
  50. # type: (HandlerInput) -> Response
  51. speech_text = "Hello Python Gurus from Classes!"
  52.  
  53. handler_input.response_builder.speak(speech_text).set_card(
  54. SimpleCard("Hello World", speech_text)).set_should_end_session(
  55. True)
  56. return handler_input.response_builder.response
  57.  
  58.  
  59. class HelpIntentHandler(AbstractRequestHandler):
  60. def can_handle(self, handler_input):
  61. # type: (HandlerInput) -> bool
  62. return is_intent_name("AMAZON.HelpIntent")(handler_input)
  63.  
  64. def handle(self, handler_input):
  65. # type: (HandlerInput) -> Response
  66. speech_text = "You can say hello to me!"
  67.  
  68. handler_input.response_builder.speak(speech_text).ask(
  69. speech_text).set_card(SimpleCard(
  70. "Hello World", speech_text))
  71. return handler_input.response_builder.response
  72.  
  73.  
  74. class CancelOrStopIntentHandler(AbstractRequestHandler):
  75. def can_handle(self, handler_input):
  76. # type: (HandlerInput) -> bool
  77. return (is_intent_name("AMAZON.CancelIntent")(handler_input) or
  78. is_intent_name("AMAZON.StopIntent")(handler_input))
  79.  
  80. def handle(self, handler_input):
  81. # type: (HandlerInput) -> Response
  82. speech_text = "Goodbye!"
  83.  
  84. handler_input.response_builder.speak(speech_text).set_card(
  85. SimpleCard("Hello World", speech_text))
  86. return handler_input.response_builder.response
  87.  
  88.  
  89. class FallbackIntentHandler(AbstractRequestHandler):
  90. def can_handle(self, handler_input):
  91. # type: (HandlerInput) -> bool
  92. return is_intent_name("AMAZON.FallbackIntent")(handler_input)
  93.  
  94. def handle(self, handler_input):
  95. # type: (HandlerInput) -> Response
  96. speech_text = (
  97. "The Hello World skill can't help you with that. "
  98. "You can say hello!!")
  99. reprompt = "You can say hello!!"
  100. handler_input.response_builder.speak(speech_text).ask(reprompt)
  101. return handler_input.response_builder.response
  102.  
  103.  
  104. class SessionEndedRequestHandler(AbstractRequestHandler):
  105. def can_handle(self, handler_input):
  106. # type: (HandlerInput) -> bool
  107. return is_request_type("SessionEndedRequest")(handler_input)
  108.  
  109. def handle(self, handler_input):
  110. # type: (HandlerInput) -> Response
  111. return handler_input.response_builder.response
  112.  
  113.  
  114. class CatchAllExceptionHandler(AbstractExceptionHandler):
  115. def can_handle(self, handler_input, exception):
  116. # type: (HandlerInput, Exception) -> bool
  117. return True
  118.  
  119. def handle(self, handler_input, exception):
  120. # type: (HandlerInput, Exception) -> Response
  121. logger.error(exception, exc_info=True)
  122.  
  123. speech = "Sorry, there was some problem. Please try again!!"
  124. handler_input.response_builder.speak(speech).ask(speech)
  125.  
  126. return handler_input.response_builder.response
  127.  
  128.  
  129. ### Custom Skill ####
  130.  
  131.  
  132.  
  133.  
  134. class TurnOffInstancesIntentHandler(AbstractRequestHandler):
  135. def can_handle(self, handler_input):
  136. # type: (HandlerInput) -> Response
  137. return is_intent_name("TurnOffInstancesIntent")(handler_input)
  138.  
  139. import boto3
  140. region = 'eu-west-1'
  141. instances = ['i-09424b66024415486']
  142. ec2 = boto3.client('ec2', region_name=region)
  143.  
  144. def lambda_handler(self, event, context):
  145. ec2.stop_instances(InstanceIds=instances)
  146. print('stopped your instances: ' + str(instances))
  147. speech_text = "Acknowledged. Turning off instances."
  148. handler_input.response_builder.speak(speech_text).set_card(
  149. SimpleCard("Hello World", speech_text)).set_should_end_session(
  150. True)
  151. return handler_input.response_builder.response
  152.  
  153. sb.add_request_handler(LaunchRequestHandler())
  154. sb.add_request_handler(HelloWorldIntentHandler())
  155. sb.add_request_handler(HelpIntentHandler())
  156. sb.add_request_handler(CancelOrStopIntentHandler())
  157. sb.add_request_handler(FallbackIntentHandler())
  158. sb.add_request_handler(SessionEndedRequestHandler())
  159. sb.add_request_handler(TurnOffInstancesIntentHandler())
  160.  
  161. sb.add_exception_handler(CatchAllExceptionHandler())
  162.  
  163. lambda_handler = sb.lambda_handler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement