Send Slack messages with Talend: part 1
By Richard Hall
At Talend we make a point of using our software wherever we can. It makes sense to use our own products: every different persona that uses our products in the real world is represented inside our organization. Having these personas using the products internally enables us to really understand where the champagne elements of the product shine and where any dog food elements can be made a little more sparkly.
We needed to alert Talend technical staff of questions on Talend Community that had not been answered. However, the requirement was that this be made as simple as possible, as efficient as possible, and as tailorable as possible. We use Salesforce to host the Talend Community and we can send notifications out from the product. But we were limited as to how these notifications could be sent, what topics the notifications would be sent about, and how we would be able to monitor these notifications. We needed a way of allowing our technical experts to pick certain categories of questions to be notified about, to be able to send notifications to groups or individuals, and for these notifications to NOT go to email where they might be missed. Slack was overwhelmingly suggested as the delivery mechanism that people said they would like us to use.
The following article is part 1 of a three part mini-series, where I will focus on configuring a Slack app.
Configuring your Slack app
Before you can start using Slack with Talend, you have to configure Slack so that you can use it. This involves setting up an app, configuring security, requesting permissions, and so on. If you are the admin of Slack at your organization this won’t be too tiresome. For the rest of us, raise a ticket with your IT team now to get their attention, then by the last blog in this series, you may be ready to start.
The following step by step guide will take you through the configuration steps I used to build the Slack App to notify Talend employees of unanswered Talend Community questions. The App is called TalendCommunityMessenger.
1. Within the Slack desktop application, click Add apps - in the bottom left corner of the screen. It will reveal the Apps screen. In the top right of the Apps screen you will see the App Directory link. Click on that to load the Slack App Directory webpage (https://talend.slack.com/apps)
2. Click Build to take you to the Slack API page.
3. Now click Your Apps to get to the page where you can create your app.
4. To create a new app, click Create New App.
5. Fill in the App Name, and select your Development Slack Workspace. Click Create App.
6. This will take you to the section to configure your app. Click Add features and functionality.
7. The section expands to reveal the following screen. Click Bots to reveal the App Home screen.
8. In order to activate your bot and grant it some permissions, click Review Scopes to Add.
You will then be presented with the OAuth & Permissions screen.
9. The OAuth & Permissions screen is a long page with several points of interest. The image below shows the top of the page. Pay attention to the greyed out Request to Install. You will need to click this when you have selected your scopes in order to start working with your implementation. This is where you will need your Slack admin’s assistance.
Scrolling further down takes you to the Scopes section. Here is where you select your scopes for the bot. The scopes grant permissions for the bot to have access to different functionalities. Click Add an OAuth Scope to grant permissions.
10. The permissions that have been granted for the TalendCommunityMessenger bot are below.
- channels:read Allows the app to get information about public channels in order to communicate with them. In this case it is used to retrieve the channelId that is needed for the bot to instant message the channels.
- groups:read Allows the app to get information about private channels that the bot is a member of. It is used to retrieve the channelId of private channels, which is needed for the bot to instant message the private channels.
- users:read Allows the app to get information about users. This is needed to retrieve a channelId for each user being messaged.
- chat:write Permits the bot to send messages as @talendcommunitymessen.chat:write.public Allows the bot to send messages to channels.files:writeRequired to allow the bot to send file data.im:writeAllows the bot to initiate a direct message.
- links:writeAllows the bot’s links to be previewed in Slack.
All of the above, apart from the links:write scope, are required in order to let your bot send messages to users, public channels, and private channels.
11. After you have selected the scopes, you need to scroll up to find the Request to Install button in the OAuth Tokens & Redirect URLs section. Click that and wait for your Slack admin to authorize your app and its scope selections.
12. When your app and its scope selections have been authorized, copy the Bot User OAuth Token and keep this secret. This is used to authorize your API usage for your bot.
Testing and understanding what your OAuth token allows you to do
Once you have your OAuth token, you are able to make use of it to test the API methods linked to each of the scopes. You can test posting messages using the info on Slack API's chat.postMessage page if you have a token and have selected the chat:write scope. There is a Tester tab that allows you to make calls by specifying parameters.
You can also test out building Blocks, which are a Slack way of formatting content. The Block Kit Builder is really useful for trying out different message formats.
At this point, you are free to work on your app without any other app configuration unless you want to add further scopes. If that is the case, you can add scopes as shown in steps 9 and 10, but you will have to get them authorized each time, as in step 11.
In order to secure the OAuth token that has been created for this app, I have used the Jasypt library. You don’t have to do this, but it makes sense to ensure that your OAuth token is safe. The Jasypt library is supplied with the Talend Studio install but can also be found on the Jasypt: Java Simplified Encryption site.
Below you can see the code that has been put together using this library. This is used to both encrypt and decrypt the OAuth token.
I thought that this code might be useful to you and it is good practice to not store your tokens in clear text. However, in this tutorial I will not focus on the security side in too much detail since you will have your own way of doing that. I will stick with the main elements of how to get Talend sending Slack messages, but may occasionally nod to other areas like this.
You can now experiment with the Slack API and you also have some time to consider how you might want to use Slack with your Talend integrations.
In part 2, I will talk about using the Slack API to retrieve essential data from Slack before you can start sending messages to people and groups.