<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[AI - A Chatbot for Taking Orders (Difficulty: 4)]]></title><description><![CDATA[<h2><a class="anchor-offset" name="key-topics-covered"></a>Key Topics Covered</h2>
<ul>
<li><a href="/topic/338/speech-to-text-premium-only">AI for speech to text</a></li>
<li><a href="/topic/341/say-sentences-with-an-ai-speaker-premium-only">AI for text to speech</a></li>
<li><a href="/topic/351/ai-for-analyzing-sentences-premium">AI for analyzing a sentence</a></li>
<li><a href="/topic/66/introduction-to-widgets-with-the-label-widget">Using the label widget</a></li>
<li><a href="/topic/86/introduction-to-variables">Using variables</a></li>
</ul>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="introduction"></a>Introduction</h2>
<p dir="auto">In this tutorial, we’ll build a chatbot that can take orders from customers (users), similar to the services provided at McDonald’s Drive-Thru.</p>
<p dir="auto"></p><div class="embed-wrapper"><div class="embed-container"><iframe src="https://www.youtube.com/embed/4nBBhIFqaZM" frameborder="0" allowfullscreen></iframe></div></div><p></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-1-remix-the-project-template"></a>Step 1 - Remix the Project Template</h3>
<p dir="auto">A template has been prepared for you, which includes all the images you need. Please log into the CreatiCode playground, and remix this project. Feel free to change the backdrop or sprite images.</p>
<p dir="auto"><a href="http://play.creaticode.com/projects/b748ef8a60fd9ff0f86dd3a6" target="_blank" rel="noopener noreferrer nofollow ugc">play.creaticode.com/projects/b748ef8a60fd9ff0f86dd3a6</a></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-2-greet-the-customer"></a>Step 2 - Greet the Customer</h3>
<p dir="auto">When the project starts, our chatbot should greet the customer with a question like “Hi, what would you like to order?”. Please select the “AI” sprite, and add the green flag block and the <a href="/topic/341/say-sentences-with-an-ai-speaker-premium-only">“say” block</a> from the “AI” category (premium-subscription required):</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1759e037-2281-4587-80f0-0c412ed8857c.png" width="900" style="filter:drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5))" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-3-make-key-blocks"></a>Step 3 - Make Key Blocks</h3>
<p dir="auto">Since this will be a relatively large project, it is a good idea to define a few key blocks to organize our code blocks.</p>
<p dir="auto">For a simple chatbot, we need to get the customer’s input, then analyze the <strong>intention</strong> of the customer, and then give a response. Therefore, let’s define 3 blocks in the “AI” sprite like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f320da17-d11b-4c7f-aa70-d8203469b22a.png" width="900" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-4-get-customer-input"></a>Step 4 - Get Customer Input</h3>
<p dir="auto">The customer can talk to our chatbot directly. We can use the "<a href="/topic/338/speech-to-text-premium-only">speech recognition</a>“ blocks to listen to the customer.</p>
<p dir="auto">Note that when we start the speech recognition process, the “text from speech” block will be reset to empty. After the customer has completed a sentence, “text from speech” will be populated with that sentence. So we can wait until “text from speech” is no longer empty, then end the recognition.</p>
<p dir="auto">Also, let’s use a <a href="/topic/86/introduction-to-variables">variable</a> named “input” to store the recognized text. We can convert the text to lower case only, so we do not need to worry about handling both upper and lower case letters.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4451d335-6451-4cd1-bfb5-cb6c4334842c.png" width="500" /></p>
<p dir="auto">Now if you run the project, you will hear the question from the chatbot, then a red mic will pop up to get your input. You can say something like “What’s on the menu?”, and you should see that sentence stored in the “input” variable. <span style="color:#fa0000">Note that you should only start talking after the mic icon shows up.</span></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/39e8260d-db4d-4d10-937d-a1124fcb1ba1.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-5-analyze-customer-intention"></a>Step 5 - Analyze Customer Intention</h3>
<p dir="auto"><strong>Although we can recognize the customer’s speech, we are still far away from understanding the customer’s intention</strong>. There are unlimited possibilities for what the customer might say, so there is no way for us to write a program that handles every possibility.</p>
<p dir="auto">Fortunately, we do not have to. Since we are creating a chatbot for taking orders, we can focus on only a few relevant intentions, such as “checking the menu”, “placing an order”, etc. Then we just ignore all other possible intentions.</p>
<p dir="auto">To get started, suppose the customer says “what’s on the menu?” or “what’s available?”, then we are pretty sure his/her intention is to check what’s available on the menu. Therefore, we can map these 2 possible inputs to the same intention of “checking the menu”.</p>
<p dir="auto">We will use a new variable “intention” to store this information. By default, the intention is “unknown”, but we can improve our guess based on the input.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bfad6712-c33b-4e9c-b2da-4fccfe635cd1.png" width="500" /></p>
<p dir="auto">Now if we say “what’s on the menu” or “what’s available”, we will get the same intention of “checking the menu”:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7164823a-aa86-42e2-bb10-ab3614697a1c.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-6-use-keywords"></a>Step 6 - Use Keywords</h3>
<p dir="auto">Currently, when we try to guess the customer’s intention, we are looking for entire sentences like “what’s on the menu”. This is too narrow. For example, if the customer says “Can you give me the menu?”, then our chatbot would not treat this as the intention of “checking the menu”.</p>
<p dir="auto">To improve this, we should check for keywords instead. For example, if the customer input contains “menu” or “available”, then it’s good enough to set the intention to “checking the menu”.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8fe3ef6e-0e51-4da1-aa87-b7dca3063ba5.png" width="400" /></p>
<p dir="auto">Now if we say “tell me the menu please”, the chatbot would still be able to get the right intention.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bb56614a-0c23-40cc-8dfa-019a65676be2.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto">Can you think of other keywords for the “checking the menu” intention?</p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-7-respond-based-on-the-intention"></a>Step 7 - Respond Based on the Intention</h3>
<p dir="auto">To respond to the customer, we need to check the customer’s intention.</p>
<p dir="auto">If the intention is “checking the menu”, we simply need to offer the list of items available. If the intention is “unknown”, we have to request the customer to say it again.</p>
<p dir="auto">We will store our response in a new variable as well, and use the “say” block again to speak it out.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/fc8cb663-b1f0-4325-9326-3c85bdf3ab2f.png" width="900" /></p>
<p dir="auto">Now our chatbot will be able to respond to us verbally:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/90864570-1906-4d3b-a3db-989a59a84222.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-8-testing-vs-production"></a>Step 8 - Testing vs Production</h3>
<p dir="auto">Before we improve our chatbot to handle more customer inquiries, we need to make our development process more efficient.</p>
<p dir="auto">Currently, our chatbot says the greeting words and then listens to the customer’s speech. That slows down our development, as we need to test our chatbot a lot.</p>
<p dir="auto">To solve this problem, <strong>we can change our program to run 2 modes: production mode and testing mode</strong>. In the production mode, the program will work the same way as now. However, in the testing mode, we will simply set the customer input directly and bypass the  “say” and “listen” parts. We also do not need to speak out the response in testing mode.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0d010147-c59d-4ed8-a83c-d1a5c2e455e3.png" width="900" /></p>
<p dir="auto">Here is what we need to do to add the testing mode:</p>
<ul>
<li>Add a new variable “is testing”, and set it to 1 when we are working in the testing mode. So we would only “say” the greeting words if we are not testing.</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7fae1fe0-67ca-4111-b03a-b2105065dfcc.png" width="900" style="margin-left:40px" /></p>
<ul>
<li>When we are in testing, set the “input” variable directly instead of getting it via voice</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e6a5a0f7-6757-4266-866b-6de741b36c36.png" width="500" style="margin-left:40px" /></p>
<ul>
<li>At the end, we only need to “say” the response if we are not testing</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1210d4ef-d1b5-4102-958f-53586157bcc7.png" width="900" style="margin-left:40px" /></p>
<p dir="auto"> <br />
Now if you run to run the program in testing mode, it generates the response immediately:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/90e4f605-4221-4d96-893d-dbc946a4d9fd.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-9-a-new-intention-placing-an-order"></a>Step 9 - A New Intention “Placing an Order”</h3>
<p dir="auto">Now we are ready to add a new intention type: “placing an order”. For example, the customer might say “I’ll have 2 burgers”, “May I have a hamburger, please?”, or “Can I get a chicken nugget?”.</p>
<p dir="auto">To map all these inputs to the “placing an order” intention, we can check for the keywords “I’ll have”, “may I have” and “can I get”.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b0e3470e-25b4-4013-8e76-8002321c57a1.png" width="450" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-9-get-the-product-type"></a>Step 9 - Get the Product Type</h3>
<p dir="auto">When we know the customer’s intention is “placing an order”, we still need to find out exactly which product they are buying. There are 4 product types: burgers, french fries, chicken nuggets and coke.</p>
<p dir="auto">Please make a new block for “get product”, and use a new variable “product” to store the product type. By default the product type is “unknown”, and we can search for some keywords in the customer input to guess the product type.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/fe973943-023c-4bf2-ae84-af74b6bcd4a8.png" width="350" /></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9704d6bc-db4b-48c2-84ab-c5578de56b23.png" width="350" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-10-get-the-product-count"></a>Step 10 - Get the Product Count</h3>
<p dir="auto">After getting the product type, we also need to know how many of it the customer needs. This step is more complicated than it appears because there are many ways for the customer to specify the count, such as “two burgers”, “a coke” or “eight large french fries”.</p>
<p dir="auto">To solve the problem, the key is to find the number word in the customer input. To do that, we can use the “<a href="/topic/351/ai-for-analyzing-sentences-premium">analyze sentence</a>” block, which will tell us the type of every word in the sentence.</p>
<p dir="auto">Let’s first add a new block “get count”, and run the “analyze sentence” block. Note that we need to create a new table named “result” to store the output of the analysis:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/878262d0-7aca-4294-99fe-04755122ea29.png" width="800" /></p>
<p dir="auto"> <br />
Next, set the input variable to “I’ll have two burgers”, and turn on the monitor for the “result” table:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6751dcae-697e-4bbb-92ab-ef123e83293f.png" width="570" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto">As shown, each word takes one row in the result table, and the word “two” is assigned a type of “Number”. This is very helpful, because we just need to look for the “Number” type to find out the number of “two”. To do that, we can use the <a href="/topic/81/reading-from-a-table">table lookup block</a>. Also, if the count is an empty string " ", we will assume the count is 1.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e42be76e-9046-4132-b161-bc972143dab6.png" width="800" /></p>
<p dir="auto">Since the product count we get maybe a word like “two” or “eight”, we need to convert it to an Arabic number:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/51a28bfc-7aaf-4c29-b37e-f9e8353364af.png" width="800" /></p>
<p dir="auto">Now when we run the program, we should see the “count” variable is set to the correct Arabic number:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/603d7b33-8319-40c7-9f63-95eb7906ca58.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-11-labels-for-product-counts"></a>Step 11 - Labels for Product Counts</h3>
<p dir="auto">We should display the count for each product, so it’s easy for the customer to review the order.</p>
<p dir="auto">To do that, we need to <a href="/topic/66/introduction-to-widgets-with-the-label-widget">add a label</a> under each product image when the green flag is clicked. For example, in the burger sprite, we can add the following blocks:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1dcbe2a9-66c0-43a2-a302-2b1ab960ead2.png" width="800" /></p>
<p dir="auto">You can copy these blocks to the other 3 products’ sprites as well. Note that the names of the label widgets are all formatted as the product name followed by “label”.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ad103569-3559-4bf6-9453-0dea6a0fd5ff.png" width="800" /></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/38e08f82-1b0c-4014-b72c-4bd7a87a51ef.png" width="800" /></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7ca46e34-e030-4ef5-ac9b-c9d24271af91.png" width="800" /></p>
<p dir="auto">As a result, we should see 4 labels like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/21f99ae4-c9b5-4099-b06f-f98b8b4c4d50.png" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h3><a class="anchor-offset" name="step-12-respond-to-placing-an-order"></a>Step 12 - Respond to “Placing an Order”</h3>
<p dir="auto">Now we are finally ready to respond to the new intention “placing an order”. This should be a new “if” block inside the “respond” block’s definition:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ffd150b8-7ad2-45c3-9631-dc322d9f85c3.png" width="450" /></p>
<p dir="auto">First, we need to change the text of the label for the product being ordered. For example, if the “product” variable is “burger”, we need to set the “burgerlabel” to the value of the “count” variable. We can use the “join” block to get the label name, so we don’t have to write a separate logic for each product:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f9489d1a-f3b4-4c5b-a0af-c082a3998d51.png" width="450" /></p>
<p dir="auto">After that, we need to set the “response” variable to what we will say to the customer. For example, we can say “Sure, 1 burger”. Here we need to compose the response using the “count” and “product” variables:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/fa10e438-452d-4441-9a21-eadcd47a488e.png" width="800" /></p>
<p dir="auto">There is still a small issue. When the count is more than 1 and the product is “burger”, we need to add an “s” at the end of the product name.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/cbb0600d-5e1d-48c8-895f-b062e8e34179.png" width="900" /></p>
<p dir="auto">Here is the final program:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8c4cc708-78e8-4d04-91a2-a082ed90c0e3.png" width="900" /></p>
<p dir="auto">Now our response captures both the product name and the count:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d61e8e3f-0435-469c-bfef-e4b8951c880e.gif" width="470" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> </p>
<h2><a class="anchor-offset" name="step-13-publish-the-program"></a>Step 13 - Publish the Program</h2>
<p dir="auto">Now we have a very simple chatbot that can offer the menu and take new orders. To publish it to users, we need to change the “is testing” variable to 0 (any value that’s not 1).</p>
<p dir="auto">Also, to repeatedly take customer inputs, we need to add a “forever” loop around our key blocks:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/893019ea-2d7b-4cc0-9087-98f8a936d3d6.png" width="400" /></p>
<p dir="auto"> <br />
Now our chatbot can have a simple conversation with the customer like this:</p>
<p dir="auto"><strong>Chatbot:</strong> Hi, what would you like to order?<br />
<strong>Customer:</strong> What’s on the menu?<br />
<strong>Chatbot:</strong> You can order burgers, french fries, chicken nuggets or coke<br />
<strong>Customer:</strong> I’ll have two burgers.<br />
<strong>Chatbot:</strong> Sure, 2 burgers.<br />
<strong>Customer:</strong> Can I have a French fries as well?<br />
<strong>Chatbot:</strong> Sure, 1 fries.</p>
<p dir="auto">Here is a demo video. <strong>Note that you need to wait for the mic icon to appear to start talking.</strong></p>
<p dir="auto"></p><div class="embed-wrapper"><div class="embed-container"><iframe src="https://www.youtube.com/embed/4nBBhIFqaZM" frameborder="0" allowfullscreen></iframe></div></div><p></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="creative-ideas"></a>Creative Ideas</h2>
<p dir="auto">Chatbots can be very complex if you want them to talk to customers like human beings. Here are some new ideas for you to explore:</p>
<ol>
<li><strong>Handle more customer inputs</strong>: there are many other ways for the customer to place an order or check the menu. You can make a list of all possible ways, and try to handle them in the “get customer intention” logic.</li>
<li><strong>Other Intentions</strong>: such as “Changing product count”, “Removing a product”, “Asking for total cost”, etc.</li>
<li><strong>Product Size</strong>: for example, for french fries and coke, we might offer “large”, “medium” and “small” sizes</li>
<li><strong>Other Scenes</strong>: you can build a different chatbot that serves other purposes, such as a chatbot at restaurants that handle customer reservations, a FAQ chatbot that answers all kinds of questions on a specific topic, a fun chatbot that cracks jokes or plays Youtube videos.</li>
</ol>
]]></description><link>https://forum.creaticode.com/topic/391/ai-a-chatbot-for-taking-orders-difficulty-4</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Jul 2026 16:55:25 GMT</lastBuildDate><atom:link href="https://forum.creaticode.com/topic/391.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Jul 2022 13:53:37 GMT</pubDate><ttl>60</ttl></channel></rss>