Skip to content

Commit 8c38d99

Browse files
author
Oleksandr Shvetsov
committed
Updated README for chat_bots samples
1 parent 4901db1 commit 8c38d99

File tree

2 files changed

+326
-10
lines changed

2 files changed

+326
-10
lines changed
Lines changed: 193 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,196 @@
1-
# RiveScript powered chat bot example
1+
## **Overview**
22

3-
This is an example of 1-to-1 chatbot with [RiveScript](https://www.rivescript.com) scripting language integrated, making it easy to write trigger/response pairs for building up a bot's intelligence.
3+
Bots are third-party applications that run inside QuickBlox platform. Bots have almost the same qualities as humans: they have profile photos, names, they can send messages and upload files, and they can be added to and removed from private group chats. Chatbots are controlled programmatically via [QuickBlox Javascript/Node.js SDK](https://docs.quickblox.com/docs/js-quick-start).
44

5-
# Guide
6-
Read Chat Bots guide to understand how to build own bot for QuickBlox with RiveScript.
5+
## **How To Build Chatbots**
76

8-
[https://quickblox.com/developers/ChatBots](https://quickblox.com/developers/ChatBots)
7+
### **Create a new app in the Dashboard**
8+
9+
QuickBlox application includes everything that brings messaging right into your application - chat, video calling, users, push notifications, etc. To create a QuickBlox application, follow the steps below:
10+
1. Register a new account following [this link](https://admin.quickblox.com/signup). Type in your email and password to sign in. You can also sign in with your Google or Github accounts.
11+
2. Create the app clicking **New app** button.
12+
3. Configure the app. Type in the information about your organization into corresponding fields and click **Add** button.
13+
4. Go to **Dashboard => *YOUR_APP* => Overview** section and copy your **Application ID**, **Authorization Key**, **Authorization Secret**, and **Account Key** .
14+
15+
### **Create bot user**
16+
17+
To create a bot user, follow the steps below:
18+
19+
1. Go to **Dashboard => *YOUR_APP* => Users => Add new user** direction
20+
2. Copy **user ID**. The user ID appears in the table with users once a User is created
21+
3. Copy **password**. The password is taken from the **Add user** form.
22+
4. Save **user ID** and **password** somewhere. We will use these 2 values later on.
23+
24+
### **Create Node.js carcass application**
25+
26+
Open terminal and type the following commands:
27+
28+
```bash
29+
mkdir my_awesome_bot
30+
cd my_awesome_bot
31+
npm init
32+
```
33+
34+
This will ask you a bunch of questions, and then write a package.json file for you. More information on [npm init](https://docs.npmjs.com/cli/init). The main thing is that we have now a **package.json** file and can start to develop our first chatbot.
35+
36+
### **Connect QuickBlox Javascript/Node.js SDK**
37+
38+
In terminal type the following command:
39+
40+
```bash
41+
npm install quickblox --save
42+
```
43+
44+
### **Create index.js file**
45+
46+
Type the following command in terminal:
47+
48+
```bash
49+
touch index.js
50+
```
51+
52+
It will create the main entry point for your bot. Open this file and let's write some logic.
53+
54+
### **Making your bot heart beat**
55+
56+
Open **index.js** file and write the following code:
57+
58+
```javascript
59+
"use strict";
60+
61+
const QB = require("quickblox");
62+
63+
const CONFIG = {
64+
appId: "...",
65+
authKey: "...",
66+
authSecret: "...",
67+
botUser: {
68+
id: "...",
69+
password: "...",
70+
},
71+
};
72+
73+
// Initialise QuickBlox
74+
QB.init(CONFIG.appId, CONFIG.authKey, CONFIG.authSecret);
75+
76+
// Connect to Real-Time Chat
77+
QB.chat.connect(
78+
{
79+
userId: CONFIG.botUser.id,
80+
password: CONFIG.botUser.password,
81+
},
82+
(chatConnectError) => {
83+
if (chatConnectError) {
84+
console.log(
85+
"[QB] chat.connect is failed",
86+
JSON.stringify(chatConnectError)
87+
);
88+
process.exit(1);
89+
}
90+
91+
console.log("[QB] Bot is up and running");
92+
93+
// Add chat messages listener
94+
QB.chat.onMessageListener = onMessageListener;
95+
}
96+
);
97+
98+
function onMessageListener(userId, msg) {
99+
// process 1-1 messages
100+
if (msg.type == "chat") {
101+
if (msg.body) {
102+
let answerMessage = {
103+
type: "chat",
104+
body: msg.body, // echo back original message
105+
extension: {
106+
save_to_history: 1,
107+
},
108+
};
109+
110+
QB.chat.send(userId, answerMessage);
111+
}
112+
}
113+
}
114+
115+
process.on("exit", function () {
116+
console.log("Kill bot");
117+
QB.chat.disconnect();
118+
});
119+
```
120+
121+
This is a simple bot that simply replies back with origin message.
122+
123+
For `CONFIG` variable put the following values:
124+
125+
1. **Bot user ID** and **password**. Get these values following **Dashboard => *YOUR_APP* => Users => Add new user** direction. The user ID appears in the table with users once a User is created and the password is taken from the **Add user** form.
126+
127+
2. **Application ID**, **Authorization Key**, and **Authorization Secret**. Get these values following **Dashboard => *YOUR_APP* => Overview** direction and copy them from here.
128+
129+
## **Improve bot's intelligence**
130+
131+
Usually, it's not enough just to build a simple bot that echoes your messages. It's better to add some intelligence for your bot. For this purpose, we need to connect some bot's intelligence platform/library to your bot. There is a cool product called [https://www.rivescript.com](https://www.rivescript.com/). RiveScript is a scripting language for chatbots, making it easy to write trigger/response pairs for building up a bot's intelligence.
132+
133+
### **Install RiveScript**
134+
135+
Inside your bot directory run the following command to install RiveScript:
136+
```bash
137+
npm install rivescript --save
138+
```
139+
140+
### **Prepare '.rive' file**
141+
142+
RiveScript is a text-based scripting language meant to aid in the development of interactive chatbots. To write your own RiveScript code, you will only need a simple text editing program. A RiveScript document is a text file containing RiveScript code. These files will have a **.rive** extension. An example file name would be **replies.rive**.
143+
144+
Create a **txt** file in your bot's directory and name it **replies.rive**. Then go to [https://www.rivescript.com/try,](https://www.rivescript.com/try) choose **rs-standard.rive** template and copy all content into your file.
145+
146+
Read [more information](https://www.rivescript.com/docs/tutorial) on RiveScript code.
147+
148+
### **Connect RiveScript to your bot**
149+
150+
Open **index.js** file and add the following code:
151+
```javascript
152+
const RiveScript = require("rivescript");
153+
154+
//...
155+
156+
// Init RiveScript logic
157+
const riveScriptGenerator = new RiveScript();
158+
159+
function loadingDone(batch_num) {
160+
console.log("[RiveScript] Batch #" + batch_num + " has finished loading!");
161+
riveScriptGenerator.sortReplies();
162+
}
163+
164+
function loadingError(batch_num, error) {
165+
console.log(
166+
"[RiveScript] Load the batch #" + batch_num + " is failed",
167+
JSON.stringify(error)
168+
);
169+
}
170+
171+
// load our replies file
172+
riveScriptGenerator.loadFile("replies.rive", loadingDone, loadingError);
173+
174+
//...
175+
176+
let answerMessage = {
177+
type: "chat",
178+
body: riveScriptGenerator.reply(userId, msg.body),
179+
extension: {
180+
save_to_history: 1,
181+
},
182+
};
183+
//...
184+
```
185+
186+
Here we initialize RiveScript, then load all replies flows from **replies.rive** file and build message reply based on RiveScript replies flows.
187+
188+
### **Run our bot**
189+
190+
Type the following command to run our bot in terminal:
191+
192+
```bash
193+
node index.js
194+
```
195+
196+
Now you can write something to your bot and will receive a reply. See the documentation at https://docs.quickblox.com/docs/js-quick-start.
Lines changed: 133 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,136 @@
1-
# Simple chat bot example
1+
## **Overview**
22

3-
This example demonstrates simple 1-to-1 chat bot. It can simply echo incoming messages.
3+
Bots are third-party applications that run inside QuickBlox platform. Bots have almost the same qualities as humans: they have profile photos, names, they can send messages and upload files, and they can be added to and removed from private group chats. Chatbots are controlled programmatically via [QuickBlox Javascript/Node.js SDK](https://docs.quickblox.com/docs/js-quick-start).
44

5-
# Guide
6-
Read Chat Bots guide to understand how to build own bot for QuickBlox.
5+
## **How To Build Chatbots**
76

8-
[https://quickblox.com/developers/ChatBots](https://quickblox.com/developers/ChatBots)
7+
### **Create a new app in the Dashboard**
8+
9+
QuickBlox application includes everything that brings messaging right into your application - chat, video calling, users, push notifications, etc. To create a QuickBlox application, follow the steps below:
10+
1. Register a new account following [this link](https://admin.quickblox.com/signup). Type in your email and password to sign in. You can also sign in with your Google or Github accounts.
11+
2. Create the app clicking **New app** button.
12+
3. Configure the app. Type in the information about your organization into corresponding fields and click **Add** button.
13+
4. Go to **Dashboard => *YOUR_APP* => Overview** section and copy your **Application ID**, **Authorization Key**, **Authorization Secret**, and **Account Key** .
14+
15+
### **Create bot user**
16+
17+
To create a bot user, follow the steps below:
18+
19+
1. Go to **Dashboard => *YOUR_APP* => Users => Add new user** direction
20+
2. Copy **user ID**. The user ID appears in the table with users once a User is created
21+
3. Copy **password**. The password is taken from the **Add user** form.
22+
4. Save **user ID** and **password** somewhere. We will use these 2 values later on.
23+
24+
### **Create Node.js carcass application**
25+
26+
Open terminal and type the following commands:
27+
```bash
28+
mkdir my_awesome_bot
29+
cd my_awesome_bot
30+
npm init
31+
```
32+
33+
This will ask you a bunch of questions, and then write a package.json file for you. More information on [npm init](https://docs.npmjs.com/cli/init). The main thing is that we have now a **package.json** file and can start to develop our first chatbot.
34+
35+
### **Connect QuickBlox Javascript/Node.js SDK**
36+
37+
In terminal type the following command:
38+
39+
```bash
40+
npm install quickblox --save
41+
```
42+
43+
### **Create index.js file**
44+
45+
Type the following command in terminal:
46+
```bash
47+
touch index.js
48+
```
49+
50+
It will create the main entry point for your bot. Open this file and let's write some logic.
51+
52+
### **Making your bot heart beat**
53+
54+
Open **index.js** file and write the following code:
55+
56+
```javascript
57+
"use strict";
58+
59+
const QB = require("quickblox");
60+
61+
const CONFIG = {
62+
appId: "...",
63+
authKey: "...",
64+
authSecret: "...",
65+
botUser: {
66+
id: "...",
67+
password: "...",
68+
},
69+
};
70+
71+
// Initialise QuickBlox
72+
QB.init(CONFIG.appId, CONFIG.authKey, CONFIG.authSecret);
73+
74+
// Connect to Real-Time Chat
75+
QB.chat.connect(
76+
{
77+
userId: CONFIG.botUser.id,
78+
password: CONFIG.botUser.password,
79+
},
80+
(chatConnectError) => {
81+
if (chatConnectError) {
82+
console.log(
83+
"[QB] chat.connect is failed",
84+
JSON.stringify(chatConnectError)
85+
);
86+
process.exit(1);
87+
}
88+
89+
console.log("[QB] Bot is up and running");
90+
91+
// Add chat messages listener
92+
QB.chat.onMessageListener = onMessageListener;
93+
}
94+
);
95+
96+
function onMessageListener(userId, msg) {
97+
// process 1-1 messages
98+
if (msg.type == "chat") {
99+
if (msg.body) {
100+
let answerMessage = {
101+
type: "chat",
102+
body: msg.body, // echo back original message
103+
extension: {
104+
save_to_history: 1,
105+
},
106+
};
107+
108+
QB.chat.send(userId, answerMessage);
109+
}
110+
}
111+
}
112+
113+
process.on("exit", function () {
114+
console.log("Kill bot");
115+
QB.chat.disconnect();
116+
});
117+
```
118+
119+
This is a simple bot that simply replies back with origin message.
120+
121+
For `CONFIG` variable put the following values:
122+
123+
1. **Bot user ID** and **password**. Get these values following **Dashboard => *YOUR_APP* => Users => Add new user** direction. The user ID appears in the table with users once a User is created and the password is taken from the **Add user** form.
124+
125+
2. **Application ID**, **Authorization Key**, and **Authorization Secret**. Get these values following **Dashboard => *YOUR_APP* => Overview** direction and copy them from here.
126+
127+
128+
### **Run our bot**
129+
130+
In terminal type the following command:
131+
132+
```bash
133+
node index.js
134+
```
135+
136+
Now you can write something to your bot and will receive a reply. See the documentation at https://docs.quickblox.com/docs/js-quick-start.

0 commit comments

Comments
 (0)