|
| 1 | +--- |
| 2 | +sidebar_label: Overview |
| 3 | +--- |
| 4 | + |
| 5 | +# Sending data using a Slack API method |
| 6 | + |
| 7 | +A bot token or user token or [token of some other kind](https://api.slack.com/concepts/token-types) must be used to call one of [the Slack API methods](https://api.slack.com/methods) with this technique. |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +Different [Slack API methods](https://api.slack.com/methods) require different [scopes](https://api.slack.com/scopes), but setup should be similar for all methods: |
| 12 | + |
| 13 | +1. [Create a Slack app](https://api.slack.com/apps/new) for your workspace or use an existing app. |
| 14 | +2. Depending on the Slack API [method](https://api.slack.com/methods) you wish to call, add the required **scopes** to your app under the **OAuth & Permissions** page on [app settings](https://api.slack.com/apps). |
| 15 | +3. Install the app to your workspace using the **Install App** page. |
| 16 | +4. Once your app is installed to a workspace, a new [token](https://api.slack.com/concepts/token-types) with your app's specified scopes will be minted for that workspace. It is worth noting that tokens are only valid for a single workspace! Find the token on the **OAuth & Permissions** page. |
| 17 | +5. Add the token as [a repository secret](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository) called `SLACK_BOT_TOKEN` or something similar and memorable. |
| 18 | +6. [Add this Action as a step](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idsteps) to your GitHub workflow and provide an input payload to send to the method. |
| 19 | + |
| 20 | +Methods that require an app configuration token should gather this token from the [app configuration token](https://api.slack.com/reference/manifests#config-tokens) settings instead of from a specific app since this token is associated with the workspace. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +Choosing inputs for these steps is left as an exercise for the actioneer since each of the Slack API methods requires certain values and specific parameters, but these snippets might be helpful when starting. |
| 25 | + |
| 26 | +### Posting a message with text |
| 27 | + |
| 28 | +Posting a message with the [`chat.postMessage`](https://api.slack.com/methods/chat.postMessage) method can be achieved by adding this step to a job in your GitHub workflow and inviting the bot associated with your app to the channel for posting: |
| 29 | + |
| 30 | +```yaml |
| 31 | +- name: Post text to a Slack channel |
| 32 | + uses: slackapi/slack-github-action@v2.0.0 |
| 33 | + with: |
| 34 | + method: chat.postMessage |
| 35 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 36 | + payload: | |
| 37 | + channel: ${{ secrets.SLACK_CHANNEL_ID }} |
| 38 | + text: "howdy <@channel>!" |
| 39 | +``` |
| 40 | +
|
| 41 | +### Posting a message with blocks |
| 42 | +
|
| 43 | +More complex message layouts, such as messages made with [Block Kit](https://api.slack.com/surfaces/messages#complex_layouts) blocks, can also be sent with one of the Slack API methods: |
| 44 | +
|
| 45 | +```yaml |
| 46 | +- name: Post blocks to a Slack channel |
| 47 | + uses: slackapi/slack-github-action@v2.0.0 |
| 48 | + with: |
| 49 | + method: chat.postMessage |
| 50 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 51 | + payload: | |
| 52 | + channel: ${{ secrets.SLACK_CHANNEL_ID }} |
| 53 | + text: "GitHub Action build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" |
| 54 | + blocks: |
| 55 | + - type: "section" |
| 56 | + text: |
| 57 | + type: "mrkdwn" |
| 58 | + text: "GitHub Action build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" |
| 59 | +``` |
| 60 | +
|
| 61 | +### Updating a message |
| 62 | +
|
| 63 | +Updating a message after it's posted can be done with the [`chat.update`](https://api.slack.com/methods/chat.update) method and chaining multiple steps together using outputs from past steps as inputs to current ones: |
| 64 | + |
| 65 | +```yaml |
| 66 | +- name: Initiate the deployment launch sequence |
| 67 | + id: launch_sequence |
| 68 | + uses: slackapi/slack-github-action@v2.0.0 |
| 69 | + with: |
| 70 | + method: chat.postMessage |
| 71 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 72 | + payload: | |
| 73 | + channel: ${{ secrets.SLACK_CHANNEL_ID }} |
| 74 | + text: "Deployment started :eyes:" |
| 75 | + attachments: |
| 76 | + - color: "dbab09" |
| 77 | + fields: |
| 78 | + - title: "Status" |
| 79 | + short: true |
| 80 | + value: "In Progress" |
| 81 | +- name: Countdown until launch |
| 82 | + run: sleep 10 |
| 83 | +- name: Update the original message with success |
| 84 | + uses: slackapi/slack-github-action@v2.0.0 |
| 85 | + with: |
| 86 | + method: chat.update |
| 87 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 88 | + payload: | |
| 89 | + channel: ${{ secrets.SLACK_CHANNEL_ID }} |
| 90 | + ts: "${{ steps.launch_sequence.outputs.ts }}" |
| 91 | + text: "Deployment finished! :rocket:" |
| 92 | + attachments: |
| 93 | + - color: "28a745" |
| 94 | + fields: |
| 95 | + - title: "Status" |
| 96 | + short: true |
| 97 | + value: "Completed" |
| 98 | +``` |
| 99 | + |
| 100 | +### Replying to a message |
| 101 | + |
| 102 | +Posting [threaded replies to a message](https://api.slack.com/messaging/sending#threading) from a past job can be done by including the `thread_ts` attribute of the parent message in the `payload`: |
| 103 | + |
| 104 | +```yaml |
| 105 | +- name: Initiate a deployment |
| 106 | + uses: slackapi/slack-github-action@v2.0.0 |
| 107 | + id: deployment_message |
| 108 | + with: |
| 109 | + method: chat.postMessage |
| 110 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 111 | + payload: | |
| 112 | + channel: ${{ secrets.SLACK_CHANNEL_ID }} |
| 113 | + text: "Deployment started :eyes:" |
| 114 | +- name: Conclude the deployment |
| 115 | + uses: slackapi/slack-github-action@v2.0.0 |
| 116 | + with: |
| 117 | + method: chat.postMessage |
| 118 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 119 | + payload: | |
| 120 | + channel: ${{ secrets.SLACK_CHANNEL_ID }} |
| 121 | + thread_ts: "${{ steps.deployment_message.outputs.ts }}" |
| 122 | + text: "Deployment finished! :rocket:" |
| 123 | +``` |
| 124 | + |
| 125 | +### Uploading a file |
| 126 | + |
| 127 | +Calling [a Slack API method](https://api.slack.com/methods) with [`@slack/web-api`](https://tools.slack.dev/node-slack-sdk/web-api) makes [uploading a file](https://api.slack.com/messaging/files#upload) just another API call with all of the convenience of the [`files.uploadV2`](https://tools.slack.dev/node-slack-sdk/web-api/#upload-a-file) method: |
| 128 | + |
| 129 | +```yaml |
| 130 | +- name: Share a file to that channel |
| 131 | + uses: slackapi/slack-github-action@v2.0.0 |
| 132 | + with: |
| 133 | + method: files.uploadV2 |
| 134 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 135 | + payload: | |
| 136 | + channel_id: ${{ secrets.SLACK_CHANNEL_ID }} |
| 137 | + initial_comment: "the results are in!" |
| 138 | + file: "./path/to/results.out" |
| 139 | + filename: "results-${{ github.sha }}.out" |
| 140 | +``` |
| 141 | + |
| 142 | +## Example workflows |
| 143 | + |
| 144 | +* [**Direct message the author**](/slack-github-action/sending-techniques/sending-data-slack-api-method/direct-message-author): Write to the Slack user with a matching email. |
| 145 | +* [**Invite a usergroup to channel**](/slack-github-action/sending-techniques/sending-data-slack-api-method/invite-usergroup-to-channel): Create a channel and invite members. |
0 commit comments