Now that we have learned about the event interfaces, let's go through some examples to implement them. On this page, we will follow three examples:
Messaging governance
Notify messages
Content inspection
Messaging governance
In this example, we will use the IPreMessageSentPrevent interface to control whether a particular message should be sent to a channel. We are using our Hello World app to test this.
Open your app folder in Visual Studio and select the main app file, in this case, HelloWorldApp.ts.
To the HelloWorldApp class, add the IPreMessageSentPrevent interface as follows:
As soon as we add the indication of implements IPreMessageSentPrevent we see that Visual Studio displays an error that it cannot find the interface and shows the option to quickly fix it in a tooltip. Click it to display different import options that would fix the issue. Import the following interface:
Now Visual Studio will display an error that a certain method part of the interface has its implementation missing with the option to fix it quickly. Accept the suggested fix. Visual Studio adds two methods of the interface that are not implemented. The methods will throw an error when executed. But now, we have a skeleton code to work with:
checkPreMessageSentPrevent?(message:IMessage, read:IRead, http:IHttp):Promise<boolean> { throw new Error('Method not implemented.');}executePreMessageSentPrevent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence): Promise<boolean> {
throw newError('Method not implemented.');}
With the checkPreMessageSentPrevent method, we will only check the messages from channels other than general. Visual Studio helps you navigate through the APIs of the classes of the objects by suggesting the available methods. For this example, you can see that the method variable message has a room attribute which has a slugifiedName string attribute. Update the method as follows:
Now Visual Studio displays an error that the method signature is not compatible with returning a Promise<boolean>. To fix this issue, add async to the method signature.
Next, implement the executePreMessageSentPrevent method. If the message equals "test", we prevent it from being published.
Your main app file should look something like this:
If we send the ‘test’ message in a channel other than general, it should not be published (it will appear grayed out). If the message is something different it will get sent. As for the room general, all messages will be sent including ‘test’ as shown in the following screenshots:
Notify messages
In this example, we will notify the general channel whenever a message is sent anywhere else. Here we will implement the IPostMessageSent interface.
Like the previous example, in the main app file (in this case, HelloWorldApp.ts), we will add the check and execute methods from the IPostMessageSent interface as shown in the code below.
The check method confirms that the general channel exists and fetches it.
In the execute method, for the general channel, we create a message containing information about the message published, the room, and the sender by getting the values from sendMessage. The messageBuilder allows us to define richly formatted messages.
Now save, deploy, and test the app. You should be able to see the warning messages posted to the general channel when writing to any other channel.
Content inspection
A typical use case of the previous two examples is to control the content of the information being exchanged. For instance, we could use regular expressions matching inappropriate words to flag them.
Save the file, deploy, and test the app. The notification is like a temporary private message visible only to the user who sent the attachment (if you refresh the page, the notification is gone).
Great! With these examples, you have learned how to implement event interfaces and react to certain events. You have made significant progress in expanding your app!
In the upcoming sections, we will look at another way to extend your app's capabilities by creating interactive user experiences with the Apps-Engine UIKit.
Last updated
Rocket.Chat versions receive support for six months after release.