I learned a few things while working with the Slack API
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...
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"
<https://www.google.com|visit google>
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
slack info