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 How to develop a Bean Shell Hash Variable for more details steps.

ในบทช่วยสอนนี้เราจะทำตาม guideline for developing a plugin เพื่อพัฒนาปลั๊กอิน Slack Webhook Tool ของเรา โปรดอ้างอิงถึง How to develop a Bean Shell Hash Variable สำหรับขั้นตอนรายละเอียดเพิ่มเติม

1. What is the problem?

อะไรคือปัญหา?

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

เราต้องการส่งข้อความถึง Slack เมื่อบางสิ่งบางอย่างเสร็จสิ้นใน Joget Workflow

2. How to solve the problem?

วิธีแก้ปัญหา

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

เราจะพัฒนา Process Tool & Post Form Submission Processing Plugin การส่งแบบฟอร์มเพื่อส่งข้อความถึง 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.

ในการพัฒนาปลั๊กอิน Slack Webhook Tool เราจะพิจารณาให้พารามิเตอร์ที่มีอยู่ใน Slack Incoming Webhook เป็นอินพุตปลั๊กอินของเรา

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.

เมื่อเครื่องมือทำงานแล้วข้อความจะส่งไปยัง Slack ตามการกำหนดค่า

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

มีทรัพยากร / API ที่สามารถนำกลับมาใช้ใหม่ได้หรือไม่?

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

เราสามารถใช้ห้องสมุด slack-webhook เพื่อรวมเข้ากับ Slack

6. Prepare your development environment

เตรียมสภาพแวดล้อมการพัฒนาของคุณ

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

The following tutorial is prepared with a Macbook Pro and the Joget Source Code is version 8.0-Snapshot. Please refer to the Guideline for Developing a Plugin article for other platform commands.

Let's say our folder directory is as follows. 

เราจำเป็นต้องให้ซอร์สโค้ด Joget Workflow ของเราพร้อมและสร้างโดยปฏิบัติตาม this guideline

บทช่วยสอนต่อไปนี้จัดทำขึ้นด้วย Macbook Pro และรหัสแหล่งที่มาของ Joget เป็นรุ่น 8.0-Snapshot โปรดอ้างอิง Guideline for Developing a Plugin สำหรับคำสั่งแพลตฟอร์มอื่น ๆ

สมมติว่าไดเรกทอรีโฟลเดอร์ของเรามีดังนี้

- Home
  - joget
    - plugins
    - jw-community

The "plugins" directory is the folder we will create and store all our plugins and the "jw-community" directory is where the Joget Workflow Source code is stored.

Run the following command to create a maven project in "plugins" directory.

ไดเรกทอรี "ปลั๊กอิน" คือโฟลเดอร์ที่เราจะสร้างและจัดเก็บปลั๊กอินทั้งหมดของเราและไดเรกทอรี "jw-community" เป็นที่เก็บ Joget Workflow Source code เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างโครงการ maven ในไดเรกทอรี "ปลั๊กอิน"

cd joget/plugins/
~/joget/jw-community/wflow-plugin-archetype/create-plugin.sh org.joget slack_webhook 8.0-Snapshot

Then, the shell script will ask us to key in a version number for the plugin and ask us for a confirmation before it generates the maven project.

จากนั้นเชลล์สคริปต์จะขอให้เราใส่หมายเลขเวอร์ชันสำหรับปลั๊กอินและขอให้เรายืนยันก่อนที่จะสร้างโครงการ Maven

Define value for property 'version':  1.0-SNAPSHOT: : 8.0-Snapshot 
[INFO] Using property: package = org.joget
Confirm properties configuration:
groupId: org.joget
artifactId: slack_webhook
version: 5.0.0
package: org.joget
Y: : y

We should get a "BUILD SUCCESS" message shown in our terminal and a "slack_webhook" folder created in the "plugins" folder.

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

เราควรได้รับข้อความ "BUILD SUCCESS" ที่ปรากฏในเครื่องของเราและโฟลเดอร์ "slack_webhook" ที่สร้างขึ้นในโฟลเดอร์ "ปลั๊กอิน" เปิดโครงการ maven ด้วย IDE ที่คุณชื่นชอบ ฉันจะใช้ 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.

