{"id":56944,"date":"2023-03-25T10:04:00","date_gmt":"2023-03-25T04:34:00","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56944"},"modified":"2024-01-02T17:41:30","modified_gmt":"2024-01-02T12:11:30","slug":"creating-alexa-skill-using-aws-lambda-as-backend","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-alexa-skill-using-aws-lambda-as-backend\/","title":{"rendered":"Creating Alexa skill using AWS lambda as backend"},"content":{"rendered":"<p><strong>Amazon Alexa<\/strong> is a virtual assistant developed by Amazon, available on a range of devices including the Amazon Echo, Echo Dot, and Echo Show. One of the key features of Alexa is the ability to use \u201cskills\u201d to extend its functionality and allow users to interact with it in new ways.<\/p>\n<p>Creating a skill for Alexa is relatively simple and can be done using the Alexa Skills Kit (ASK), a collection of tools and resources for building Alexa skills. To get started, you will need to:<\/p>\n<p><strong>Sign up for an Amazon developer account, if you don\u2019t have one already.<\/strong><\/p>\n<ul>\n<li>Visit the Alexa Skills Kit developer portal and create a new skill.<\/li>\n<li>Choose a \u201cskill type\u201d that best suits your skill\u2019s functionality.<\/li>\n<li>Define the \u201cinteraction model\u201d for your skill by creating a list of \u201cutterances\u201d (the things users can say to Alexa to invoke your skill) and \u201cintent\u201d (what the skill should do when it\u2019s invoked).<\/li>\n<li>Set up the \u201cbackend\u201d for your skill, which is the code that runs on Amazon\u2019s servers to handle requests and provide responses.<\/li>\n<li>Test your skill using the Alexa Skills Kit developer portal or an Alexa-enabled device.<\/li>\n<li>Submit your skill for certification by Amazon.<\/li>\n<\/ul>\n<p><strong>Dividing the whole process into 3 steps:<\/strong><\/p>\n<ul>\n<li>Developing the skill\u2019s functionality.<\/li>\n<li>Creating an interaction model.<\/li>\n<li>Setting up the backend.<\/li>\n<\/ul>\n<p><strong>Developing the skill\u2019s functionality:<\/strong><\/p>\n<p>This step involves determining what the skill should do, and how users will interact with it. For example, a skill might allow users to listen to a podcast, play a game, or control their smart home devices. It\u2019s important to choose a unique and useful skill that will appeal to your target audience.<\/p>\n<p><strong>Creating an interaction model:<\/strong><\/p>\n<p>An interaction model defines the ways in which users can interact with your skill, including what they can say to invoke it and what the skill should do in response. You will need to create a list of \u201cutterances\u201d (the things users can say to invoke your skill) and \u201cintents\u201d (what the skill should do when it\u2019s invoked). This step involves writing sample phrases that users might say and mapping them to the intents that are defined for your skill.<\/p>\n<p><strong>Setting up the backend:<\/strong><\/p>\n<p>This step involves creating the code that runs on Amazon\u2019s servers to handle requests and provide responses. The backend code can be written in a variety of languages such as python, node.js and Java. This code should handle the requests coming from Alexa and respond accordingly.<\/p>\n<p><strong>Practicle Part start from here:<\/strong><\/p>\n<p><strong>Prerequisites:<\/strong><\/p>\n<ul>\n<li>An Amazon developer account. This is required to create and configure Alexa\u2019s skills.<\/li>\n<li>An aws (Amazon web services) account to host your skill on aws lambda<\/li>\n<li>An ask-sdk requires Python 2 (&gt;= 2.7) or Python 3 (&gt;= 3.6).<\/li>\n<\/ul>\n<p><strong>To check the python version run the below command in terminal:<\/strong><\/p>\n<p>$python &#8211;version<\/p>\n<p><strong>Adding ask-sdk to your project:<\/strong><\/p>\n<p>You can install it globaly but it is better to use virtual environment. virtual environment is an isolated Python environment that helps manage project dependencies and package versions.<\/p>\n<p><strong>use the below command to create virtual environment:<\/strong><\/p>\n<p>$python3 -m venv name_of_virtual_env<\/p>\n<p>In my case i am venv using name <strong>alexa-skill-venv<\/strong><\/p>\n<p>$python3 -m venv alexa-skill-venv<\/p>\n<p>After creating virtual environment run the below command to <strong>activate your virtual environment<\/strong>:<\/p>\n<p>$source alexa-skill-venv\/bin\/activate<\/p>\n<p>above command will activate the alexa-skill-venv(your venv name may be different)<\/p>\n<p><strong>Next, Install ask-sdk in the virtualenv<\/strong><\/p>\n<p>$pip install ask-sdk<\/p>\n<p>The SDK will be installed in the alexa-skill-venv\/lib\/python3.8\/site-packages<\/p>\n<p>Your python version may be different so it could be python2.8 or python3.6 etc.<\/p>\n<p><strong>Developing the Hello world skill<\/strong><\/p>\n<p>create <strong>hello_world.py<\/strong> file in the alexa-skill folder<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1DEzRR7g3dUKpohMnh_37yg.png\" \/><\/p>\n<p><strong>Next, implementing the request handlers for hello_world skill<\/strong><br \/>\n<strong>Request Handlers: <\/strong>Request handlers in Alexa Skills Kit (ASK) are functions that process requests and generate responses for Alexa to fulfill user\u2019s request. There are several types of request handlers in ASK that are used to handle different types of requests, including:<\/p>\n<p><strong>LaunchRequest Handler:<\/strong> This handler is invoked when the user launches the skill. It is used to provide a welcome message or instruction to the user.<\/p>\n<p><strong>IntentRequest Handler:<\/strong> This handler is invoked when the user invokes an intent. An intent represents a specific action that the user wants to perform, such as asking for a weather update or play music etc. IntentRequest handlers are used to process the user\u2019s request and provide an appropriate response.<\/p>\n<p><strong>SessionEndedRequest Handler:<\/strong> This handler is invoked when the session ends. It is used to perform any cleanup or logging tasks that need to be done before the session is terminated.<\/p>\n<p>Your code is responsible for making sure that the right request handler is used to process incoming requests and for providing a response.<\/p>\n<ul>\n<li>Creating a skill builder object. The skill builder object helps in adding components responsible for handling input requests and generating custom responses for your skill.<\/li>\n<\/ul>\n<p>Type the following code in your hello_world.py file<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1MZqojFlo59LzwIQoYjQcIA.png\" \/><\/p>\n<p><strong>Each request handler is written as a class that implements two methods of the AbstractRequestHandler class:<\/strong><\/p>\n<ul>\n<li><strong>can_handle :<\/strong> The can_handle method checks if the incoming request matches the specified intent name.<\/li>\n<li><strong>handle :<\/strong> the handle method generates a response containing the &#8220;Hello, World!&#8221; message.<\/li>\n<\/ul>\n<p><strong>LaunchRequest handler<\/strong><br \/>\nA LaunchRequest handler is a request handler in Alexa Skills Kit (ASK) that is responsible for handling requests made by users when they launch the skill. When the user opens your skill without providing a specific intent, a LaunchRequest is triggered.<\/p>\n<p>In a LaunchRequest handler, you can provide a welcome message to the user or provide instructions on how to use the skill.<\/p>\n<p>add the following code into your hello_world.py file, after the previous code.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1Wx5OV4Dy-Qf4_fzcwmY-Ww.png\" \/><\/p>\n<p>The can_handle function returns\u00a0<strong class=\"th mr\"><em class=\"adi\">True<\/em><\/strong>\u00a0if the incoming request is a LaunchRequest.<\/p>\n<p><strong>HelloWorldIntent handler<\/strong><br \/>\nType the follwing code after the <strong>LaunchRequestHandler<\/strong> class.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1osu6uh6zeQ3kpk-YM91AFA.png\" \/><\/p>\n<p>In the avobe example, we define a custom request handler called HelloWorldIntentHandler that can handle requests for the \u201cHelloWorldIntent\u201d intent. The can_handle method checks if the incoming request matches the specified intent name, and the handle method generates a response containing the \u201cHello, World\u201d message.<\/p>\n<p><strong>HelpIntentHandler:<\/strong><\/p>\n<p>Type the following code after the previous handler code.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/11kL4MVOdk_snawctbEGB9A.png\" \/><\/p>\n<p>In Alexa Skills Kit (ASK) development, the HelpIntentHandler is a built-in handler function that is automatically invoked when the user says something like \u201chelp\u201d or \u201cwhat can I say?\u201d while using an Alexa skill. The purpose of the HelpIntentHandler is to provide the user with guidance on how to interact with the skill and what types of requests are supported.<\/p>\n<p><strong>CancelAndStopIntent handler:<\/strong><br \/>\nType the follwing code after the previous handler class.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1e43SDkJ3gcOhAXhqQkZtGg.png\" \/><\/p>\n<p>The <strong>CancelAndStopIntent<\/strong> handler is a built-in handler function that is automatically invoked when the user says something like \u201ccancel\u201d, \u201cstop\u201d. while using an Alexa skill. The purpose of the CancelAndStopIntent handler is to end the user\u2019s session with the skill gracefully.<\/p>\n<p><strong>SessionEndedRequest handler:<\/strong><\/p>\n<p>Type the following code after the previous handler class.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1br2M61T4IPw1vzCO-iI3vw.png\" \/><\/p>\n<p>The SessionEndedRequest handler is a built-in handler function that is automatically invoked when the user\u2019s session with the skill ends for any reason. The purpose of the SessionEndedRequest handler is to perform any necessary cleanup or logging operations and gracefully exit the skill.<\/p>\n<p><strong>Exception handlers:<\/strong><br \/>\nType the following code after the previous handler class.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-56943\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/03\/Screenshot-from-2023-03-27-11-45-13-300x169.png\" alt=\"\" width=\"422\" height=\"238\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/03\/Screenshot-from-2023-03-27-11-45-13-300x169.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/03\/Screenshot-from-2023-03-27-11-45-13-1024x576.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/03\/Screenshot-from-2023-03-27-11-45-13-768x432.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/03\/Screenshot-from-2023-03-27-11-45-13-624x351.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/03\/Screenshot-from-2023-03-27-11-45-13.png 1366w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/p>\n<p><strong>Exception handlers<\/strong> are used to handle errors that occur within the skill code. There are two main types of exception handlers: generic exception handlers and request-specific exception handlers. A generic exception handler is used to catch any unhandled exceptions within the skill code.<\/p>\n<p><strong>Lambda handler:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1FZyfkrrYnuEA1n6thcFvbg.png\" \/><\/p>\n<p>The Lambda handler is the entry point for the function. It\u2019s the function that AWS Lambda invokes when it runs the code.<\/p>\n<p><strong>Next, Make zip file of the folder in my case it is alexa-skill.zip<\/strong><\/p>\n<p><strong>Create a new skill by following these steps:<\/strong><br \/>\n&#8211; Log in to the Alexa Skills Kit Developer Console.<br \/>\n&#8211; Click the Create Skill button in the upper right.<br \/>\n&#8211; Enter \u201cskill_name_of_your_choice\u201d as your skill name and click Next.<br \/>\nFor the model, select Custom and click Create skill.<\/p>\n<p>Create a lambda function on aws.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1ntaEWxM4-pwKvuJJR_p0Kg.png\" \/><\/p>\n<ul>\n<li>After creating lambda go to Add triggers section, and select Alexa as a trigger point.<\/li>\n<li>Add the skill id of which you get after creating the Alexa skill go to the end point tab you will see the skill id copy it and paste into the lambda trigger configuration.<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1VA625Q4mOO2gqt3spDYCcw.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1G2WfDezRDZ7r4oqCrdAbNg.png\" \/><\/p>\n<ul>\n<li>Next, upload your zip file by going to tab Upload from you can see in the below image.<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1vh1T-aYi2z72zjzjCdTQyQ.png\" \/><\/p>\n<p>After successful upload. Go to the Alexa skill that you created earlier.<\/p>\n<p>Next, Go to the Invocation tab and set the Invocation name, as you can see in the below image.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/16I_IojmGHBqL74wqn7syMQ.png\" \/><\/p>\n<p id=\"d0cc\" class=\"pw-post-body-paragraph abm abn zq th b abo abp wo abq abr abs wr abt abu abv abw abx aby abz aca acb acc acd ace acf acg qd bx\" data-selectable-paragraph=\"\"><strong class=\"th mr\">Next<\/strong>, Go to the Interaction Modal tab, select Intents, and click add Intemt HelloWorldIntent<\/p>\n<p id=\"66fc\" class=\"pw-post-body-paragraph abm abn zq th b abo abp wo abq abr abs wr abt abu abv abw abx aby abz aca acb acc acd ace acf acg qd bx\" data-selectable-paragraph=\"\">see the below image for reference.<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1R494N3Zcf0Opg7bOpuo_vQ.png\" \/><\/p>\n<p data-selectable-paragraph=\"\">Next, add your custom intent in create a custom intent section.<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1Y-mdDOSQPQyK27L_t64tYQ.png\" \/><\/p>\n<p>Next, click on HelloWorldIntent to some utterances which the user needs to speak to call that intent.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1IYuIjPzLCz6nmncXMaHUhQ.png\" \/><\/p>\n<p>AMAZON.CancelIntent, AMAZON.HelpIntent, and AMAZON.StopIntent are built-in Alexa intents, you do not need to provide sample utterances for them.<\/p>\n<p>Next, Once you are done editing the interaction model, dont forget to save and build the model.<br \/>\nNext, configure the endpoint for the skill. To do this, follow these steps:<\/p>\n<p>copy the lambda function arn<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1T3UFSth-ofKGvdUk7Fhjuw.png\" \/><\/p>\n<p>paste it to the alexa skill endpoint section<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1mCour__PACTBCv8px9-YNw.png\" \/><\/p>\n<p>Save the endpoint , go to interaction tab click save and build model.<\/p>\n<p>All done.<\/p>\n<p>By going to test tab in Alexa developer console you can test your skill .<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1wptjyO-f0NkoO2u2x8MgcA.png\" \/><\/p>\n<p>You can also open the Alexa app on your phone or at https:\/\/alexa.amazon.com and see your skills listed under Your Skills.<\/p>\n<p>Reference link :https:\/\/developer.amazon.com\/en-US\/alexa\/alexa-developer-documentation-welcome<\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Amazon Alexa is a virtual assistant developed by Amazon, available on a range of devices including the Amazon Echo, Echo Dot, and Echo Show. One of the key features of Alexa is the ability to use \u201cskills\u201d to extend its functionality and allow users to interact with it in new ways. Creating a skill for [&hellip;]<\/p>\n","protected":false},"author":1565,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":98},"categories":[1174,1],"tags":[5142],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56944"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1565"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56944"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56944\/revisions"}],"predecessor-version":[{"id":59721,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56944\/revisions\/59721"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}