Get startedGet started for free

Creating a personality

1. Creating a personality

Creating an engaging personality is a fun and absolutely crucial part of chatbot development. It's one of the key differences compared to any other kind of software

2. Why personality?

So why bother with a personality? If all you could do was type precise instructions to your bot, you would actually just have a command line application, not a chatbot. Most chatbots are embedded in a messaging app that people are comfortable using to talk to their friends. And you can expect that straightaway your users will want to make a bit of smalltalk, often before trying out any 'functionality' that they came for. It's not much effort to code up some responses to common questions, and is worth it for the improved user experience.

3. Smalltalk

The simplest thing we can do is use a python dictionary, with user messages as the keys and responses as the values. For example, here we define a dictionary called `responses`, with the messages `"what's your name?"` and `"what's today's weather"` as keys, and suitable responses as values. Next, we define a function called `respond`, which accepts a message as a single argument. This function tests if a message has a defined response by using the `in` keyword, that is "if message in responses". This statement only returns `True` if the message corresponds to one of the keys in the dictionary. Notice that this will only work if the user's message *exactly* matches a key in the dictionary. In later chapters you will build much more robust solutions. Notice that if there isn't a matching message, the `return` keyword will never be reached, so the function will return None.

4. Including variables

Since the world outside is always changing, your bot's answers have to be able to as well. The first thing you can do is add some placeholders to the responses. For example, instead of `"The weather is sunny"`, you can have a template string, like `"it's {} today"`. Then later, you can insert a variable `weather_today` by calling format weather today.

5. Choosing responses

Now, it quickly gets dull hearing the same responses over and over again, so it's a very good idea to add a little variety! To return completely different responses, we can replace the values in the responses dictionary with lists. Then when you're choosing a response, you can randomly select an answer from the appropriate list. To do this, import random, and use the random dot choice function, passing the list of options as an argument. For now, we're still relying on the user message matching our predefined messages *exactly*, but we'll soon move to a more flexible approach.

6. Asking questions

A great way to keep users engaged is to ask them questions, or invite them to go into more detail. This was actually one of the things which made the ELIZA bot so fun to talk to. Instead of using a bland default message like "I'm sorry, I didn't understand you", you can use some phrases that invite further conversation. Questions are a great way to achieve this. "Why do you think that?", "How long have you felt this way?", and "Tell me more!" are appropriate responses to many different kinds of message, and even when they don't quite match are more entertaining than a boring fallback.

7. Let's practice!