Rahul Sinha
Rahul Sinha Blog

Rahul Sinha Blog

Create Alexa Skills in Python | Alexa skills in Python-hosted

Create Alexa Skills in Python | Alexa skills in Python-hosted

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Hello Guys, Here we will know how to create an Alexa skill in python. We will create a simple Space-fact skill in python.

image.png

Firstly, we should have an Amazon Developer account. If you don’t have a developer account, don’t worry simply go to Create your new account. After clicking on sign in you will be navigated to the sign-in page. Write all your necessary details, if you don’t have a developer account below down you will see a button → Create your Amazon Developer Account. Now simply create your account. I will not teach you that. ;)

image.png

After doing all this stuff you will be navigated to → the developer dashboard. There you will see Alexa written in one of the menus. Click on Alexa and you will see a drop-down menu then Click on Alexa Skills kit as shown in the picture below.

image.png

After clicking on the Alexa skills kit, you will be navigated to the Alexa developer console. There you will see a blue button written on it Create Skill. Click on that button to start making your skills.

image.png

Now let’s start making our first Alexa skills in python. We name it as Space Fact. You can give of your choice. Below that choose your country. I choose India because your bro is from India. Now Don’t change anything just go down and choose →Alexa-Hosted(Python) and go above click again on that blue button →Create Skill. It will take a minute to load and then you will be navigated to the frontend where you will design your Alexa.

image.png

This is the page where you will do all the necessary kinds of stuff. You will create intent, utterances, and slots. In this skill, we will not use a slot. So I will suggest you to give your only 5 mins to know some theory of creating Alexa skills. After reading, come again and now without wasting time → Let’s start….

image.png

Now you will see in your left written Intent, below intent you will see HelloWorldIntent → DELETE AS FAST AS POSSIBLE. We don’t require that. It is a build-in skill. Now click on Add and you will see Create Custom Intent, write there → GetFactIntent exactly similar to these. Don’t use spaces in between. Then click on create custom intent.

Now the task comes, write some utterances → are some basic English sentences that will invoke the intent. Here is some example you also add your own as many as you can.

tell me some space facts

give me a space fact

tell me a space fact

more space fact

image.png

After creating Intent and giving your utterances simply save the model and then click on → Build Model. It will take some time to build and you will get a message Full Build Successful. Go above, Click on Code beside the Build. There we do some coding stuff.

image.png

Now come to the coding part, you will see from written, above from write → import random. Now below import write this line code→ coolfacts=[ ‘SPACE IS COMPLETELY SILENT.’, ‘THE HOTTEST PLANET IN OUR SOLAR SYSTEM IS 450° C.’, ‘THERE MAY BE LIFE ON MARS.’, ‘NOBODY KNOWS HOW MANY STARS ARE IN SPACE.’, ‘HALLEYs COMET WONT ORBIT PAST EARTH AGAIN UNTIL 2061.’, ‘A FULL NASA SPACE SUIT COSTS $12,000,000.’]

Use Comma(,) after completion of quote(’ ).

You can add n number of facts in this manner. What we have done let me explain to you. We have taken a variable coolfacts and written all the facts in a List. We have imported random function so that Alexa can pick random facts. Save it but don’t Deploy, we have completed half right now more is left.

image.png

Now slowly come down you will see this line code…

speak_output = “Welcome, you can say Hello or Help. Which would you like to try?”

Change the line of code with this line of code…or you can write of your own choice also. It is an introductory line when the user will Alexa skill.

speak_output = “Welcome to Space Fact. You can ask that tell me a space fact”

Below this, you will see a class name HelloWorldIntent which we have deleted earlier.

class HelloWorldIntentHandler(AbstractRequestHandler):
“””Handler for Hello World Intent.”””
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name(“HelloWorldIntent”)(handler_input)

def handle(self, handler_input):
# type: (HandlerInput) -> Response
speak_output = “Hello World!”

return (
handler_input.response_builder
.speak(speak_output)
# .ask(“add a reprompt if you want to keep the session open for the user to respond”)
.response
)

Instead of this line of code paste this line of code…

class GetFactIntentHandler(AbstractRequestHandler):
“””Handler for Hello World Intent.”””
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name(“GetFactIntent”)(handler_input)

def handle(self, handler_input):
# type: (HandlerInput) -> Response
speak_output = random.choice(coolfacts)

return (
handler_input.response_builder
.speak(speak_output)
# .ask(“add a reprompt if you want to keep the session open for the user to respond”)
.response
)

Here we have changed the name of HelloWorldIntent → GetFactIntent which was the intent name. We also add a small code → speak_output = random.choice(coolfacts).

Now you can do the same for HelpIntentHandler. There you just have to change the speak_output text. You can use this code and paste it there…

speak_output = “Hello, How I can help you? You can ask that tell me a space fact”

image.png

Now totally come down, there you will see this line code change it with code which has provided below. It is the mainline by which our intent is called.

sb.add_request_handler(HelloWorldIntentHandler())

change this with this…

sb.add_request_handler(GetFactIntentHandler())

Now Save it and Deploy it. After successful deployment, go to Test to check your Alexa skill.

image.png

Now it's time to test your Alexa skill. Change it Off to Development.

Now type → open space fact {in lower case}. Here Alexa will reply and then use your utterances which you defined earlier → tell me a space fact.

image.png

Hurray!! You successfully made your first Alexa skills. Now you are an Alexa Developer. Go and rock the world….

You can also publish your skills by going to the Distribution tab and filling in some necessary details.

 
Share this