- Print
- DarkLight
- PDF
Develop a QuickMeet.Chat SDK Bot
- Print
- DarkLight
- PDF
This bot is a simple example of how you can use QuickMeet.Chat JS SDK methods directly. It is extremely basic and does not handle errors, different message types, server resets, and other production situations.
QuickMeet.Chat Bot is a Node.js package that works with Node and NPM. To develop the bot, follow the steps below.
Install QuickMeet.Chat SDK package
Open the terminal in the directory where you want to develop your bot and run the following command.
npm init -y
npm install @quickmeet.chat/sdkCreate SDK bot files
To proceed with the simplest setup, you need to create two files: the first one is responsible for the bot's working logic, and the other maps the responses.
Create a
server.jsfile with the following content:
const { driver } = require('@quickmeet.chat/sdk');
const respmap = require('./reply');
// Environment setup (consider using environment variables)
const HOST = '<ROCKETCHAT HOST>';
const USER = '<BOT USER NAME>';
const PASS = '<BOT USER PASS>';
const BOTNAME = '<ROCKET CHAT BOT ALIAS>';
const SSL = '<SSL USAGE>';
const ROOMS = ['<ROCKETCHAT CHANNEL>'];
var myUserId;
// Bot configuration
const runbot = async () => {
try {
const conn = await driver.connect({ host: HOST, useSsl: SSL });
myUserId = await driver.login({ username: USER, password: PASS });
const roomsJoined = await driver.joinRooms(ROOMS);
console.log('joined rooms');
const subscribed = await driver.subscribeToMessages();
console.log('subscribed');
const msgloop = await driver.reactToMessages(processMessages);
console.log('connected and waiting for messages');
const sent = await driver.sendToRoom(BOTNAME + ' is listening ...', ROOMS[0]);
console.log('Greeting message sent');
} catch (error) {
console.error("Error:", error);
}
};
// Process messages
const processMessages = async (err, message, messageOptions) => {
if (!err) {
if (message.u._id === myUserId) return;
const roomname = await driver.getRoomName(message.rid);
console.log('got message ' + message.msg);
var response;
if (message.msg in respmap) {
response = respmap[message.msg];
} else {
response = message.u.username + ', how can I' + ' help you with "' + message.msg + '"';
}
try {
const sentmsg = await driver.sendToRoomId(response, message.rid);
} catch (error) {
console.error("Error sending message:", error);
}
}
};
runbot()Create a
.envfile in your project root folder to add the server and user credentials.Make sure
<BOT USER NAME>has aBOTrole on the server. For more information on how to create a bot user please refer to this page.
Create a
reply.jsfile with the following content:
const respmap = {
"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.jsAfter executing the last command, QuickMeet.Chat SDK bot tries to connect to the QuickMeet.Chat instance and listen to messages in the general room:
$ node server.js
[connect] Connecting { username: 'username',
password: 'pass',
ldap: false,
host: 'quickmeet.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":"QuickMeet.Chat BOT is listening ...","bot":{"i":"js.SDK"},"rid":"GENERAL"}]
[sendMessage] Success: {"msg":"QuickMeet.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 GENERALTalk 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:
.png?sv=2022-11-02&spr=https&st=2024-12-22T17%3A50%3A47Z&se=2024-12-22T18%3A00%3A47Z&sr=c&sp=r&sig=7YLcYW68rq7VOlFqfItQmrTxLK9a1W4CEqg9zQ5OKiI%3D)