สร้างคลาส "SlackWebhookTool" ภายใต้แพ็คเกจ "org.joget" จากนั้นขยายคลาสด้วย org.joget.plugin.base.DefaultApplicationPlugin คลาสนามธรรม โปรดอ้างอิงProcess Tool & Post Form Submission Processing Plugin การประมวลผลการส่งแบบฟอร์มโพสต์ เราจะต้องใช้งานคลาสส่วนติดต่อ org.joget.plugin.base.PluginWebSupport เพื่อให้ปุ่มส่งข้อความทดสอบในหน้าคุณสมบัติปลั๊กอิน โปรดอ้างอิง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.

ตามปกติเราต้องใช้วิธีนามธรรมทั้งหมด เราจะใช้วิธี AppPluginUtil.getMessage เพื่อสนับสนุน i18n และใช้ตัวแปร MESSAGE_PATH คงที่สำหรับไดเรกทอรีทรัพยากรข้อความมัด

package org.joget;
 
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.plugin.base.DefaultApplicationPlugin;
import org.joget.plugin.base.PluginWebSupport;
 
public class SlackWebhookTool extends DefaultApplicationPlugin implements PluginWebSupport {
    private final static String MESSAGE_PATH = "message/SlackWebhookTool";
    
    @Override
    public String getName() {
        return "Slack Webhook Tool";
    }
    @Override
    public String getVersion() {
        return "5.0.0";
    }
    
    @Override
    public String getClassName() {
        return getClass().getName();
    }
    
    @Override
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.SlackWebhookTool.pluginLabel", getClassName(), MESSAGE_PATH);
    }
    @Override
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.SlackWebhookTool.pluginDesc", getClassName(), MESSAGE_PATH);
    }
    
    @Override
    public String getPropertyOptions() {
        return AppUtil.readPluginResource(getClass().getName(), "/properties/slackWebhookTool.json", null, true, MESSAGE_PATH);
    }
}

Now, we have to create a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our Plugin Properties Options definition file is located at "/properties/slackWebhookTool.json". Let us create a directory "resources/properties" under "slack_webhook/src/main" directory. After creating the directory, create a file named "slackWebhookTool.json" in the "properties" folder.

In the properties definition options file, we will need to provide options as below. Please note that we can use "@@message.key@@" syntax to support i18n in our properties options.

ตอนนี้เราต้องสร้าง UI สำหรับผู้ใช้ผู้ดูแลระบบเพื่อให้อินพุตสำหรับปลั๊กอินของเรา ในวิธีการ getPropertyOptions เราได้ระบุไฟล์ข้อกำหนด Plugin Properties Options ของเราแล้วที่ "/properties/slackWebhookTool.json" ให้เราสร้างไดเรกทอรี "resources / properties" ภายใต้ไดเรกทอรี "slack_webhook / src / main" หลังจากสร้างไดเรกทอรีให้สร้างไฟล์ชื่อ "slackWebhookTool.json" ในโฟลเดอร์ "properties" ในไฟล์ตัวเลือกคำจำกัดความของคุณสมบัติเราจะต้องระบุตัวเลือกดังต่อไปนี้ โปรดทราบว่าเราสามารถใช้ไวยากรณ์ "@@ message.key @@" เพื่อรองรับ i18n ในตัวเลือกคุณสมบัติของเรา

[{
    title : '@@SlackWebhookTool.config@@',
    properties : [{
        name : 'url',
        label : '@@SlackWebhookTool.url@@',
        type : 'textfield',
        required : 'true'
    },
    {
        label : '@@SlackWebhookTool.from@@',
        type : 'header'
    },
    {
        name : 'username',
        label : '@@SlackWebhookTool.fromUsername@@',
        type : 'textfield',
        value : '@@SlackWebhookTool.fromUsername.value@@'
    },
    {
        name : 'customIcon',
        label : '@@SlackWebhookTool.customIcon@@',
        type : 'selectbox',
        value : 'joget',
        options : [{
            value : '',
            label : '@@SlackWebhookTool.customIcon.none@@'
        },
        {
            value : 'joget',
            label : '@@SlackWebhookTool.customIcon.joget@@'
        },
        {
            value : 'url',
            label : '@@SlackWebhookTool.customIcon.url@@'
        },
        {
            value : 'emoji',
            label : '@@SlackWebhookTool.customIcon.emoji@@'
        }]
    },
    {
        name : 'iconUrl',
        label : '@@SlackWebhookTool.customIcon.url@@',
        type : 'textfield',
        required : 'true',
        control_field: 'customIcon',
        control_value: 'url',
        control_use_regex: 'false'
    },
    {
        name : 'iconEmoji',
        label : '@@SlackWebhookTool.customIcon.emoji@@',
        type : 'textfield',
        required : 'true',
        control_field: 'customIcon',
        control_value: 'emoji',
        control_use_regex: 'false'
    },
    {
        label : '@@SlackWebhookTool.to@@',
        type : 'header'
    },
    {
        name : 'channels',
        label : '@@SlackWebhookTool.channels@@',
        description : '@@SlackWebhookTool.channels.desc@@',
        type : 'textfield'
    },
    {
        name : 'participantIds',
        label : '@@SlackWebhookTool.participantIds@@',
        description : '@@SlackWebhookTool.participantIds.desc@@',
        type : 'textfield'
    },
    {
        name : 'usernames',
        label : '@@SlackWebhookTool.usernames@@',
        description : '@@SlackWebhookTool.usernames.desc@@',
        type : 'textfield'
    },
    {
        name : 'usernameTransform',
        label : '@@SlackWebhookTool.usernameTransform@@',
        description : '@@SlackWebhookTool.usernameTransform.desc@@',
        type : 'textfield',
        value : '@@SlackWebhookTool.usernameTransform.value@@',
        required : 'True'
    },
    {
        label : '@@SlackWebhookTool.message@@',
        type : 'header'
    },
    {
        name : 'text',
        label : '@@SlackWebhookTool.text@@',
        description : '@@SlackWebhookTool.text.desc@@',
        type : 'codeeditor',
        required : 'True'
    },
    {
        name : 'unfurl_links',
        label : '@@SlackWebhookTool.unfurl_links@@',
        description : '@@SlackWebhookTool.unfurl_links.desc@@',
        type : 'checkbox',
        value : 'true',
        options : [{
            value : 'true',
            label : ''
        }]
    },
    {
        name : 'unfurl_media',
        label : '@@SlackWebhookTool.unfurl_media@@',
        description : '@@SlackWebhookTool.unfurl_media.desc@@',
        type : 'checkbox',
        value : 'true',
        options : [{
            value : 'true',
            label : ''
        }]
    }],
    buttons : [{
        name : 'sendTestMessage',    
        label : '@@SlackWebhookTool.sendTestMessage@@',
        ajax_url : '[CONTEXT_PATH]/web/json/app[APP_PATH]/plugin/org.joget.SlackWebhookTool/service?action=sendTestMessage',
        fields : ['url'],
        addition_fields : [
            {
                name : 'testChannel',
                label : '@@SlackWebhookTool.sendTestMessage.testChannel@@',
                type : 'textfield'
            }
        ]
    }]
},
{
    title : '@@SlackWebhookTool.attachment@@',
    properties : [{
        name:'fallback',
        label:'@@SlackWebhookTool.attachment.fallback@@',
        description:'@@SlackWebhookTool.attachment.fallback.desc@@',
        type:'textfield',
        value: '@@SlackWebhookTool.attachment.fallback.value@@',
        required : 'True'
    },
    {
        name:'color',
        label:'@@SlackWebhookTool.attachment.color@@',
        description:'@@SlackWebhookTool.attachment.color.desc@@',
        type:'textfield'
    },
    {
        name:'pretext',
        label:'@@SlackWebhookTool.attachment.pretext@@',
        description:'@@SlackWebhookTool.attachment.pretext.desc@@',
        type:'textfield'
    },
    {
        name:'author_name',
        label:'@@SlackWebhookTool.attachment.author_name@@',
        description:'@@SlackWebhookTool.attachment.author_name.desc@@',
        type:'textfield'
    },
    {
        name:'author_link',
        label:'@@SlackWebhookTool.attachment.author_link@@',
        description:'@@SlackWebhookTool.attachment.author_link.desc@@',
        type:'textfield'
    },
    {
        name:'author_icon',
        label:'@@SlackWebhookTool.attachment.author_icon@@',
        description:'@@SlackWebhookTool.attachment.author_icon.desc@@',
        type:'textfield'
    },
    {
        name:'attachment_title',
        label:'@@SlackWebhookTool.attachment.title@@',
        description:'@@SlackWebhookTool.attachment.title.desc@@',
        type:'textfield'
    },
    {
        name:'attachment_title_link',
        label:'@@SlackWebhookTool.attachment.title_link@@',
        description:'@@SlackWebhookTool.attachment.title_link.desc@@',
        type:'textfield'
    },
    {
        name:'attachment_text',
        label:'@@SlackWebhookTool.attachment.text@@',
        description:'@@SlackWebhookTool.attachment.text.desc@@',
        type:'textfield'
    },
    {
        name:'fields',
        label:'@@SlackWebhookTool.attachment.fields@@',
        description : '@@SlackWebhookTool.attachment.fields.desc@@',
        type:'grid',
        columns:[{
            key:'title',
            label:'@@SlackWebhookTool.attachment.fields.title@@'
        },
        {
            key:'value',
            label:'@@SlackWebhookTool.attachment.fields.value@@'
        },
        {
            key:'short',
            label:'@@SlackWebhookTool.attachment.fields.short@@',
            options: [
            {
                value : 'false',
                label : '@@SlackWebhookTool.attachment.fields.short.no@@'
            },{
                value : 'true',
                label : '@@SlackWebhookTool.attachment.fields.short.yes@@'
            }]
        }]
    },
    {
        name:'image_url',
        label:'@@SlackWebhookTool.attachment.image_url@@',
        description:'@@SlackWebhookTool.attachment.image_url.desc@@',
        type:'textfield'
    },
    {
        name:'thumb_url',
        label:'@@SlackWebhookTool.attachment.thumb_url@@',
        description:'@@SlackWebhookTool.attachment.thumb_url.desc@@',
        type:'textfield'
    }]
}]

After completing the properties option to collect input, we can work on the main method of the plugin which is executed method.

หลังจากเสร็จสิ้นตัวเลือกคุณสมบัติเพื่อรวบรวมอินพุตเราสามารถทำงานกับวิธีการหลักของปลั๊กอินซึ่งเป็นวิธีการดำเนินการ

    private SlackApi api = null;
 
    @Override
    public Object execute(Map props) {
        final Set<String> channelList = new HashSet<String>();
        
        String channels = getPropertyString("channels");
        if (!channels.isEmpty()) {
            String[] temp = channels.split(";");
            for (String t : temp) {
                if (t.startsWith("#")) {
                    channelList.add(t);
                }
            }
        }
        
        WorkflowAssignment wfAssignment = (WorkflowAssignment) getProperty("workflowAssignment");
        
        String participantIds = getPropertyString("participantIds");
        if (wfAssignment != null && !participantIds.isEmpty()) {
            String[] temp = participantIds.split(";");
            if (temp.length > 0) {
                WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
                WorkflowProcess process = workflowManager.getProcess(wfAssignment.getProcessDefId());
                for (String pid : temp) {
                    pid = pid.trim();
                    if (!pid.isEmpty()) {
                        Collection<String> userList = WorkflowUtil.getAssignmentUsers(process.getPackageId(), wfAssignment.getProcessDefId(), wfAssignment.getProcessId(), wfAssignment.getProcessVersion(), wfAssignment.getActivityId(), "", pid);
                        if (userList != null && userList.size() > 0) {
                            for (String username : userList) {
                                channelList.add(getSlackUsername(username, wfAssignment));
                            }
                        }
                    }
                }
            }
        }
        
        String usernames = getPropertyString("usernames");
        if (!usernames.isEmpty()) {
            String[] temp = usernames.split(";");
            for (String username : temp) {
                channelList.add(getSlackUsername(username, wfAssignment));
            }
        }
        
        Thread sendMessageThread = new PluginThread(new Runnable() {
            public void run() {
                try {
                    LogUtil.info(SlackWebhookTool.class.getName(), "SlackWebhookTool: Sending message to=" + channelList.toString());
                    sendMessage(channelList);
                    LogUtil.info(SlackWebhookTool.class.getName(), "SlackWebhookTool: Sending message completed to=" + channelList.toString());
                } catch (Exception ex) {
                    LogUtil.error(SlackWebhookTool.class.getName(), ex, "");
                }
            }
        });
        
        sendMessageThread.setDaemon(true);
        sendMessageThread.start();
        
        return null;
    }
    
    protected SlackApi getApi() {
        if (api == null) {
            api = new SlackApi(getPropertyString("url"));
        }
        return api;
    }
    
    protected String getSlackUsername(String username, WorkflowAssignment assignment) {
        String syntax = getPropertyString("usernameTransform");
        syntax = syntax.replaceAll(StringUtil.escapeRegex("{username}"), StringUtil.escapeRegex(username));
        return AppUtil.processHashVariable(syntax, assignment, null, null);
    }
    
    protected void sendMessage(Set<String> channels) {
        SlackMessage message = createMessage();
        if (channels != null && !channels.isEmpty()) {
            for (String channel : channels) {
                if (!channel.isEmpty()) {
                    sendMessage(channel, message);
                }
            }
        } else {
            sendMessage(null, message);
        }
    }
    
    protected void sendMessage(String channel, SlackMessage message) {
        if (channel != null && !channel.isEmpty()) {
            message.setChannel(channel);
        }
        
        getApi().call(message);
    }
    
    protected SlackMessage createMessage() {
        SlackMessage message = new SlackMessage(getPropertyString("text"));
        
        String username = getPropertyString("username");
        if (!username.isEmpty()) {
            message.setUsername(username);
        }
        
        String customIcon = getPropertyString("customIcon");
        if (!customIcon.isEmpty()) {
            if ("joget".equals(customIcon)) {
                HttpServletRequest request = WorkflowUtil.getHttpServletRequest();
                if (request != null) {
                    String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/images/v3/logo.png";
                    message.setIcon(url);
                }
            } else if ("url".equals(customIcon)) {
                message.setIcon(getPropertyString("iconUrl"));
            } else {
                message.setIcon(getPropertyString("iconEmoji"));
            }
        }
        
        message.setUnfurlLinks("true".equalsIgnoreCase(getPropertyString("unfurl_links")));
        message.setUnfurlMedia("true".equalsIgnoreCase(getPropertyString("unfurl_media")));
        
        boolean hasAttachment = false;
        SlackAttachment atatchment = new SlackAttachment();
        
        String fallback = getPropertyString("fallback");
        if (!fallback.isEmpty()) {
            atatchment.setFallback(fallback);
        }
        
        String color = getPropertyString("color");
        if (!color.isEmpty()) {
            atatchment.setColor(color);
        }
        
        String pretext = getPropertyString("pretext");
        if (!pretext.isEmpty()) {
            atatchment.setPretext(pretext);
            hasAttachment = true;
        }
        
        String author_name = getPropertyString("author_name");
        if (!author_name.isEmpty()) {
            atatchment.setAuthorName(author_name);
        }
        
        String author_link = getPropertyString("author_link");
        if (!author_link.isEmpty()) {
            atatchment.setAuthorLink(author_link);
        }
        
        String author_icon = getPropertyString("author_icon");
        if (!author_icon.isEmpty()) {
            atatchment.setAuthorIcon(author_icon);
        }
        
        String attachment_title = getPropertyString("attachment_title");
        if (!attachment_title.isEmpty()) {
            atatchment.setTitle(attachment_title);
            hasAttachment = true;
        }
        
        String attachment_title_link = getPropertyString("attachment_title_link");
        if (!attachment_title_link.isEmpty()) {
            atatchment.setTitleLink(attachment_title_link);
        }
        
        String attachment_text = getPropertyString("attachment_text");
        if (!attachment_text.isEmpty()) {
            atatchment.setText(attachment_text);
            hasAttachment = true;
        }
        
        String image_url = getPropertyString("image_url");
        if (!image_url.isEmpty()) {
            atatchment.setImageUrl(image_url);
            hasAttachment = true;
        }
        
        String thumb_url = getPropertyString("thumb_url");
        if (!thumb_url.isEmpty()) {
            atatchment.setThumbUrl(thumb_url);
            hasAttachment = true;
        }
        
        Object[] fields = null;
        Object fieldsProperty = getProperty("fields");
        if (fieldsProperty != null && fieldsProperty instanceof Object[]){
            fields = (Object[]) fieldsProperty;
        }
 
        if (fields != null && fields.length > 0) {
            for (Object o : fields) {
                Map mapping = (HashMap) o;
                String title = mapping.get("title").toString();
                String value = mapping.get("value").toString();
                String isShort = mapping.get("short").toString();
                SlackField field = new SlackField();
                field.setTitle(title);
                field.setValue(value);
                field.setShorten("true".equalsIgnoreCase(isShort));
                
                atatchment.addFields(field);
                hasAttachment = true;
            }
        }
        
        if (hasAttachment) {
            message.addAttachments(atatchment);
        }
        
        return message;
    }

In our plugin properties, we have a button to send a test message. Let's implement the webService method to provide an API to send a test message.

ในคุณสมบัติปลั๊กอินของเราเรามีปุ่มสำหรับส่งข้อความทดสอบ ให้ใช้วิธีการ webService เพื่อจัดทำ API เพื่อส่งข้อความทดสอบ

    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.

เราจำเป็นต้องรวมไลบรารี "jsp-api" และ "slack-webhook" ไว้ในไฟล์ POM ของเรา

        <!-- 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

เตรียมปลั๊กอินสากลให้พร้อม (i18n)

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.

เรากำลังใช้คีย์ข้อความ i18n ในวิธี getLabel และ getDescription เราจะใช้คีย์ข้อความ i18n ในการกำหนดตัวเลือกคุณสมบัติของเราเช่นกัน จากนั้นเราจะต้องสร้างไฟล์คุณสมบัติสำหรับปลั๊กอินของเรา

Create a directory, "resources/message", under "slack_webhook/src/main" directory. Then, create a "SlackWebhookTool.properties" file in the folder. In the properties file, add all the message keys and its label as below.

สร้างไดเรกทอรี "resources / message" ภายใต้ไดเรกทอรี "slack_webhook / src / main" จากนั้นสร้างไฟล์ "SlackWebhookTool.properties" ในโฟลเดอร์ ในไฟล์คุณสมบัติให้เพิ่มคีย์ข้อความและป้ายกำกับทั้งหมดดังต่อไปนี้

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

