A chat bot is a program which allows mock communication with the computer or application. This can be developed by solely using Scratch; it involves many lists, operators, and the ask [] and wait block in particular. This tutorial shows how to make one.

Some examples for chatbots can include assistants in operating system projects.

Brief Description

To create a chat bot, the "ask" block needs to be used to enter a message. Then, the project takes that message, breaks it apart into words, and scans the list of words for specific words. Then, if the message contains those specific words, the chat bot can respond a pre-set message. To break apart the answer variable into words, each letter must be repetitively added to a list until a space is reached, and then create a new item in the list and continue adding the letters, forming words. To program this, only one list will be needed:

  • (words:: list)

and one or two variables:

  • (letter #)
  • (pick)
Note Note: The variables and lists can have arbitrary names.

Programming the Bot

The following code replication can be used to make the bot respond to messages. All these scripts can go in any sprite.

when gf clicked
forever
ask [Type a message] and wait
set [letter # v] to [1] //the iteration begins with the first letter of the answer
delete all of [words v] //clears the list of words; the first step is separating the words into a list
insert [] at (1) of [words v] //it needs a blank item to start out
repeat (length of (answer)) //one repetition for each letter
if <(letter (letter #) of (answer)) = [ ]> then //note that a space is inserted into this string, not nothing
insert [] at (1) of [words v] //since it is a space, create a new item
else
replace item (1) of [words v] with (join (item (1) of [words v]) (letter (letter #) of (answer))) //otherwise, add the letter to the current word
end
change [letter # v] by [1] //move on to the next letter
end
if <[words v] contains [hello]> then //word detection
say [Hello to you, too!] for (2) secs//response
end
if <[words v] contains [like]> then
if <(pick random (1) to (2)) = [1]> then //randomized responses
say [I like that, too!] for (2) secs
else
say [Really? I don't like that too much.] for (2) secs
end
end
end

For example, if "I like Scratch" is entered, the bot may respond "I like that too!" or "Really? I don't like that too much" because the list of words contained "like". The script can be made more complex by breaking it down into individual likes and more responses. The "pick" variable is used solely for making it respond one of multiple possible messages. However, if for instance, "I like Scratch" is typed in, the bot may respond "I like that too!", but if it is typed in again, it might say "Really? I don't like that too much.". To prevent this, some lists can be made to store words that the bot already responded to.

Make two lists:

  • (Likes:: list)
  • (Dislikes:: list)

Then change this part of the script:

if <[words v] contains [like]> then
if <(pick random (1) to (2)) = [1]> then //randomized responses
add (item (length of [words v]) of [words v]) to [Likes v]
if <not <[Likes v] contains (item (length of [words v]) of [words v])>> then
say [I like that, too!] for (2) secs
else
delete (length of [Likes v]) of [Likes v]
say [Didn't I already tell you I liked it?] for (2) secs
else
add (item (length of [words v]) of [words v]) to [Dislikes v]
if <not <[Dislikes v] contains (item (length of [words v]) of [words v])>> then
say [Really? I don't like it too much.] for (2) secs
else
delete (length of [Dislikes v]) of [Dislikes v]
say [Didn't I already tell you I didn't like it?] for (2) secs
end
Note Note: This might not work if there is more than one word after "like"

Final Product

Excluding scripts in the next section, the whole script would be something like this:

when gf clicked
forever
ask [type a message] and wait
set [letter # v] to [1] //so the iteration begins with the first letter of the answer
delete all of [words v] //clears the list of words; the first step is separating the words into a list
insert [] at (1) of [words v] //it needs a blank item to start out
repeat (length of (answer)) //one repetition for each letter
if <(letter (letter #) of (answer)) = [ ]> then //note that a space is inserted into this string, not nothing
insert [] at (1) of [words v] //since it is a space, create a new item
else
replace item (1) of [words v] with (join (item (1) of [words v]) (letter (letter #) of (answer))) //otherwise, add the letter to the current word
end
change [letter # v] by [1] //move on to the next letter
end
if <[words v] contains [hello]> then //word detection
say [Hello to you, too!] for (2) secs//response
end
if <[words v] contains [like]> then
if <(pick random (1) to (2)) = [1]> then //randomized responses
add (item (length of [words v]) of [words v]) to [Likes v]
if <not <[Likes v] contains (item (length of [words v]) of [words v])>> then
say [I like that, too!] for (2) secs
else
delete (length of [Likes v]) of [Likes v]
say [Didn't I already tell you I liked it?] for (2) secs
else
add (item (length of [words v]) of [words v]) to [Dislikes v]
if <not <[Dislikes v] contains (item (length of [words v]) of [words v])>> then
say [Really? I don't like it too much.] for (2) secs
else
delete (length of [Dislikes v]) of [Dislikes v]
say [Didn't I already tell you I didn't like it?] for (2) secs
end

Taking it Further

To make the chat bot more realistic, a separate list of previously stored words can be created from old entered messages, and make the bot refer to those later in the conversation. For example, if it is told that the user likes programming, and then is later told that the user likes Scratch, it can relate the two using the list of old words and say "Of course you like Scratch, you like programming" by sensing if the list of old words contains "programming".

The script could also be changed to make the bot understand even more words, using this:

if <[words v] contains [Word you would like bot to understand]> then //word detection
say [Response you would like your bot to say] for (2) secs //response
end

Simplified Version

A somewhat simplified version, which uses fixed prompts and responses, is as follows:

when @greenFlag clicked
forever
    ask [What would you like to talk about?] and wait
    if <[Prompts v] contains (answer)?::list> then
        say (item (item # of (answer) in [Prompts v]) of [Responses v]) for (2) seconds
    else
        add (answer) to [Prompts v]
        say [Hmm... I'm not sure how to respond to that.] for (2) seconds
        ask [What should I say next time?] and wait
        add (answer) to [Responses v]
    end
end

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.