How to extend Brooklyn with new chat protocols

Do you like Brooklyn but you use a chat protocol which is not officially supported?
All you have to do is following this tutorial step by step!

First of all, you have to check if there is a Java library for the protocol whereby you want to create the bot.
If it doesn't exist, you've to write it by yourself (as I did to support Rocket.Chat). Then, add the library you have just created to Maven.
Finally import it on Brooklyn through Maven.

Secondly, you have to create a *nameOfTheProtocol*Bot class (e.g. IrcBot) inside the bots package.
This class have to implement the Bot interface:
public final class IrcBot implements Bot {
   ...
}
Then you have to implement these methods:
  • boolean init(String botId, Map configs, String[] channels): here you have to init your bot. botId is the name retrieved from the config file, configs contains all the bot configs and channels is the list of channels in which the bot have to be. It returns true if everything is ok, false otherwise;
  • void addBridge(Bot bot, String channelTo, String channelFrom): it has to connect the channel channelTo of the bot bot to its channel channelFrom so every message received from channelFrom have to be sent to the channel channelTo of bot;
  • Optional<String> sendMessage(BotTextMessage msg, String channelTo): when the bot receives a message from another bot, it has to send it to the right channel. In this case it has to send a text message msg to the channelTo channel. Usually this function returns a message id related to the message, but there are protocols which don't support ids like IRC. In this particular scenario this function simply have to return Optional.empty();
  • Optional<String> sendMessage(BotDocumentMessage msg, String channelTo): same as the previous function, except that it has to send an attachment and not only plain text;
  • void editMessage(BotTextMessage msg, String channelTo, String messageId): it has to edit the message sent on channelTo which has an id messageId. The new message can be retrieved from msg;
  • List getUsers(String channel): it has to return a list of usernames that are inside channel;
  • String getId(): it has to return the botId of the bot in which it is called;
There are protocols in which channel ids are different from the channel names (e.g. telegram).
In this case the bot must have to override String getChannelName(String channelId).
NB: you don't always have to reinvent the wheel.
There is a beautiful class named BotsController which help you formatting messages and routing them correctly.
You should look at other bot classes to learn how to implement these methods correctly.

Finally, when you've done everything you just have to rebuild the application and insert a new bot of type *nameOfTheProtocol*Bot in the config file.

Commenti

Post popolari in questo blog

Brooklyn 0.1 is out there: full Telegram and IRC support

Stop writing code like we're in the '90s: a practical approach (PART I)