Notification Service
Notification Service
Overview
NotificationService interface provides access to Notification actions such as sending messages, attachments and templates via channel or just template evaluation.
Interface is located in eu.ibagroup.easyrpa.engine.service package.
Channel
Channels are used to determine the recipients of the notification and delivery type.
There are two channel types are supported by current version - Email and Slack. Their config is stored in CS configuration section under notification.channels.config key.
Email channel configuration
All config parameters are shown above. For example Gmail configuration is described here
The "outputContentType" parameter allows to select the message output format between text and html. By default, the type is determined automatically.
Setup slack channel configuration
- Create Slack App and add necessary Bot OAuth Scopes such as chat:write. Slack app configuration link - https://api.slack.com/apps
- Go to CS Configuration → notification.channels.config and add new config in format - "<config_name>" : {"token" : "<Bot User OAuth Token>"}
- Create Slack channel and add your App in it. Copy Channel ID as it will be used further (ID starts from xoxb-...).
- Create notification. Use your config_name as Config Name and Channel ID as recipient.
Invite Application Bot into channel you are going to use as recipient. In slack channel send the following
/invite @AppName
Template
Templates could be used if it is needed to insert some data into message dynamically. Control server support two template engines:
- Thymeleaf
Currently implemented ThymeleafHtml and ThymeleafText handlers. The key difference between textual template mode and the markup one is that in a textual template there are no tags into which to insert logic in the form of attributes, so we have to rely on other mechanisms. Read more about template modes here. Also useful documentation about Thymeleaf dialect is here. FreeMarker
Apache FreeMarker is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language
Getting started manual is here.
All parameters are passed to the template engine as a Map<String, ?> params.
There is an example of FreeMarker template for Debtors sample.
Usage
- Create Channel in Notification Management section. Config Name should be one of notification.channels.config keys.
- Create Template in Notification Management section. It could be validated by one of the entities - AutomationProcess, DocumentType, Node, Schedule.
Inject NotificationService in ApTask as well as notification channel and template names.
ApTask configuration@Inject private NotificationService notificationService; @Configuration(value = "notificationChannel", defaultValue = "Default Notification Channel") private String notificationChannel; @Configuration(value = "notificationTemplate", defaultValue = "Default Email Template") private String notificationTemplate;
Use one of the provided interface methods:
NotificationService.java/** * Evaluates template with provided parameters. * @param templateName template name * @param params params for template * @return the array of byte for processed template */ byte[] evaluateTemplate(String templateName, Map<String, ?> params); /** * Evaluates template with provided parameters and sends result into specified channel. * @param templateName template name * @param params params for template * @param channelName name of channel for sending * @param attachments list of attachments for channel if needed */ void evaluateTemplateAndSend(String templateName, Map<String, ?> params, String channelName, List<File> attachments); /** * Sends notification into specified channel. * @param channelName name of channel for sending * @param text body text to send * @param attachments list of attachments for channel if needed */ void sendNotification(String channelName, String text, List<File> attachments);
Examples
Evaluating and sending template in the Debtors sample.
Sending notification with attachment in the SAP sample.