In this tutorial, you and your child will learn how to make your own Alexa-like chatbot with AI in Scratch! You've likely heard of and maybe even have used a device powered by Alexa. Alexa is Amazon's AI assistant that can be found on Amazon Echo devices, TVs, cell phones, and many other types of devices. Alexa responds to voice commands to perform tasks or respond to questions, like turning on your lights or telling you the weather for today. The more you use Alexa, the more it learns about you and your interests and preferences.

As you create this chatbot, you'll design responses for when key words are said. It can be quite complex to program, but the satisfaction of designing your very own chatbot is worth it!

To learn how to create apps and games with Scratch, enroll your child in an award-winning live online Scratch Ninja coding class, designed by experts from Google, Stanford, and MIT. Start with the free Scratch course:

How To Make An Alexa-Like Chatbot With AI In Scratch

Let's get started creating your chatbot. Here's the project we'll be making.

Step 1: Start with your sprite and backdrop.

Go to the Scratch website and start a new project. First, choose a character for your chatbot and a backdrop. By default, Scratch gives you the cat sprite and a blank backdrop. You can use the default cat sprite, or you can choose your own. Find the cat icon in the bottom right corner of the screen to choose your own.

You can choose a sprite from the library, use the Paint option to draw your own, use the Surprise option to let Scratch randomly select one for you, or use the Upload option to upload your own image.

Choose a Sprite
Upload a Sprite

If you decide to choose your own, you can delete the default cat sprite by selecting it and then clicking its trash can icon.

To choose a backdrop, find the mountain icon.

Just like in the sprite menu, you have options to choose a backdrop from the library, use the Paint option to draw your own, use the Surprise option to let Scratch randomly select one for you, or use the Upload option to upload your own image.

Pick a backdrop in Scratch

Step 2: Tell the user to say something or ask the chatbot a question.

Let's start your code! You first need to tell the user to say something or ask a question so your chatbot can respond. You also will want your program to continue the conversation as long as the program is running.

Make sure your sprite is selected in the Sprite window. It should be highlighted in blue to indicate it is selected.

Select your Sprite

On the left side of the screen, you will see categories of blocks that you can use to write your code. When you choose a category, you will see its available blocks.

First, go to the Events category and pull out the when flag clicked block.

You want the conversation to continue as long as the program is running. Go to the Control category and pull out the forever block. Add the forever block to the when flag clicked block.

Inside the forever block, let's tell the user to say something or ask a question. Go to the Sensing category and pull out the ask block. Add this inside the forever block. In the text box, write something like "Say something or ask me a question."

Say something or ask me a question

Step 3: Make variables to keep track of whether the user's answer was found in the chatbot's list and the location of where this answer was found in the list.

Just like Alexa, your AI chatbot needs to learn and remember what the user has said and what it should say in response.

To help your AI chatbot do this, go to the Variables category and create one variable called found to store whether or not the answer was found in the chatbot's list and another variable called prompt location to store where the answer was found.

Step 4: Create lists to store what the chatbot has learned and how it should respond.

Go to the Variables category and create a list called prompts and another list called responses. The prompts list will store what the user has said to the chatbot, and the responses list will store how the chatbot should respond to these statements or questions.

Step 5: Set the initial values for the variables.

Every time the user says something or asks a question, the values for these variables need to be reset.

Go to the Variables category and pull out two set blocks. Add these after the ask block. Set the found variable to false and the prompt location variable to 1.

Set variable values

Step 6: Check the list of prompts for the user's answer.

The chatbot needs to see if it already knows how to respond to what the user said. It first has to search the prompts list. Go to the Control category and pull out a repeat block. Add this after the set blocks.

The repeat block takes a condition for how many times it should repeat. It should repeat for each item in the prompts list. Go to the Variables category and pull out the length of block. Choose prompts from its dropdown.

Check user's answer

Let's check if the user's answer is part of one of the prompts the AI chatbot already knows. Let's say the AI chatbot knows that when the user says "Hello", it should respond with "Hello!" This should also happen if the user says "Hello!" It should recognize that "Hello" and "Hello!" are the same prompts from the user.

Go to the Control category and pull out an if-else block. Add this inside the repeat block.

Then go to the Operators category and pull out the contains block. Add this to the condition for the if block.

From the Sensing category, pull out the answer block. Add this to the first space in the contains block.

From the Variables category, pull out the item ___ of ___ block. Choose the prompts list from its dropdown. Also from the Variables category, pull out the answer block, and add it to the empty space in the contains block.

Check prompts list

This will check if the user's answer is part of any of the items in the prompts list.

Step 7: Teach the AI chatbot the new version of the prompt.

The user's answer may contain words the AI chatbot already knows but with different punctuation.

To make sure the AI chatbot knows what to say when the entire prompt is used, it needs to check if the entire prompt is in its prompts list. For example, it may have "Hello" in its prompts list but not have "Hello!" in the list. Let's check whether or not the entire prompt is in the list, and add the prompt if it is not in the list.

From the Control category, pull out an if block. Add this inside the if portion of the if-else block.

From the Operators category, pull out the not block and the contains block. Add the not block inside the condition for the if statement, then add the contains block inside the not block.

