Chat Service
Usage
A callback for an incoming message on a channel can be defined using the registerCallback
function, which takes a channel id and a callback function as parameters.
reactToMessage
provides the functionality to send a message to a given channel. With MSTeams, this is possible only in the course of a callback function registered with registerCallback
. reactToMessage
takes one parameter of the type GenericContext
, this is an object with the attributes text
and channel
or teamsContext
in the case of a MSTeamsService.
Following, a short example on how to use the services:
import { ChatService } from './ChatService';
import { RocketChatService } from './RocketChatService';
const chatService: ChatService = new RocketChatService();
function bot(message: string): void {
switch (message.toLowerCase()) {
case 'hello':
chatService.reactToMessage({
text: 'Hi!',
channel: 'GENERAL',
});
chatService.reactToMessage({
text: 'Are you fine?',
channel: 'GENERAL',
});
break;
case 'yes':
chatService.reactToMessage({
text: "That's great!",
channel: 'GENERAL',
});
break;
case 'no':
chatService.reactToMessage({
text: 'Oh no!',
channel: 'GENERAL',
});
break;
default:
break;
}
}
chatService.registerCallback('GENERAL', bot);
For differences between MSTeams, Slack and RocketChat, see their respecting docs: