Working with Slack Bot

See: Basic Slack app setup

See Your Created Apps to get bot token

Initial Setup

$ echo 'export SLACK_BOT_TOKEN=xoxb-1234567890-1234567890123-abcdef1234567890' >> ~/.zshrc

Get Channel Info

A conversation object contains information about a channel-like thing in Slack. It might be a public channel, a private channel, a direct message, or a multi-person direct message.

$ curl -sF token=${SLACK_BOT_TOKEN} https://slack.com/api/conversations.list | jq .

See: https://api.slack.com/types/conversation

Need to add some permission to list some channels type. See: https://api.slack.com/methods/conversations.list

$ curl -s -F token=${SLACK_BOT_TOKEN} https://slack.com/api/conversations.list | jq '.channels[] | select(.is_archived != true and .is_channel == true and .is_private == false and .name_normalized == "note")'

List all private, public, channels, private groups and direct message

And filter with jq to get a specific channel

$ curl -sF token=${SLACK_BOT_TOKEN} https://slack.com/api/conversations.list\?types=public_channel,private_channel,mpim,im\&exclude_archived=true\&limit=200 | jq '.channels[] | select(.name == "test3")'

Get Channel Id

Use cursor and limit to get more result since the result is limited by 100 by default

See: https://api.slack.com/docs/pagination

$ curl -sF token=${SLACK_BOT_TOKEN} https://slack.com/api/conversations.list\?types=public_channel\&exclude_archived=true\&limit=500 | jq -r .response_metadata.next_cursor
dGVhbTpDMTRBOU5LRUU=

$ curl -sF token=${SLACK_BOT_TOKEN} https://slack.com/api/conversations.list\?types=public_channel\&exclude_archived=true\&limit=500\&cursor=dGVhbTpDMTRBOU5LRUU= | jq -r '.channels[] | select(.name == "github-actions-test") | .id'
C01HA3WGJ66

Related: Node.js: Slack API

Leave a Comment

Your email address will not be published.

1 × 2 =