From the Variables category, pull out the prompts block. Add this to the first empty space in the contains block.

From the Sensing category, pull out the answer block. Add this to the other empty space in the contains block.

Contains block in Scratch

If the prompt does not contain the entire exact prompt, it needs to add it to its prompts list and the response it knows at the same location in its responses list.

From the Variables category, pull out two add ___ to ___ blocks. Add these inside the if statement.

For the first add block, pull out the answer block from the Sensing category. Add this to the empty space of the add block. Choose the prompts list from the dropdown.

For the second add block, pull out the item ___ of ___ block from the Variables category. Add this to the empty space of the add block. Choose the responses list for both drop downs.

Code for AI chatbot

Step 8: Say the response!

Since the chatbot already knows what to say to this prompt, let's say it!

First, it needs to indicate that it found the user's answer in the prompts list. Go to the Variables category and pull out the set block. Add this after the second if statement but still inside the first if statement. Use this to set the found variable to true by choosing found from its dropdown and entering true in the space.

To say the response, pull out the say block from the Looks category. Add this after the set found to true block.

To say the right response to the prompt, go to the Variables category and pull out the item ___ of ___ block. Add this inside the say block. Also from the Variables category, pull out the prompt location block, and add this to the space in the item ___ of ___ block. Choose the responses list from its dropdown.

Say the response

Step 9: Update the prompt location for the next item in the list.

If the user's answer does not match the current item in the list, the prompt location needs to move to the next item in the list.

From the Variables category, pull out the change ___ by 1 block. Add this inside the else statement. Choose prompt location from its dropdown.

Choose prompt location

Step 10: Teach the AI.

An important aspect of AI is the ability to learn. If the user's answer was not found in the prompts list after checking every item, the AI needs to know what to say the next time the user uses this prompt.

From the Control category, pull out the if block. Add this after the repeat block.

To check if the found variable is still false because the prompt was not found, pull out the = block from the Operators category. Add this in the condition for the if statement.

Go to the Variables category and pull out the found block. Add this to the first space of the = block. Enter false in the second space.

Scratch coding for chatbot

To teach the AI the new prompt, pull out the add ___ to ___ block from the Variables category. Add this inside the if block.

Go to the Sensing category, and pull out the answer block. Add this to the space in the add block. Choose the prompts list from its dropdown.

Now, let's tell the user that the chatbot doesn't know what to say and ask them what it should say in the future.

Go to the Sensing category, and pull out the ask block. Add this after the add block. In the text box, write something like "Sorry, I don't know how to respond to that yet. What should I say next time?"

Finally, add the answer to the responses list. Go to the Variables category, and pull out another add ___ to ___ block.

Go to the Sensing category, and pull out the answer block. Add this to the space in the add block. Choose the responses list from its dropdown.

Show responses in chatbot through coding

Customize your chatbot with coding!

Congratulations! You have created an Alexa-like AI chatbot that can learn prompts from the user and how it should respond.

You can customize your chatbot to do more and become smarter. Try:

  • Adding prompts and responses to the lists when the program starts so the chatbot already knows some.
  • Change the backdrop when the chatbot doesn't know how to respond.
  • Or any other idea you can think of!

Code Your Alexa-Like Chatbot With AI In Scratch

Learn how to make your own Pong game with Scratch.

Enroll your child is Create & Learn's live online beginner-friendly Scratch coding classes designed by experts from Google, Stanford, and MIT to build their problem solving skills as they design fun games and animations. Students even earn a certificate upon class completion! Enjoy a free introduction session.

Review of Key Coding Concepts Used Today

1. Variables

Variables store data that can change throughout your program. In this project, variables like Found and Prompt Location are used to track whether the chatbot has found the user’s input and the location of this input in a list. Variables are essential because they allow your chatbot to remember and update information dynamically.

2. Lists

Lists store multiple pieces of data in a specific order. In this chatbot, two lists (prompts and responses) are used to store what the user says and the chatbot’s corresponding replies. Lists are crucial in organizing and managing multiple inputs, enabling the chatbot to retrieve the right response based on what the user says.

3. Control Structures

Control structures such as If-Else blocks and Repeat loops guide the flow of your program. They are used here to check if the user’s input matches any known prompts, and to teach the AI chatbot new responses if the input is unfamiliar. Control structures are vital for making decisions and executing code based on specific conditions.

4. Operators

Operators are blocks that perform comparisons and logical operations. In this project, operators are used to check if a user’s input contains certain keywords or matches existing prompts. Operators are essential for making comparisons and performing calculations, which help your chatbot understand user input more accurately.

5. Sensing Blocks

Sensing blocks allow the program to interact with user inputs. In this chatbot, the Ask block is used to receive input from the user, and the Answer block stores this input. Sensing blocks are critical for creating interactive projects where your program needs to respond dynamically based on user interactions.

Written by Jamila Cocchiola who has always been fascinated with technology and its impact on the world. The technologies that emerged while she was in high school showed her all the ways software could be used to connect people, so she learned how to code so she could make her own! She went on to make a career out of developing software and apps before deciding to become a teacher to help students see the importance, benefits, and fun of computer science.