Send Slack messages with Talend: part 2
By Richard Hall
In the part 1 of this series, you looked at preparing Slack to allow a developer to use its APIs. There wasn’t much Talend development involved (none, apart from a routine that you might find useful for encryption/decryption), but hopefully those of you who are familiar with Talend API Services or ESB products have already jumped ahead to try out the Slack APIs. The next part of the process is a Job to collect user and channel data from the Slack API.
Retrieving key Slack information to send messages
Now that you have the Slack token you acquired in the first blog, you can’t just jump into sending messages. You have some other work to do first.
Unfortunately, Slack will not allow you to simply send messages by username or channel name. You need to find the channel ID for channels, and you need to open (or resume) a conversation for direct messages to individuals. This process will pass you a channel ID for your direct message conversation.
To retrieve this information and to open conversations, I have put together a small Talend Job that runs before I want to send messages. Once a channel ID has been retrieved or a conversation has been initiated, the channel ID will not change, so this Job does not have to be run every time you want to send messages. It needs to run once at the beginning of the project, then every time users or channels are added, so I run it once a day to ensure that we are not missing any data when we want to send messages.
This Job also requires a context variable to be configured. For this version, I have set the context variable in the Contexts tab as shown below:
The SlackOAuthAccessToken context variable value is the token you created in the previous blog. In this example, I am adding it in the Contexts tab, but it should probably be stored elsewhere in your Job in an encrypted state, then decrypted when it’s used in your Job. In the previous blog I also gave an example routine to handle the encryption.
Once you have the context variable for this Job sorted, you are ready to move on to building the Job. Since there are a lot of components in this Job, I am not going to go into great detail on all of them. Some will need more detail than others and some (like the tLogRow components) need no explanation.
Each component I discuss will be linked to its number in the screenshot. Some components do exactly the same thing in different places, the descriptions of those may be written in the same place.
After you have created this Job to collect Slack data, you have all the information you need to start sending Slack messages to users and channels.
Running the Job
This Job is required to gather the user and channel ID data to allow you to send messages to the entities. It doesn't need to be run for every message sent, but must be run when new users and/or channels are created. Therefore, you should run this Job once per day. This Job can be run in Talend Studio or on Talend Cloud. All you need is to set the SlackOAuthAccessToken context variable, then run the Job. When the Job is finished running, your target data repository will be populated with all of your user and channel data. This example uses a Snowflake table for the data repository, but you can use whatever you prefer.