Creating a Chatbot in Python can seem daunting at first, but it’s fairly simple once you break it down. The basis of a chatbot is to send a message, and then to receive a response. In this example, I’ll be showing you how to make a chatbot for an online store.
This bot is similar to those that would appear on a website -- users can use it to ask for help or get information about when the store is open etc. This will serve almost as a shell, and after this, you can modify or add keywords and responses to customize it yourself!
1. The first step is to create two variables, user_prefix = “User: “ and bot_prefix, which is identical to user_prefix besides the title. These exist simply just so that you can tell when the bot or user is speaking.
2. Next, we create the dictionary of keywords in a variable named keywords_dict and have it equal to “greet” as the key, and “hello” as the value ({“greet”:”hello”}). You can add as many key-value combinations as you like.
3. Create the dictionary list of responses by creating a responses variable equal to {“greet”:”Hello! How can I help you?”}. Again, add as many responses and keywords as you want to add.
4. Now we will print a message so that it is displayed when the chatbot is started. Make sure to use bot_prefix and then add whatever message you would like to be displayed.
5. We need to create a while loop so the chatbot will run indefinitely; don’t worry, we will be adding an escape function later on. To create an indefinite loop, the easiest way to do this is with while (True).
6. We must grab the user’s input so they can type to the bot and request different information. Store this variable as user_input and have it equal to the input using user_prefix as the message to be displayed. It’s very important to end it with .lower(), doing this will convert their string to lowercase and make it much easier to sort and analyze.
7. Define the exit function so the user can exit the chatbot. We need to create an if statement and check to see if the user_input variable is equal to “exit” or “quit”, if true, make it print a thank you message and then use break to exit the program.
8. Create another variable named matched_intent and set that equal to None. This is essentially a placeholder value that will be changed later on.
9. Next we need to make a for loop with two variables, intent and pattern, to iterate through the items in the keywords dictionary. We also need to define an import for this program and we need to import re, or regular expression.
10. Now we set up an if statement using re.search to check the user’s input for keywords that we specified earlier in keywords_dict. If there is a match, than the matched_intent variable is set to intent.
11. Outside the for loop, we need to make a default response variable named key, set this to the keyword for the default response.
12. Create another if statement to check if matched_intent is in the responses dictionary, if this is true, set the value of key to the value of matched_intent.
13. Lastly, outside the if statement, create a print statement with the bot_prefix and selected intent by doing responses[key].
To reiterate, this code will prompt the user to enter in their question/message, then it analyzes what they said and determines if the user inputted a keyword. If there’s a match, it’ll take the keyword and pass it to the responses dictionary. It will match up the key with the desired value(response), and then this will print out the matching response.
Zach Hutton recently graduated from North Central Texas College with an Associates Degree in Cybersecurity, and he is currently a software development intern with us at StandardUser. Connect with him here on LinkedIn.
We are committed to preparing a new generation of cybersecurity professionals for success in the ever-changing digital world. To apply to be a part of our intern program, please contact us here.
Comments