Develop a Rocket.Chat SDK Bot
This bot is a simple example of how you can use Rocket.Chat JS SDK methods directly. It is extremely basic and does not handle errors, different message types, server resets and other production situations.

RocketChat SDK Bot Quick Start Guide

Rocket.Chat Bot is a Node.js package and works with Node and npm.

Install Rocket.Chat SDK Package

Open up terminal in the directory you wish to develop your bot in and run the following command
1
npm init -y
2
npm install @rocket.chat/sdk
Copied!

Create SDK Bot Files

To proceed with the simplest setup, you need to create two files: the first one will be responsible for the working logic of the bot. Another one will contain a map with the responses.
  • create a server.js file with the following content:
1
const { driver } = require('@rocket.chat/sdk');
2
const respmap = require('./reply');
3
4
// Environment Setup
5
const HOST = '<ROCKETCHAT HOST>';
6
const USER = '<BOT USER NAME>';
7
const PASS = '<BOT USER PASS>';
8
const BOTNAME = '<ROCKET CHAT BOT ALIAS>';
9
const SSL = '<SSL USAGE>';
10
const ROOMS = ['<ROCKETCHAT CHANNEL>'];
11
var myUserId;
12
13
// Bot configuration
14
const runbot = async () => {
15
const conn = await driver.connect({ host: HOST, useSsl: SSL })
16
myUserId = await driver.login({ username: USER, password: PASS });
17
const roomsJoined = await driver.joinRooms( ROOMS );
18
console.log('joined rooms');
19
20
const subscribed = await driver.subscribeToMessages();
21
console.log('subscribed');
22
23
const msgloop = await driver.reactToMessages( processMessages );
24
console.log('connected and waiting for messages');
25
26
const sent = await driver.sendToRoom( BOTNAME + ' is listening ...', ROOMS[0]);
27
console.log('Greeting message sent');
28
}
29
30
// Process messages
31
const processMessages = async(err, message, messageOptions) => {
32
if (!err) {
33
if (message.u._id === myUserId) return;
34
const roomname = await driver.getRoomName(message.rid);
35
36
console.log('got message ' + message.msg)
37
var response;
38
if (message.msg in respmap) {
39
response = respmap[message.msg];
40
}
41
const sentmsg = await driver.sendToRoomId(response, message.rid)
42
}
43
}
44
45
runbot()
Copied!
NOTE: Adjust the Environment Setup section content to fit your server and user credentials.
Make sure <BOT USER NAME> has a BOT role on the server. For more information on how to create a bot user please refer to this page.
  • create a reply.js file with the following content:
1
const respmap = {
2
"hi" : "hey",
3
"u da bot" : "no, YOU da bot",
4
"no u da bot" : "Come'on - YOU DA BOT!!",
5
"I give up" : "ok. silly human :rolleyes:"
6
};
7
8
module.exports = respmap;
Copied!

Run the SDK Bot

1
node server.js
Copied!
After executing the last command, Rocket.Chat SDK bot will try to connect to the Rocket.Chat instance and then will listen to messages in general room:
1
$ node server.js
2
[connect] Connecting { username: 'username',
3
password: 'pass',
4
ldap: false,
5
host: 'rocket.chat.host',
6
useSsl: true,
7
timeout: 20000,
8
rooms: [],
9
allPublic: false,
10
dm: false,
11
livechat: false,
12
edited: false,
13
integrationId: 'js.SDK',
14
roomCacheMaxSize: 10,
15
roomCacheMaxAge: 300000,
16
dmCacheMaxSize: 10,
17
dmCacheMaxAge: 100000 }
18
[connect] Connected
19
[login] Logging in botUser
20
[getRoomIdByNameOrId] Calling (caching): general
21
[getRoomIdByNameOrId] Success: "GENERAL"
22
[joinRoom] Calling (async): ["GENERAL"]
23
[joinRoom] Success
24
joined rooms
25
[subscribe] Preparing subscription: stream-room-messages: __my_messages__
26
[subscribe] Stream ready: 4
27
subscribed
28
[reactive] Listening for change events in collection stream-room-messages
29
connected and waiting for messages
30
[getRoomIdByNameOrId] Calling (cached): general
31
[getRoomIdByNameOrId] Success: "GENERAL"
32
[sendMessage] Calling (async): [{"msg":"Rocket.Chat BOT is listening ...","bot":{"i":"js.SDK"},"rid":"GENERAL"}]
33
[sendMessage] Success: {"msg":"Rocket.Chat BOT is listening ...","bot":{"i":"js.SDK"},"rid":"GENERAL","ts":{"$date":1573598821912},"u":{"_id":"GDJn6Exf9LpK2Pp5n","username":"botUser"},"mentions":[],"channels":[],"_updatedAt":{"$date":1573598821920},"_id":"6yTcbcNKuMDBdPsFc"}
34
Greeting message sent
35
[received] Message in room GENERAL
Copied!

Talk to Your SDK Bot

On the server, login as a regular user (not the BOT user), go to general room, and talk to your newly created bot:
Last modified 13d ago
Export as PDF
Copy link
Edit on GitHub