Code: Chatbot
Search…
Getting Started
The code below will get you started with the iDEW chatbot template. It is a minimum starting point for a working chatbot that you will later modify and refine towards your design goals. There is a live example which doesn't do anything exciting -- that's up to you to change.

HTML

Below is the core html for the chatbot. The div element with an id="dialogue" is the block that contains the back-and-forth messages like you see in common messaging interfaces. The form element handles the user's text input at the bottom of the interface.
The required libraries are also loaded. PapaParse.js enables you to use a back-end database using Google Sheets. JQuery is a common library for Javascript programmers. Rivescript is the chatbot scripting language you will use (more on this later). Chatbot.js is the iDEW library that makes your programming easier.
Update February 11, 2022: The PapaParse library is now used, instead of the Tabletop library, to optionally import data from a spreadsheet. The change is seen at line 21 below.
HTML - (index.html)
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>iDEW Chatbot</title>
6
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7
<meta name="viewport" content="width=device-width, initial-scale=1">
8
<link rel="stylesheet" type="text/css" href="style.css">
9
</head>
10
11
<body>
12
<div class='wrapper'>
13
<div id="dialogue"></div>
14
<form onSubmit="return chatbot.sendMessage()">
15
<div class="text-box">
16
<input type="text" name="message" id="message" autocomplete="off" disabled placeholder="Please wait... loading...">
17
<input class="send-button" type="submit" value=">">
18
</div>
19
</form>
20
</div>
21
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>
22
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
23
<script src="https://unpkg.com/[email protected]/dist/rivescript.min.js"></script>
24
<script src="https://cdn.jsdelivr.net/gh/idewcomputing/code-chatbot/src/chatbot.js"></script>
25
<script src="script.js"></script>
26
</body>
27
28
</html>
Copied!

Javascript

Below is the minimal Javascript needed to get started. The setup ()function starts automatically once the page loads in the browser and simply loads the Rivescript file as your chatbot script to get things started.
JS (script.js)
1
function setup() {
2
chatbot.loadFiles(['bot.rive']);
3
}
4
5
window.onload = setup;
Copied!

Rivescript

The script below determines how your chatbot will converse with a visitor. The + start is automatically started to get the conversation going. The + * is a wildcard that catches anything that you have not programmed your chatbot to recognize. At first your chatbot recognizes little, but we will work on that soon.
Rivescript (bot.rive)
1
// conversation script is below
2
// "start" auto-runs to begin the bot conversation
3
4
+ start
5
- Hello. I am a brand new bot with little to say.
6
7
+ *
8
- Sorry, I don't understand that.
Copied!

CSS

There is a lot going on here with the styling of the chatbot. Use the code below as your starting point, but you may choose to change some elements later. A simple way to customize the look would be to change the background, font-family, and color of the body element.
CSS (style.css)
1
body {
2
background: linear-gradient(#A90329, #6d0019);
3
font-family: Verdana,Arial,Helvetica,sans-serif;
4
font-size: small;
5
color: #000000;
6
padding: 0;
7
margin: 0;
8
line-height: 1.7em;
9
}
10
11
button {
12
background-color: rgba(255,255,255,.5);
13
padding: 5px;
14
outline: none;
15
border: 1px solid rgba(0,0,0,.3);
16
border-radius: 5px;
17
}
18
19
.wrapper {
20
max-width: 450px;
21
height: 100vh;
22
box-sizing: border-box;
23
margin: auto;
24
padding: 0;
25
background-color: rgba(255,255,255,.1);
26
display: flex;
27
flex-direction: column;
28
border-left: 1px solid rgba(255,255,255,.2);
29
border-right: 1px solid rgba(255,255,255,.2);
30
}
31
32
form {
33
flex: 5;
34
display: block;
35
padding: 15px;
36
}
37
.text-box {
38
font-size: 16px;
39
display: flex;
40
width: 100%;
41
}
42
input#message {
43
padding-left: 11px;
44
padding-right: 9px;
45
font-size: 16px;
46
height: 27px;
47
display: block;
48
flex: 10;
49
outline: none;
50
margin: 0 10px 0 0;
51
box-sizing: border-box;
52
border: 1px solid #aaa;
53
border-radius: 13px;
54
}
55
56
input.send-button {
57
display: block;
58
background-color: rgba(255,255,255,.6);
59
flex: 1;
60
border: 1px solid rgba(0,0,0,.2);
61
height: 27px;
62
box-sizing: border-box;
63
font-size: 16px;
64
line-height: 25px;
65
padding: 0;
66
border-radius: 13px;
67
}
68
69
div#dialogue {
70
overflow-x: none;
71
overflow-y: auto;
72
display: block;
73
flex: 95;
74
box-sizing: border-box;
75
width: 100%;
76
padding: 15px;
77
}
78
79
.bot-row, .user-row {
80
padding: 1px 0 10px 0;
81
display: block;
82
postion: relative;
83
}
84
.user-row {
85
text-align: right;
86
}
87
88
.user, .bot {
89
font-size: 16px;
90
padding: 5px 8px;
91
margin: 1px;
92
border-radius: 12px;
93
display: inline-block;
94
postion: relative;
95
}
96
.bot {
97
margin-right: 50px;
98
background-color: rgba(255,255,255,.85);
99
border-bottom-left-radius: 0;
100
}
101
.user {
102
margin-left: 50px;
103
color: white;
104
background-color: #3b3;
105
border-bottom-right-radius: 0;
106
}
Copied!