简体   繁体   中英

Mentioning people with multi-word display names or user ids in a Slack Message using a WebHook and Ansible Slack

I'm using the Ansible slack module to send notifications, which include @mentions for notifiable people. I'm having trouble with multi-word display names, though single-word display names work fine.

For example:

ok: [localhost] => {
    "msg": "message: `SANDPIT` started ELMS and LACM MapGuide\nFYI <@alice> <@bob>"
}

But then <@Genghis Khan> comes to work alongside Alice and Bob. And he, because he's Ghengis Khan, of course chooses a multi-word display name. Given the following message:

ok: [localhost] => {
    "msg": "message: `SANDPIT` started ELMS and LACM MapGuide\nFYI <@Ghenkis Khan> <@bob>"
}

@bob is recognised, <@Ghenkis Khan> isn't.

I've also tried user ids as recommended at https://api.slack.com/changelog/2017-09-the-one-about-usernames but they don't work any better, so I've reverted to display names. But for completeness here is an example of the message content, with valid user ids but an invalid result:

TASK [slack : debug] 
***********************************************************
ok: [localhost] => {
    "msg": "message: `SANDPIT` started ELMS and LACM MapGuide\nFYI <@U12345DK> <@U12345TT>"
}

As mentioned, Slack is not buying these user ids either.

Here's the Ansible task that sends the message to Slack. All tokens etc are working as expected, and this is all happening in the same workspace.

- name: Send message via Slack
  slack:
    token: 'valid token'
    color: 'valid color'
    msg: '{{ message }}'
    link_names: 1
  # problems with notifications should not fail the pipeline
  ignore_errors: yes

According to the official documentation of the msg property of the slack plugin all angle brackets and ampersands need to be escaped using HTML notation:

Message to send. Note that the module does not handle escaping characters. Plain-text angle brackets and ampersands should be converted to HTML entities (eg & to &) before sending. See Slack's documentation ( https://api.slack.com/docs/message-formatting ) for more.

So you want to try something like this:

TASK [slack : debug] 
***********************************************************
ok: [localhost] => {
    "msg": "message: `SANDPIT` started ELMS and LACM MapGuide\nFYI &lt;@U12345DK&gt; &lt;@U12345TT&gt;"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM