def create_skill(): skill_adapter = SkillAdapter( skill=sb.create(), skill_id="amzn1.ask.skill.bd7515ac-93e7-48c6-b2b5-58dcd0fb0951", app=app) # Déclaration de la route skill_adapter.register(app=app, route="/alexa")
def test_init_app_sets_default_config_if_not_present(self): self.test_app.config[VERIFY_SIGNATURE_APP_CONFIG] = False test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id) test_skill_adapter.init_app(self.test_app) self.check_config_set(self.test_app, signature_config=False)
def test_init_app_sets_multiple_ext_with_skill_ids(self): self.test_app.extensions[EXTENSION_NAME] = {} self.test_app.extensions[EXTENSION_NAME]["test"] = "value" test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id) test_skill_adapter.init_app(self.test_app) self.check_extension_mapping_set(self.test_app, "test", "value")
def create_app(): print("************** create_app") app = Flask(__name__) skill_adapter = SkillAdapter( skill=sb.create(), skill_id="amzn1.ask.skill.bd7515ac-93e7-48c6-b2b5-58dcd0fb0951", app=app) skill_adapter.register(app=app, route="/alexa") return app
def test_register_url_rule_no_app_throw_exception(self): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id) with self.assertRaises(TypeError) as exc: test_skill_adapter.register(app=None, route="/") self.assertIn( "Expected a valid Flask instance", str(exc.exception), "register method didn't throw exception when no app was passed")
def test_register_url_rule_no_route_throw_exception(self): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id) with self.assertRaises(TypeError) as exc: test_skill_adapter.register(app=self.test_app, route=None) self.assertIn( "Expected a valid URL rule string", str(exc.exception), "register method didn't throw exception when no route was passed")
def test_register_url_rule_with_endpoint(self): mock_app = mock.MagicMock(Flask) test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id) test_skill_adapter.register(app=mock_app, route="/", endpoint="test") mock_app.add_url_rule.assert_called_with( "/", endpoint="test", methods=['POST'], view_func=test_skill_adapter.dispatch_request)
def test_init_app_sets_app_configurations(self): self.check_config_not_set(self.test_app) self.check_extension_mapping_not_set(self.test_app) test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id) test_skill_adapter.init_app(self.test_app) self.check_config_set(self.test_app) self.check_extension_mapping_set(self.test_app)
def test_request_dispatch_failure_throw_exception(self): self.test_app.config[VERIFY_SIGNATURE_APP_CONFIG] = False self.test_app.config[VERIFY_TIMESTAMP_APP_CONFIG] = False self.test_app.logger.setLevel(logging.CRITICAL) mocked_handler = mock.MagicMock(spec=WebserviceSkillHandler) mocked_handler.verify_request_and_dispatch.side_effect = \ AskSdkException("test") with mock.patch( "flask_ask_sdk.skill_adapter.WebserviceSkillHandler", return_value=mocked_handler): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.test_app.add_url_rule( "/", "index", test_skill_adapter.dispatch_request, methods=["POST"]) self.test_app.testing = True with self.test_app.test_client() as c: test_response = c.post( "/", data={}, content_type="application/json") self.assertEqual( test_response.status_code, 500, "SkillAdapter didn't raise internal error when " "skill invocation failed")
def test_request_dispatch(self): self.test_app.config[VERIFY_SIGNATURE_APP_CONFIG] = False self.test_app.config[VERIFY_TIMESTAMP_APP_CONFIG] = False mocked_handler = mock.MagicMock(spec=WebserviceSkillHandler) test_response_env = ResponseEnvelope().to_str() with self.test_app.app_context(): expected_response = jsonify(test_response_env) mocked_handler.verify_request_and_dispatch.return_value = \ test_response_env with mock.patch( "flask_ask_sdk.skill_adapter.WebserviceSkillHandler", return_value=mocked_handler): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.test_app.add_url_rule( "/", "index", test_skill_adapter.dispatch_request, methods=["POST"]) self.test_app.testing = True with self.test_app.test_client() as c: actual_response = c.post( "/", data={}, content_type="application/json") self.assertEqual( actual_response.status_code, 200, "SkillAdapter didn't return valid status code " "during successful skill dispatch") self.assertEqual( actual_response.data, expected_response.data, "SkillAdapter didn't return correct response on " "successful skill dispatch")
def test_add_custom_user_agent_to_valid_skill_initialization(self): SkillAdapter(skill=self.mock_skill, skill_id=None) self.assertIn( "flask-ask-sdk", self.mock_skill.custom_user_agent, "SkillAdapter didn't update custom user agent " "for a valid custom skill")
def test_init_sets_config_when_app_provided(self): self.check_config_not_set(self.test_app) self.check_extension_mapping_not_set(self.test_app) SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.check_config_set(self.test_app) self.check_extension_mapping_set(self.test_app)
def test_init_app_creates_user_agent_if_not_set(self): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.assertIn("ask-webservice flask-ask-sdk", self.mock_skill.custom_user_agent, "SkillAdapter didn't update custom user agent " "for a valid custom skill without any user agent set")
def test_invalid_skill_instance_throw_exception(self): with self.assertRaises(TypeError) as exc: SkillAdapter(skill=None, skill_id=None) self.assertIn( "Invalid skill instance provided", str(exc.exception), "SkillAdapter constructor didn't throw exception for " "invalid skill input")
def test_init_app_appends_user_agent_if_already_set(self): self.mock_skill.custom_user_agent = "test-agent" test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.assertEqual("test-agent ask-webservice flask-ask-sdk", self.mock_skill.custom_user_agent, "SkillAdapter didn't update custom user agent " "for a valid custom skill with a user agent set")
def test_request_dispatch_http_get_throw_method_not_allowed(self): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.test_app.add_url_rule( "/", "index", test_skill_adapter.dispatch_request) self.test_app.testing = True with self.test_app.test_client() as c: test_response = c.get("/") self.assertEqual( test_response.status_code, 405, "SkillAdapter didn't raise method not allowed exception " "when a GET request hits the registered route")
sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(LoginIntentHandler()) sb.add_request_handler(CreateMedicineReminderHandler()) sb.add_request_handler(GetMedDataIntentHandler()) sb.add_request_handler(GetSideEffectsIntentHandler()) sb.add_request_handler(GetNextDoseIntentHandler()) sb.add_request_handler(GetRemainingStockIntentHandler()) sb.add_request_handler(ReorderMedicinesIntentHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) # make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers sb.add_request_handler(IntentReflectorHandler()) sb.add_global_request_interceptor(LocalizationInterceptor()) sb.add_exception_handler(CatchAllExceptionHandler()) skill_response = SkillAdapter( skill=sb.create(), skill_id="amzn1.ask.skill.cd33a56e-3c27-409e-aa97-8e24a2f0d8da", app=app) skill_response.register(app=app, route="/") if __name__ == "__main__": app.run(debug=True)
""" # type: (HandlerInput, Exception) -> None print("Encountered following exception: {}".format(exception)) #to do: changer le texte speech = "il y a un problème" handler_input.response_builder.speak(speech).ask(speech) return handler_input.response_builder.response # Handler to be provided in lambda console. lambda_handler = sb.lambda_handler() #deploy on Kermit app = Flask(__name__) skill_response = SkillAdapter( skill=sb.create(), skill_id = config.skill_id, app=app) @app.route("/",methods=['GET','POST']) def invoke_skill(): return skill_response.dispatch_request() #API base de données #GET collection @app.route("/databases/<db_name>/collections/<db_collection>", methods= ['GET']) def get_collection_api(db_name,db_collection): # API utilisant la connection créée dès le début du programme collection = functions.get_collection(client_bdd, db_name, db_collection) documents = collection.find() response = [] for document in documents: del document['_id']
.ask(speak_output) .response ) # The SkillBuilder object acts as the entry point for your skill, routing all request and response # payloads to the handlers above. Make sure any new handlers or interceptors you've # defined are included below. The order matters - they're processed top to bottom. sb = SkillBuilder() # Register all handlers, interceptors etc. sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(MoveNorthIntentHandler()) #sb.add_request_handler(HelloWorldIntentHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_request_handler(IntentReflectorHandler()) # make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers sb.add_exception_handler(CatchAllExceptionHandler()) app = Flask(__name__) app.config['ASK_SDK_VERIFY_TIMESTAMP'] = False skill_response = SkillAdapter( skill=sb.create(), skill_id='amzn1.ask.skill.327b488d-bf14-476a-8456-d092972cd5b5', app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': app.run()
sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(StatusIntentHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_request_handler(MessageHandler()) sb.add_request_handler( IntentReflectorHandler() ) # make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers sb.add_exception_handler(CatchAllExceptionHandler()) handler = sb.lambda_handler() skill_response = SkillAdapter( skill=sb.create(), skill_id="amzn1.ask.skill.153a3e93-50d5-4821-b7f8-246b4dd0906b", app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': read_wm_status() # app.run(host= '0.0.0.0',ssl_context=('cert.pem', 'privkey.pem')) app.run( host='0.0.0.0', ssl_context= ('/home/pi/devel/wash-bot-alexa-skill/self-signed_cert/certificate.pem', '/home/pi/devel/wash-bot-alexa-skill/self-signed_cert/private-key.pem' )) # app.run()
else: speech_text = 'Passenger {} is not on the manifest.'.format(hostname) return handler_input.response_builder.speak(speech_text).response @skill_builder.request_handler(can_handle_func=is_request_type('SessionEndedRequest')) def session_ended_request_handler(handler_input): return handler_input.response_builder.response @skill_builder.global_request_interceptor() def request_logger(handler_input): app.logger.debug('Request received: {}'.format(handler_input.request_envelope.request)) @skill_builder.global_response_interceptor() def response_logger(handler_input, response): app.logger.debug('Response generated: {}'.format(response)) skill_response = SkillAdapter(skill=skill_builder.create(), skill_id=config['applicationId'], app=app) skill_response.register(app=app, route='/') if __name__ == '__main__': app.run(host = '0.0.0.0', debug = True)
from flask import Flask from flask_ask_sdk.skill_adapter import SkillAdapter from ask_sdk_core.skill_builder import SkillBuilder import sys sys.path.append('../custom/') from constants.skill_id import skill_id from number import sb app = Flask(__name__) skill_response = SkillAdapter(skill=sb.create(), skill_id=skill_id, app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': app.run(debug=True, threaded=True)
# Add all request handlers to the skill. sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(GetConnectedDevices()) sb.add_request_handler(GetMeasures()) sb.add_request_handler(GetThresholds()) sb.add_request_handler(TimeToThreshold()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(ExitIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_request_handler(FallbackIntentHandler()) # Add exception handler to the skill. sb.add_exception_handler(CatchAllExceptionHandler()) # Add response interceptor to the skill. sb.add_global_request_interceptor(RequestLogger()) sb.add_global_response_interceptor(ResponseLogger()) app = Flask(__name__) skill_response = SkillAdapter(skill=sb.create(), skill_id=data.SKILL_ID, app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': app.run()
return handler_input.response_builder.response @sb.exception_handler(can_handle_func=lambda i, e: True) def all_exception_handler(handler_input, exception): # type: (HandlerInput, Exception) -> Response # Log the exception in CloudWatch Logs print(exception) speech = "Sorry, I didn't get it. Can you please say it again?" handler_input.response_builder.speak(speech).ask(speech) return handler_input.response_builder.response app = Flask(__name__) skill_response = SkillAdapter( skill=sb.create(), skill_id="AMAZON SKILL ID GOES HERE", app=app) skill_response.register(app=app, route="/") @app.route("/") def invoke_skill(): return skill_response.dispatch_request() if __name__ == '__main__': app.run()
""" # type: (HandlerInput, Exception) -> Response logger.error(exception, exc_info=True) return handler_input.response_builder.speak(ERROR_PROMPT).set_should_end_session(True).response # --- aws lambda lambda_handler = sb.lambda_handler() # --- flask from flask import Flask from flask_ask_sdk.skill_adapter import SkillAdapter app = Flask(__name__) skill_adapter = SkillAdapter(skill=sb.create(), skill_id='TEST', app=app) @app.route("/", methods=['POST']) def invoke_skill(): """ POST Methode, die den Skill als FLASK app startet. :return: Startet den Skill :rtype: dispatch_request """ return skill_adapter.dispatch_request() if __name__ == '__main__': app.run(debug=True)
handler_input.response_builder.speak(speech).ask(speech) 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 # any cleanup logic goes here return handler_input.response_builder.response sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(HelloWorldIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_exception_handler(AllExceptionHandler()) app = Flask(__name__) skill_response = SkillAdapter( skill=sb.create(), skill_id='amzn1.ask.skill.41125c2b-0229-4ff4-8ea4-5290ad3830ea', app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': app.run()
@sb.exception_handler(can_handle_func=lambda i, e: True) def all_exception_handler(handler_input, exception): """Catch all exception handler, log exception and respond with custom message. """ # 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 #handler = sb.lambda_handler() skill_adapter = SkillAdapter( skill=sb.create(), skill_id="amzn1.ask.skill.6fbbb190-9c92-4a5f-bfb6-fd9ec2d8c860", app=app) skill_adapter.register(app=app, route="/") # @app.route("/") # def invoke_skill(): # print("invokte_skill") # return skill_adapter.dispatch_request() if __name__ == '__main__': app.run(debug=True, port=8050)
from intents.catch_all_exception_handler import CatchAllExceptionHandler from intents.first_order_intent_handler import FirstOrderIntentHandler from intents.second_order_intent_handler import SecondOrderIntentHandler from intents.third_order_intent_handler import ThirdOrderIntentHandler load_dotenv() sb = SkillBuilder() sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(FirstOrderIntentHandler()) sb.add_request_handler(SecondOrderIntentHandler()) sb.add_request_handler(ThirdOrderIntentHandler()) sb.add_exception_handler(CatchAllExceptionHandler()) app = Flask(__name__) skill_response = SkillAdapter(skill=sb.create(), skill_id=os.getenv('SKILL_ID'), app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': app.run(threaded=True)
# defined are included below. The order matters - they're processed top to bottom. @app.route('/ddog/webhook') def alert(payload): return payload sb = SkillBuilder() sb.add_request_handler(LaunchRequestHandler()) sb.add_request_handler(GetMetricsIntentHandler()) sb.add_request_handler(HelpIntentHandler()) sb.add_request_handler(CancelOrStopIntentHandler()) sb.add_request_handler(SessionEndedRequestHandler()) # make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers sb.add_request_handler(IntentReflectorHandler()) sb.add_global_request_interceptor(LocalizationInterceptor()) sb.add_exception_handler(CatchAllExceptionHandler()) skill_response = SkillAdapter( skill=sb.create(), skill_id="amzn1.ask.skill.1fd6a485-84ff-406c-aba1-75007ce3df11", app=app) skill_response.register(app=app, route="/") if __name__ == '__main__': app.run()
@skill_builder.exception_handler(can_handle_func=lambda i, e: True) def all_exception_handler(handler_input, exception): # """Catch all exception handler, log exception and # respond with custom message. # """ # 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 # skill_id="amzn1.ask.skill.e909e9d9-3ad7-4563-bbfe-ec0247ab3f7a" skill_adapter = SkillAdapter(skill=skill_builder.create(), skill_id=10, app=app) skill_adapter.register(app=app, route="/moveplan") @app.route('/heartbeat') def default_route(): """Default route to return a simple message""" return jsonify('hi! I am alive') if __name__ == "__main__": app.run(host='0.0.0.0', debug=True)