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 the terminal in the directory you wish to develop your bot in and run the following command
npm init -y
npm install @rocket.chat/sdk
Create SDK Bot Files
To proceed with the simplest setup, you need to create two files: the first one is responsible for the working logic of the bot, and the other is to map the responses.
create a server.js file with the following content:
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:
constrespmap= {"hi":"hey","u da bot":"no, YOU da bot","no u da bot":"Come'on - YOU DA BOT!!","I give up":"ok. silly human :rolleyes:"};module.exports= respmap;
Run the SDK Bot
node server.js
After executing the last command, Rocket.Chat SDK bot tries to connect to the Rocket.Chat instance and listen to messages in the general room:
$ node server.js
[connect] Connecting { username: 'username',
password: 'pass',
ldap: false,
host: 'rocket.chat.host',
useSsl: true,
timeout: 20000,
rooms: [],
allPublic: false,
dm: false,
livechat: false,
edited: false,
integrationId: 'js.SDK',
roomCacheMaxSize: 10,
roomCacheMaxAge: 300000,
dmCacheMaxSize: 10,
dmCacheMaxAge: 100000 }
[connect] Connected
[login] Logging in botUser
[getRoomIdByNameOrId] Calling (caching): general
[getRoomIdByNameOrId] Success: "GENERAL"
[joinRoom] Calling (async): ["GENERAL"]
[joinRoom] Success
joined rooms
[subscribe] Preparing subscription: stream-room-messages: __my_messages__
[subscribe] Stream ready: 4
subscribed
[reactive] Listening for change events in collection stream-room-messages
connected and waiting for messages
[getRoomIdByNameOrId] Calling (cached): general
[getRoomIdByNameOrId] Success: "GENERAL"
[sendMessage] Calling (async): [{"msg":"Rocket.Chat BOT is listening ...","bot":{"i":"js.SDK"},"rid":"GENERAL"}]
[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"}
Greeting message sent
[received] Message in room GENERAL
Talk to Your SDK Bot
On the server, log in as a regular user (not the BOT user), go to general room, and talk to your newly created bot:
Last updated
Rocket.Chat versions receive support for six months after release.