Mule meets Slack
The Mule Slack component is another interesting piece of software which you can find as a part of the MuleSoft Anypoint Platform.
Slack is a chat and messenger application which can be used as an alternative to email. One huge advantage of Slack compared to email is the lean and responsive environment for collaboration. Furthermore, Slack is hosted as a cloud service so there is no infrastructure necessary. That’s why many startups rely on Slack for communication.
The Slack component can be used as an inbound and outbound component, which means, that a Mule flow can listen for Slack events and also perform several operations towards Slack. Therefore, there are many use cases the component can be used in. One example is related to the term Multi-Channel enablement, which describes a systematic and integrative approach to read from and post data to different communication channels. The Slack connector could be used to filter and aggregate data from Twitter, Facebook and news sites to report those topic based to different Slack channels. So subscribers of these channels can be informed topic wise by Slack messages instead of following the sources directly. This reduces mails and results in a centralized information platform where only interested people are notified. Moreover, people can discuss these imported news and posts, posts can be discussed internally on Slack and even a task list could be created if a post is configured to represents a task.
On the other hand slack can also be used as a data source itself.
In the following, we describe a Mule application, which provides a REST interface to accept messages which are forwarded to Slack, which is basically done with 3 different flows:
The flow mulemeetsslack-main provides our HTTP based REST interface, where the HTTP traffic is handled and our small API is published.
The incoming requests are handled by the RAML based API description which configures the APIkit Router component. As soon as a POST message with a messageText is received, the request is forwarded to the post:mulemeetsslack-config flow by the APIkit Router. There, the incoming payload is stored as a variable, a log entry is created and then the message is posted to Slack via the Slack component.
The flow mulemeetsslack-console provides a HTTP based documentation and test console, we will later use for testing.
Let us have a look at the Slack component. The last element of the post:mulemeetsslack-config flow described by “Post message on Slack anonymously” is using the component. The component is provided by the Anypoint platform and can be added to the flow via drag and drop. (If you try this and the component is not available on your system, you can install it either via Mule Exchange)
Clicking on the component opens the property panel, where we can configure the component. First of all, the Slack component needs a configuration containing a token, so that Slack knows which customer the message belongs to.
For authentication OAUTH2 or a token could be used, we chose to generate a token, which you can do via https://api.slack.com/docs/oauth-test-tokens is you have an active Slack account and are logged in.
Afterwards we can choose from the operations, which action we would like to perform. As we want to post to an existing channel, we select Chat-Post message. Then we have to provide the channel id, the message we want to post and a name, which is as a kind of author.
When Mule Anypoint Studio connects to Slack you can choose the appropriate channel from a Drop-down-List.
This results in the following XML representation of the flow.
<slack:post-message config-ref="Slack__Token_Configuration" channelId="C2VFCB5L1" username="MuleBot" message="#[flowVars.messageText]" doc:name="Post message on Slack anonymously"/>
That’s it! That is all which needed to be done to configure the adapter.
For testing purposes we use the APIkit Router Console to send a POST request to our interface. The console is rendered by the Mule Anypoint Platform and is another component which is contained in our third flow mulemeetslack-console. The console also uses the RAML specification to generate the user interface and even to add examples.
As we specified such an example and have, so far, only one interface, we simple need to click on the post button to send a message, which directly results in a new entry in Slack.
There are plenty other connectors provided by Mulesoft’s Anypoint Platform which can be found on the Anypoint Exchange. The usage of those components is usually straightforward and easy. They are usually provided with working examples you just have to adapt to your purpose. As soon as the platform concepts are understood, it takes only a couple of minutes to realize such scenarios.