Skip to content

Instantly share code, notes, and snippets.

@dsmith73
Last active December 15, 2022 13:03
Show Gist options
  • Save dsmith73/f389d224bd95e56c0e84e8b64fc3b30c to your computer and use it in GitHub Desktop.
Save dsmith73/f389d224bd95e56c0e84e8b64fc3b30c to your computer and use it in GitHub Desktop.
Things I learned while working with the Slack API

Slack API

I learned a few things while working with the Slack API

Slack oauth

No one seems to mention is, but when you want to auth with slack you need to pass

headers:
  Authorization: "Bearer <token>"

I know it's a standard, but it's not really mentioned anywhere...


Block messages

Content-Type

While most of slack prefers Content-Type: "application/x-www-form-urlencoded"
I find that I get more mileage with message blocks using Content-Type: "application/json"

Hpyerlinks

<https://www.google.com|visit google>

@user / mention user

Like hyperlinks, but you need json.user.name from the user lookup:

    "json": {
        "ok": true,
        "user": {
            "color": "e0a729",
            "deleted": false,
            "id": "U777DAAAA77",
            "is_admin": true,
            "is_app_user": false,
            "is_bot": false,
            "is_email_confirmed": true,
            "is_owner": true,
            "is_primary_owner": false,
            "is_restricted": false,
            "is_ultra_restricted": false,
            "name": "george.washington",     

Once you retrieve it, just enclose it like a link
<@json.user.name>

And here's a play that I use to automate delivery of a reset password to a user

- name: "MESSAGE: User"
  ansible.builtin.uri:
    url: "https://slack.com/api/chat.postMessage"
    method: POST
    body_format: json
    headers:
      Content-Type: "application/json" 
      Authorization: "Bearer {{ bot_user_oauth_token }}"
    body: 
      channel: "{{ slack_channel_id }}"
      blocks: [
        {
          "type": "header",
          "text": {
            "type": "plain_text",
            "text": ":warning:  Password Reset: {{ app_name }}  :warning:"
          }
        },
        {
          "type": "context",
          "elements": [
            {
              "text": "*{{ ansible_date_time.date }}*",
              "type": "mrkdwn"
            }
          ]
        },
        {
          "type": "context",
          "elements": [
            {
              "text": "_Please <https://www.weblink.goes.here.com|contact your IT team> for additional assistance_",
              "type": "mrkdwn"
            }
          ]
        },
        {
          "type": "divider"
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "<@{{ slack_uname }}>, your new password is:"
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "```{{ new_password }}```"
          }
        }
      ]
  register: slack_message

@dsmith73
Copy link
Author

slack info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment