Sending Messages to Slack Workspace Members via Slack API

Sending Messages to Slack Workspace Members via Slack API

 

1 Overview

SQLMessenger version 2.1 and above allows you to send messages (such as text messages, images, files, etc.) to your Slack workspace members via the Slack API. This article will guide you on how to configure a Slack API Sender in SQLMessenger.

Configuration process for Slack App in SQLMessenger

2 Create Slack App

Before configuring the Slack API Sender, you need to create a Slack App in your Slack workspace. If you have already created a Slack App, please skip this step.

Step 1-1: Log in to the Slack App management page (https://api.slack.com/apps) as workspace administrator, then click the "Create New App" button.

Step 1-2: Click the "From scratch" option.

Step 1-3: Enter a name for the new App, and select the workspace where you want to create the App, then click the "Create App" button.

Step 1-4: Click the OAuth & Permissions menu, then scroll down to the Scopes label. Click "Add an OAuth Scope" and add the necessary permissions for both Bot Token and User Token.

To ensure SQLMessenger can function properly with the Slack App, the following permissions need to be added to the App:

channels:read View basic information about public channels in a workspace
channels:write Manage a user’s public channels and create new ones on a user’s behalf
chat:write Send messages on a user’s behalf
files:write Upload, edit, and delete files on a user’s behalf
groups:read View basic information about a user’s private channels
groups:write Manage a user’s private channels and create new ones on a user’s behalf
im:read View basic information about a user’s direct messages
im:write Start direct messages with people on a user’s behalf
mpim:read View basic information about a user’s group direct messages
mpim:write Start group direct messages with people on a user’s behalf
users:read View people in a workspace
users:read.email View email addresses of people in a workspace

Step 1-5: After adding the permissions, scroll up the page to find the "OAuth Tokens" label and click the "Install to XXX" button (XXX is your workspace name).

Step 1-6: Click the "Allow" button on the page to complete the app installation.

Note: If you are not the workspace administrator, you will need to ask the workspace administrator to complete the app installation.

3 Configuring Slack Apps in SQLMessenger

After creating a Slack App, you need to configure the Slack App's connection information in SQLMessenger.

Step 2-1: Click the "Slack Apps Config" icon in the MainMenu of SQLMessenger.

Step 2-2: Click the "Add App to Workspace" button, and select the "Create a new workspace and add Apps" option.

Step 2-3: Enter the name of the workspace and click "OK".

Step 2-4: After the workspace saves the information, the system will prompt whether to add the app to the workspace immediately. Click "Yes".

Step 2-5: Select the App Type to add, and enter the App name and the OAuth Token for the App.

Tips: The App Name must be unique in SQLMessenger.

Note: You can find the OAuth Token by clicking on the OAuth & Permissions menu in the Slack app management page, then finding the OAuth Tokens label to view your "User OAuth Token" and "Bot User OAuth Token".

Find "User OAuth Token" and "Bot User OAuth Token"

Step 2-6: After entering the OAuth Token, you can click "Test" to send a test message to your Slack workspace using this app.

Tips: You can enter a Channel Name or a member's ID in the "Recipient Member ID for Test Message" text box, and the system will send the test message to the specified channel or member.

Step 2-7: After entering the application information, click "OK" to save the app config. Then, when prompted to import the members and channels list to the local database, click "Yes".

Tips: SQLMessenger will import your workspace members and channels into a local PostgreSQL database to facilitate selecting recipients when configuring tasks.

4 Send Messages and Files to Slack Members

To send a message or file to Slack workspace members, simply select Slack workspace members as recipients under the "Send Via Slack" tab when configuring the task.

Send messages to workspace members via the Slack API

Note: If you choose Slack workspace members as recipients under the "Send Via Email" tab, the system will send the message to the members' email inboxes.

Tips: When the system sends email messages via the Slack API, the email body is converted into a text message. You can also choose to convert the email body into a PNG image or PDF file and send it to Slack workspace members by selecting this option in the body template settings.

Convert the email body into a PNG image or PDF file when sending it to Slack workspace members

5 Use Variables to Dynamically Determine Message Recipients

In SQLMessenger, you can use variables to dynamically determine which recipients to send messages to during task execution. For example, you can retrieve a recipient list from a database, an Excel spreadsheet, or a CSV file and send messages to those recipients. Below, we'll demonstrate how to dynamically set message recipients using an example.

Step 1: Create a variable in the task to retrieve the Slack member IDs of recipients from a table during task execution.

Tips: The variable value can be a Member ID, a Channel Name, or an email address of a member. To send messages to multiple members, you can separate the Member IDs with commas (e.g., "U015NGUUGXX,U07FF9K8AXX,project"). If the variable value is retrieved from a database, Excel spreadsheet, or CSV file, each row can return a Member ID or email address.

Step 2: When selecting recipients for the task, click "From a Variable" on the right side of the dialog box under the Send Via Slack tab, then choose the variable and the Slack app to use for sending messages.

About the "Use the Variable Value as" option:

Member's ID The system treats the value of the variable as either a Slack workspace member's Member ID or a Channel Name.
Member's email address

The system treats the value of the variable as a Slack workspace member's email address. In this case, the system will look up the corresponding Member ID in the workspace member list that has been imported into the local database.

Tips: If you choose this option, after new members join the workspace or member information changes, you need to import the updated member list into the local database in SQLMessenger. Otherwise, SQLMessenger will be unable to send messages to these members because it cannot find the corresponding Member IDs for the email addresses.

Using Variables in Tasks, Using Information Distribution Task

6 Import Slack Workspace Member and Channel List into Local Database

If you use variable Slack recipients and select the "Use the variable value as member's email address" option, you should promptly import the updated workspace member list into SQLMessenger's local database after changes occur to workspace members or Channels.

Step 1: Click the Slack Apps Config icon in the Main Menu of SQLMessenger.

Step 2: Click the workspace name where you want to import the member list, then click the Apps tab. Select an application (note: do not click on a Webhook-type application). In the application's "Members & Channels" tab, click the "Import Members & Channels List" button.

Tips: You only need to read from one application per workspace; there's no need to read from each application.

After successfully reading the workspace member list, it is saved in the local PostgreSQL database (which SQLMessenger uses). You can access this list through the view "user01.v_slack_workspace_members" in the default data source of SQLMessenger. Below is a description of the fields in this view:

workspace_id int The internal ID of the workspace, automatically assigned by SQLMessenger
workspace_name text The name of the wrokspace
member_id text The Member ID of the workspace member, generated by the Slack platform
member_name text The name of the member
mail_addr text The email address of the member. Channels do not have this attribute.
real_name text The real name of the member.
display_name text The display name of the member as shown in the Slack interface.
deleted char(1) The status of the member. If it is the string "1," it indicates that the member has been deactivated by an administrator.
slack_member_data json The profile data of the member on the Slack platform. For specific data formats and meanings, please refer to the relevant Slack API documentation.

7 FAQ

Q: Where can I find the Slack workspace member's ID?

A: You can copy the Member ID to the system clipboard on the member's profile panel.

Step 1: In the Slack interface, click the profile picture of the member, and then click the menu button on the profile panel.

Step 2: Click the "Copy member ID" menu item to copy the member's Member ID to the system clipboard.

Q: How to distribute messages point-to-point to multiple Slack workspace members?

A: You can use SQLMessenger's "Information Distribute" feature to accomplish this task. For example, you can create a table like the one below:

The table to be distributed includes the recipient's Member ID and Message.

Step1: Create a new task in SQLMessenger, select the "Information Distribute" option, and import the table into SQLMessenger.

Step2: In the recipient selection dialog, under the Send Via Slack tab, click the "Add From a Variable" button on the right, then click "Choose Variable".

Step3: In the variable selection dialog, under the "System Variables/Functions" tab, click the button next to the "@@LoopData()" entry, and select "Member ID" as the Function Parameter.

Step4: After clicking OK, return to the task configuration screen. In the body template editor, right-click and then click the "Insert Variable" menu item.

Step5: In the variable selection dialog, under the "System Variables/Functions" tab, find the "@@LoopData()" entry and click the button on the right, then select "Member" as the Function Parameter.

Step 6: Click "OK" to return to the task configuration screen, then click "Deploy" to apply the new task configuration immediately.

The task needs to be configured only once and can be reused later. When you need to send a message, right-click on the task and select the "Run selected tasks immediately" menu item to start the task.

You can also use this method to distribute images, files, reports, and other content point-to-point to workspace members. Additionally, SQLMessenger can automatically read report data from the database and handle the distribution.

Using Variables in Tasks, Using Information Distribution Task

Last updated on January 15, 2025