Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this tutorial, we will follow the guideline for developing a plugin to develop our Slack Webhook Tool plugin. Please also refer to the very first tutorial วิธีการพัฒนา Bean Shell Hash Variable for more details steps.

1. What is the problem?

We want to send message to Slack when something is completed in Joget Workflow.

2. How to solve the problem?

We will develop a Process Tool/ Post Form Submission Processing Plugin to send message to Slack

3. What is the input needed for your plugin?

To develop a Slack Webhook Tool plugin, we will consider providing the parameters available in Slack Incoming Webhook as our plugin input.

4. What is the output and expected outcome of your plugin?

When the tool is execute, a message will send to Slack based on the configuration.

5. Are there any resources/API that can be reused?

We can use slack-webhook library to integrate with Slack.

6. Prepare your development environment

We need to always have our Joget Workflow Source Code ready and builded by following this guideline

...

Open the maven project with your favourite IDE. I will be using NetBeans.  

7. Just code it!

a. Extending the abstract class of a plugin type

Create a "SlackWebhookTool" class under "org.joget" package. Then, extend the class with org.joget.plugin.base.DefaultApplicationPlugin abstract class. Please refer to Process Tool/ Post Form Submission Processing Plugin. We will need to implement org.joget.plugin.base.PluginWebSupport interface class as well to provide a send test message button in plugin properties page. Please refer to Web Service Plugin.

b. Implement all the abstract methods

As usual, we have to implement all the abstract methods. We will using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.

...

Code Block
languagexml
    public void webService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean isAdmin = WorkflowUtil.isCurrentUserInRole(WorkflowUserManager.ROLE_ADMIN);
        if (!isAdmin) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        
        String action = request.getParameter("action");
        if ("sendTestMessage".equals(action)) {
            String message = "";
            try {
                AppDefinition appDef = AppUtil.getCurrentAppDefinition();
                
                String url = AppUtil.processHashVariable(request.getParameter("url"), null, null, null, appDef);
                String testChannel = AppUtil.processHashVariable(request.getParameter("testChannel"), null, null, null, appDef);
                
                setProperty("url", url);
                setProperty("text", AppPluginUtil.getMessage("SlackWebhookTool.testMessage", getClassName(), MESSAGE_PATH));
                
                if (testChannel != null && !testChannel.isEmpty()) {
                    Set<String> channels = new HashSet<String>();
                    channels.add(testChannel);
                    sendMessage(channels);
                } else {
                    sendMessage(null);
                }
                
                message = AppPluginUtil.getMessage("SlackWebhookTool.sendTestMessage.success", getClassName(), MESSAGE_PATH);
            } catch (Exception e) {
                LogUtil.error(this.getClassName(), e, "Fail to send Test Message to Slack");
                message = AppPluginUtil.getMessage("SlackWebhookTool.sendTestMessage.fail", getClassName(), MESSAGE_PATH) + "\n" + StringEscapeUtils.escapeJavaScript(e.getMessage());
            }
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.accumulate("message", message);
                jsonObject.write(response.getWriter());
            } catch (Exception e) {
                //ignore
            }
        } else {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        }
    }

c. Manage the dependency libraries of your plugin

We need to include "jsp-api" and "slack-webhook" libraries in our POM file.

Code Block
        <!-- Change plugin specific dependencies here -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>net.gpedro.integrations.slack</groupId>
            <artifactId>slack-webhook</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- End change plugin specific dependencies here -->

d. Make your plugin internationalization (i18n) ready

We are using i18n message key in getLabel and getDescription method. We will use i18n message key in our properties options definition as well. Then, we will need to create a message resource bundle properties file for our plugin.

...

