Contact Us 1-800-596-4880

Slack Connector 1.0 Examples - Mule 4

The following examples show how to create a public Slack channel and how to post a message to a channel.

These examples use variables for some field values. You can either:

  • Replace the variables with their values in the code.

  • Provide the values for each variable in a properties file and then refer to that file from the connector configuration.

If you don’t know how to use a properties file, see Configuring Property Placeholders.

Before you try the examples, access Anypoint Studio (Studio) and verify that the Mule Palette view displays entries for Slack Connector. If not, follow the instructions in Add the Connector to Your Mule Project.

Create a Channel

This example shows how to create a public Slack channel with a user-specified name.

The following screenshot shows the app flow for this example:

App flow that shows the components used in the Create a Channel example
Figure 1. App flow for the Create a Channel example

Implementing this example involves creating a new Mule project and configuring HTTP Listener, the Create Channel operation, two Transform Message components, and one Logger component.

Before You Begin

This example requires the channels:write scope in both the connection configuration and Slack application.

Configure HTTP Listener

Configure HTTP Listener to initiate a Mule flow when a call is made to the /create-channel path on localhost port 8081:

  1. Create a new Mule project in Studio.

  2. In Studio, click HTTP and drag the Listener operation to the canvas.

  3. In the Listener properties window, click + next to the Connector configuration field to add a global element.

  4. Accept the defaults.

  5. In the Listener properties window, set the Path field value to /create-channel.

Add the First Transform Message Component

The initial Transform Message component takes the channel name as input and then converts the name to JSON format:

  1. In the Mule Palette view, select Core and drag the Transform Message component to the right of Listener.

  2. Change the name of the Transform Message component to Channel Details.

  3. Click the Transform Message component, add the channel name to the output, and set the output format to application/json:

Transform Message component that specifies the channel name in JSON format
Figure 2. First Transform Message component for the Create a Channel example

Add the Create Channel Operation

The Create Channel operation connects with Slack and posts a request to the https://slack.com/api/conversations.create endpoint:

  1. From the Mule Palette view, select Slack and drag the Create Channel operation to the right of Listener.

  2. In the Search properties window, click + next to the Connector configuration field to add a global element.

  3. Enter the following values:

    Field Value

    Consumer Key

    ${consumer.key}

    Consumer Secret

    ${consumer.secret}

    Authorization url

    https://slack.com/oauth/authorize

    Access token url

    https://slack.com/api/oauth.access

    Scopes

    channels:write groups:write im:write mpim:write users:read.email

    Listener config

    HTTP_Listener-config

  4. In the Slack properties window, enter payload in the Conversationcreate content field.

Add the Second Transform Message Component

The second Transform Message component converts the result of the Create Channel operation to JSON format:

  1. In the Mule Palette view, select Core and drag the Transform Message component to the right of Listener.

  2. Change the name of the Transform Message component to Payload as JSON.

  3. Click the Transform Message component and set the output to application/json:

    Transform Message component that converts the output of the Create Channel operation to JSON format
    Figure 3. Second Transform Message Component for the Create Channel example

Add the Logger Component

The Logger component logs the query result to the console:

  1. In the Mule Palette view, select Core and then select the Logger component.

  2. Configure the Logger component.

  3. Save the project.

Run the App

To run the app:

  1. Start the Mule app.

  2. To send a request, you must perform the OAuth dance. Open your browser and go to http://localhost:8082/authorize to enable access to the app.

  3. Send a POST request to http://localhost:8081/account?name="your channel name".

XML Code for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:slack="http://www.mulesoft.org/schema/mule/slack"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/slack http://www.mulesoft.org/schema/mule/slack/current/mule-slack.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<slack:config name="Slack_Connector_Config" doc:name="Slack Connector Config"  >
		<slack:slack-auth-connection >
			<slack:oauth-authorization-code consumerKey="${consumer.key}" consumerSecret="${consumer.secret}" scopes="channels:write groups:write im:write mpim:write users:read.email "/>
			<slack:oauth-callback-config listenerConfig="HTTP_Listener_config" callbackPath="/callback" authorizePath="/authorize" externalCallbackUrl="http://localhost:8081/callback"/>
		</slack:slack-auth-connection>
	</slack:config>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<configuration-properties doc:name="Configuration properties" file="application.properties" />
	<flow name="create-channel-flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/create-channel"/>
		<ee:transform doc:name="Channel Details" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	name: "your-channel-name"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<slack:create-conversationscreate doc:name="Create Channel" config-ref="Slack_Connector_Config"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Result" message="#[payload]"/>
	</flow>
  <flow name="send-message-to-channel-flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/send-message"/>
		<ee:transform doc:name="Message Details" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	channel: "your-existing-channel",
	text: "Your text goes here"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<slack:create-chatpost-message doc:name="Send Message" config-ref="Slack_Connector_Config"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Result" message="#[payload]"/>
	</flow>
</mule>

Post a Message to a Channel

This example shows how to post a message to a public, private, or direct message (IM) channel.

The following screenshot shows the app flow for this example:

App flow that shows the components used in the Post a Message to a Channel example
Figure 4. App flow for the Post a Message to a Channel example

Before You Begin

This example requires the following scopes in both the connection configuration and Slack application:

  • chat:write

  • chat:write:user

  • chat:write:bot

Components

This example uses the following components:

  • HTTP Listener

    Initiates a Mule flow when a call is made to the /send/message path on localhost port 8081

  • First Transform Message component

    Specifies the target channel and provides the message content for the Send Message operation

  • Send Message operation

    Connects with Slack and posts the message to the specified channel

  • Second Transform Message component

    Outputs the results of the Send Message operation in JSON format

  • Logger

    Logs the result of the Send Message operation to the console

XML Code for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:slack="http://www.mulesoft.org/schema/mule/slack"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/slack http://www.mulesoft.org/schema/mule/slack/current/mule-slack.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<slack:config name="Slack_Connector_Config" doc:name="Slack Connector Config" >
		<slack:slack-auth-connection >
			<slack:oauth-authorization-code consumerKey="${consumer.key}" consumerSecret="${consumer.secret}" scopes="channels:write groups:write im:write mpim:write users:read.email "/>
			<slack:oauth-callback-config listenerConfig="HTTP_Listener_config" callbackPath="/callback" authorizePath="/authorize" externalCallbackUrl="http://localhost:8081/callback"/>
		</slack:slack-auth-connection>
	</slack:config>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<configuration-properties doc:name="Configuration properties" file="application.properties" />
	<flow name="find-user-by-email-flow"  >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="find-by-email"/>
		<slack:get-userslookup-by-email doc:name="Find User by Email" config-ref="Slack_Connector_Config" email="example@emailaddress.com"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Lookup Result" message="#[payload]"/>
	</flow>
  <flow name="send-message-to-channel-flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/send-message"/>
		<ee:transform doc:name="Message Details"  >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	channel: "your-existing-channel",
	text: "Your text goes here"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<slack:create-chatpost-message doc:name="Send Message" config-ref="Slack_Connector_Config"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Result" message="#[payload]"/>
	</flow>
</mule>
View on GitHub