diff --git a/knowledge base/slack.md b/knowledge base/slack.md index ce82149..5dbdb1b 100644 --- a/knowledge base/slack.md +++ b/knowledge base/slack.md @@ -3,8 +3,9 @@ 1. [TL;DR](#tldr) 1. [Add custom emoji](#add-custom-emoji) 1. [Give aliases to existing emojis](#give-aliases-to-existing-emojis) -1. [Apps](#apps) -1. [Incoming webhooks](#incoming-webhooks) +1. [Use incoming webhooks for notifications](#use-incoming-webhooks-for-notifications) +1. [Use bots for notifications](#use-bots-for-notifications) +1. [Create apps](#create-apps) 1. [Further readings](#further-readings) 1. [Sources](#sources) @@ -18,16 +19,38 @@ brew install --cask 'slack' mas install '803453959' ``` +CLI: + +```sh +brew install --cask 'slack-cli' \ +&& slack login +``` + +Authorization data will be saved to `~/.slack/credentials.json`. +
Usage ```sh -# Send notifications to channels -curl -X 'POST' -H 'Content-type: application/json' \ - --data '{"text": "Hello, World!"}' \ - 'https://hooks.slack.com/services/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' +# Login. +slack login + +# List authorized accounts. +slack auth list +slack list +``` + +```sh +# Send notifications to channels using the APIs. +curl --request 'POST' --url 'https://slack.com/api/chat.postMessage?pretty=1' \ + --header 'Authorization: Bearer xoxb-012345678901-0123456789012-abcdefghijklmnopqrstuvwx' \ + --header 'Content-type: application/json' --data '{"channel": "C04K1234567", "text": "Hello, World!"}' + +# Send notifications to channels using incoming webhooks. +curl --request 'POST' --url 'https://hooks.slack.com/services/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' \ + --header 'Content-type: application/json' --data '{"text": "Hello, World!"}' ```
@@ -59,38 +82,62 @@ Check out [slackmojis] for some common reactions. 1. Choose the image. 1. Give it an alias. -## Apps - -Direct links: - -- [Create a new App](https://api.slack.com/apps?new_app=1). - -## Incoming webhooks +## Use incoming webhooks for notifications Refer [Sending messages using incoming webhooks] and [Setting Up Slack Webhook URL Simplified 101]. 1. Enable Incoming Webhooks for a Slack app.
_Features_ → _Incoming Webhooks_ → _Activate Incoming Webhooks_. 1. Create a new Webhook URL and authorize it (or request authorization for it). -1. Install the app in the workspace. -1. Send a test request to the webhook. +1. Install (or request an higher entity to install) the app in the workspace.
+ App details → _Settings_ → _Install App_ → _OAuth Tokens_.
+1. Send a test request to the webhook: ```sh - curl -X 'POST' -H 'Content-type: application/json' \ - 'https://hooks.slack.com/services/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' --data '{"text": "Hello, World!"}' + curl --request 'POST' --url 'https://hooks.slack.com/services/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' \ + --header 'Content-type: application/json' --data '{"text": "Hello, World!"}' ``` +## Use bots for notifications + +Refer [Automating Slack Notifications: Sending Messages as a Bot with Python]. + +1. [Create a new Slack App][create apps]. +1. Select _From Scratch_. +1. Enter a name (e.g., `test bot`) and select a workspace for it. +1. Add the required OAuth scopes (e.g. `chat:write`).
+ App details → _Features_ → _OAuth & Permissions_ → _Scopes_ → _Bot Token Scopes_. +1. Install (or request an higher entity to install) the app in the workspace.
+ App details → _Settings_ → _Install App_ → _OAuth Tokens_.
+ This will automatically generate an OAuth token for the app. Note that token down. +1. Add the app to the channels it needs to interact with. + Channel details → _Integrations_ tab → _Apps_ section → _Add apps_. +1. Try sending messages using the app's OAuth token: + + ```sh + curl --request 'POST' --url 'https://slack.com/api/chat.postMessage?pretty=1' \ + --header 'Authorization: Bearer xoxb-012345678901-0123456789012-abcdefghijklmnopqrstuvwx' \ + --header 'Content-type: application/json' --data '{"channel": "C04K1234567", "text": "Hello, World!"}' + ``` + +## Create apps + +[Direct link](https://api.slack.com/apps?new_app=1). + ## Further readings - [Website] - [Sending messages using incoming webhooks] - [Posting messages using curl] +- [CLI] ### Sources - [Slackmojis] - [Slack Notifications for Ansible Tower (AWX)] - [Setting Up Slack Webhook URL Simplified 101] +- [How to quickly get and use a Slack API bot token] +- [Automating Slack Notifications: Sending Messages as a Bot with Python] +[create apps]: #create-apps + +[cli]: https://tools.slack.dev/slack-cli/ [posting messages using curl]: https://api.slack.com/tutorials/tracks/posting-messages-with-curl [sending messages using incoming webhooks]: https://api.slack.com/messaging/webhooks [website]: https://slack.com/ +[how to quickly get and use a slack api bot token]: https://api.slack.com/tutorials/tracks/getting-a-token +[automating slack notifications: sending messages as a bot with python]: https://medium.com/@sid2631/automating-slack-notifications-sending-messages-as-a-bot-with-python-2beb6c16cd8c [setting up slack webhook url simplified 101]: https://hevodata.com/learn/slack-webhook-url/ [slack notifications for ansible tower (awx)]: https://mpolinowski.github.io/docs/DevOps/Ansible/2021-04-30-ansible-tower-slack-notifications/2021-04-30/ [slackmojis]: https://slackmojis.com/