Send Slack Messages with Talend: part 3
By Richard Hall
In part 1 of this series, you looked at preparing Slack to allow a developer to use its APIs, and in part 2, you gathered the data that is needed to send messages using the Slack API. In this final part, you will create a Job that allows you to send messages to users and channels in Slack.
You will need to identify which posts should be sent to your intended users, and this may require you to adjust the source data components to suit the data you wish to send messages about. Here is a high level overview of the process Talend uses, which may be different from your use case.
There are two parts of the process:
1. The first involves collecting the details of users and channels that want to be notified, and what Community boards and/or topics (known as labels) they want to be informed about. This information is kept in a Snowflake database. Each user/channel is linked to the boards/topics they want to be notified about, and is also linked to a "live" status that is a Boolean. This allows us to switch notifications off for the user/channel temporarily, without removing the data.
2. The second part involves collecting all of the Community post and comment data from Salesforce and storing it in Snowflake every 30 minutes. We store the data outside of Salesforce because it is faster to query the data in Snowflake; we are not limited to CPU cycles or how we can join the data, meaning that the SQL we use is simpler and significantly faster than if we tried to query directly in Salesforce. Our Our Slack_Data table (discussed in part 2) and user/channel configuration data are also stored in Snowflake.
Slack message format
There are two things you need to do to send Slack messages using Talend: build a Job, and decide how you want your messages to look within Slack. To make the latter as easy as possible, use Slack's Block Kit Builder. It allows you to build the look and feel you want, then gives you the code to create that look and feel in Slack.
As an example, here is the look and feel I put together for the Community unanswered post notifications. It is very basic and has just a title, date, and a series of links to posts that currently need answers.
You can achieve the required look and feel by dragging and dropping fields from the left side bar onto the display screen, which are converted to code on the right. You can then modify that code (if you choose) to tweak what you see in the display screen. I identified how the code could be dynamically built using Java String manipulation, then built the Job to accommodate this. I will show this code when I describe building the Job.
Another useful tool to help with Java String manipulation is Freeformatter.com. This helps you to "escape" all of the characters in the JSON code that do not work well with Java String manipulation. Since the Block Kit Builder produces JSON, you will find this tool very useful if you want to preserve formatting (such as quotes, new lines, tabs).
Job to send Slack messages
This Job can be run on its own, or as a child Job being called by a parent Job. In the case of Talend Community, our parent Job identifies users and channels that have posts to be informed about. The user/channel name and ID are passed to the child Job, then that Job sends out the messages associated with that user/channel. Below is a screenshot of the child Job that sends out the messages.
As in part 2, I will follow the numbering in explaining what each component does, except for the tLogRow components that are just used for logging to the System.out. These can be disabled by right-clicking the component and selecting Deactivate.
This Job requires some context variables to be able to function, shown below. These variables are used to pass values into this Job when it is called as a child Job, but they can also be used to run the Job without a parent Job.
- The first is- SlackOAuthAccessToken Set this to the token you created in part 1
- Next is Username, the name of the Slack user or Slack channel the message needs to be sent to in this run of the Job.
- This isn’t as important for sending the messages (the ChannelId is important for that), but I have used it to search for relevant posts for our user/channel to be informed about. It’s a key for the data to be sent. You may find a better key to find the content you want to send to the user/channel.
- Last is ChannelId, the ID that the message will be sent to. If this is incorrect or not provided, the Job will fail.
Once these context variables are set, you can start building the Job.
Running the Job
This Job is intended to be run by a parent Job that will call it for every user/channel that needs to be sent notifications, but it can also be run on its own if the required context variables are supplied. Since the data you are using as your source for the Slack messages will be different to the data in this example, how you run this is entirely up to you. In this case, I will run the child Job on its own to demonstrate the output.
To do this, I have set the following context variables in the Context tab of the Job:
Once those are set, I simply select the Run tab and click Run. The output I get in my Slack app is shown below:
This marks the end of this series. I hope you got everything you hoped for from it. If you have any questions about this example, please contact me at firstname.lastname@example.org. I am very interested in what you achieve with this information.