Code Block
org.joget.SlackWebhookTool.pluginLabel=Slack Webhook Tool
org.joget.SlackWebhookTool.pluginDesc=Used to send message to Slack through incoming webhook
SlackWebhookTool.config=Configure Slack Webhook Tool
SlackWebhookTool.url=Webhook URL
SlackWebhookTool.from=From
SlackWebhookTool.fromUsername=Username
SlackWebhookTool.fromUsername.value=Joget Workflow
SlackWebhookTool.customIcon=Custom Icon
SlackWebhookTool.customIcon.none=None
SlackWebhookTool.customIcon.joget=Joget Workflow Logo
SlackWebhookTool.customIcon.url=Image URL
SlackWebhookTool.customIcon.emoji=Emoji Code
SlackWebhookTool.to=To
SlackWebhookTool.channels=Channels
SlackWebhookTool.channels.desc=Separated by semicolon(;). E.g. #general;#info
SlackWebhookTool.participantIds=Participant Ids
SlackWebhookTool.participantIds.desc=Separated by semicolon(;). E.g. applicant;approver
SlackWebhookTool.usernames=Usernames
SlackWebhookTool.usernames.desc=Separated by semicolon(;). E.g. admin;cat
SlackWebhookTool.usernameTransform=Transform username to Slack username
SlackWebhookTool.usernameTransform.desc=Hash Variable can be used to transform username to Slack username. Eg. @#form.slack.username[{username}]#
SlackWebhookTool.usernameTransform.value=@{username}
SlackWebhookTool.message=Message
SlackWebhookTool.text=Text
SlackWebhookTool.text.desc=Refer to <a href="https://api.slack.com/docs/formatting" target="_blank">Slack Message Formatting</a>.
SlackWebhookTool.unfurl_links=Unfurling Links
SlackWebhookTool.unfurl_links.desc=Automatically find URLs in a message and create attachments based on the content of those URLs
SlackWebhookTool.unfurl_media=Unfurling Media
SlackWebhookTool.unfurl_media.desc=Automatically find Media URLs in a message and create attachments based on the media of those URLs
SlackWebhookTool.attachment=Attachment
SlackWebhookTool.attachment.fallback=Fallback
SlackWebhookTool.attachment.fallback.desc=A plain-text summary of the attachment. This text will be used in clients that don't show formatted text (eg. IRC, mobile notifications) and should not contain any markup.
SlackWebhookTool.attachment.fallback.value=Attachment fail to display.
SlackWebhookTool.attachment.color=Color
SlackWebhookTool.attachment.color.desc=An optional value that can either be one of good, warning, danger, or any hex color code (eg. #439FE0). This value is used to color the border along the left side of the message attachment.
SlackWebhookTool.attachment.pretext=Pretext
SlackWebhookTool.attachment.pretext.desc=This is optional text that appears above the message attachment block.
SlackWebhookTool.attachment.author_name=Author Name
SlackWebhookTool.attachment.author_name.desc=Small text used to display the author's name.
SlackWebhookTool.attachment.author_link=Author Link
SlackWebhookTool.attachment.author_link.desc=A valid URL that will hyperlink the Author Name text mentioned above. Will only work if Author Name is present.
SlackWebhookTool.attachment.author_icon=Author Icon
SlackWebhookTool.attachment.author_icon.desc=A valid URL that displays a small 16x16px image to the left of the Author Name text. Will only work if Author Name is present.
SlackWebhookTool.attachment.title=Attachment Title
SlackWebhookTool.attachment.title.desc=The Attachment Title is displayed as larger, bold text near the top of a message attachment.
SlackWebhookTool.attachment.title_link=Attachment Title Link
SlackWebhookTool.attachment.title_link.desc=By passing a valid URL in the Attachment Title Link parameter (optional), the Attachment Title text will be hyperlinked.
SlackWebhookTool.attachment.text=Text
SlackWebhookTool.attachment.text.desc=This is the main text in a message attachment, and can contain standard message markup. The content will automatically collapse if it contains 700+ characters or 5+ linebreaks, and will display a "Show more..." link to expand the content.
SlackWebhookTool.attachment.fields=Fields
SlackWebhookTool.attachment.fields.desc=Fields will be displayed in a table inside the message attachment.
SlackWebhookTool.attachment.fields.title=Title
SlackWebhookTool.attachment.fields.value=Value
SlackWebhookTool.attachment.fields.short=Short
SlackWebhookTool.attachment.fields.short.no=No
SlackWebhookTool.attachment.fields.short.yes=Yes
SlackWebhookTool.attachment.image_url=Image URL
SlackWebhookTool.attachment.image_url.desc=A valid URL to an image file that will be displayed inside a message attachment. We currently support the following formats: GIF, JPEG, PNG, and BMP. Large images will be resized to a maximum width of 400px or a maximum height of 500px, while still maintaining the original aspect ratio.
SlackWebhookTool.attachment.thumb_url=Thumbnail URL
SlackWebhookTool.attachment.thumb_url.desc=A valid URL to an image file that will be displayed as a thumbnail on the right side of a message attachment. We currently support the following formats: GIF, JPEG, PNG, and BMP. The thumbnail's longest dimension will be scaled down to 75px while maintaining the aspect ratio of the image. The filesize of the image must also be less than 500 KB.
SlackWebhookTool.sendTestMessage=Send Test Message
SlackWebhookTool.sendTestMessage.testChannel=Test Channel
SlackWebhookTool.sendTestMessage.success=Test message sent.
SlackWebhookTool.sendTestMessage.fail=Fail to sent test message. Error:
SlackWebhookTool.testMessage=Test Message

e. Register your plugin to the Felix Framework

Next, we will have to register our plugin class in the Activator class (Auto generated in the same class package) to tell the Felix Framework that this is a plugin.

Code Block
languagejava
    public void start(BundleContext context) {
        registrationList = new ArrayList<ServiceRegistration>();
        //Register plugin here
        registrationList.add(context.registerService(SlackWebhookTool.class.getName(), new SlackWebhookTool(), null));
    }

f. Build it and test

Let's build our plugin. Once the building process is done, we will find a "slack_webhook-5.0.0.jar" file created under "slack_webhook/target" directory.

...

The message is received in Slack.

8. Take a step further, share it or sell it

You can download the source code from slack_webhook_src.zip.

...