ลงทะเบียนปลั๊กอินของคุณไปที่ 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.

ต่อไปเราจะต้องลงทะเบียนคลาสปลั๊กอินของเราในคลาส Activator (สร้างอัตโนมัติในแพ็คเกจคลาสเดียวกัน) เพื่อบอก Felix Framework ว่านี่เป็นปลั๊กอิน

    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.

Then, let's upload the plugin jar to Manage Plugins. After uploading the jar file, double-check that the plugin is uploaded and activated correctly.

มาสร้างปลั๊กอินของเรากัน เมื่อกระบวนการสร้างเสร็จสิ้นเราจะพบไฟล์ "slack_webhook-5.0.0.jar" ที่สร้างขึ้นภายใต้ไดเรกทอรี "slack_webhook / target"

จากนั้นลองอัปโหลดปลั๊กอินไปที่ Manage Plugins หลังจากอัปโหลดไฟล์ jar ให้ตรวจสอบอีกครั้งว่ามีการอัปโหลดและเปิดใช้งานปลั๊กอินอย่างถูกต้อง


Check the Slack Webhook Tool is available in process tool mapping.

ตรวจสอบว่า Slack Webhook Tool พร้อมใช้งานในการแมปเครื่องมือกระบวนการ


Now, let us configure the Incoming Webhooks in Slack platform.

ตอนนี้ให้เรากำหนดค่าเว็บรับเข้าในแพลตฟอร์ม Slack

  1. Go to your_team.slack.com/services/new.

    ไปที่ your_team.slack.com/services/new
  2. Search for Incoming WebHook and click in Add

    ค้นหา WebHook ที่เข้ามาและคลิกที่ Add

  3. Choose Channel to Post and press Add Incoming WebHooks Integration

    เลือกช่องทางที่จะโพสต์และกดเพิ่มการผนวก WebHooks

  4. Into Setup Instructions, you have a WebHook URL. This is the argument will use for the "Webhook URL" later. Then, copy it.

    ในคำแนะนำในการตั้งค่าคุณคือ URL ของ WebHook นี่คืออาร์กิวเมนต์ที่จะใช้สำหรับ "Webhook URL" ในภายหลัง จากนั้นคัดลอก

Configure the Slack Webhook Tool.

กำหนดค่า Slack Webhook Tool


The message is received in Slack.

ได้รับข้อความใน Slack


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

ก้าวไปอีกขั้นแบ่งปันหรือขายมัน

You can download the source code from slack_webhook_src.zip.

To download the ready-to-use plugin jar, please find Slack Webhook at http://marketplace.joget.org/

คุณสามารถดาวน์โหลดซอร์สโค้ดจาก slack_webhook_src.zip

หากต้องการดาวน์โหลด jar ปลั๊กอินที่พร้อมใช้งานโปรดค้นหาได้ที่ http://marketplace.joget.org/ (เร็ว ๆ นี้)