How to Tag Users in Slack Notifications

When a Slack notification rule is invoked is it possible to tag the users associated with that notification rule in slack?

For example, someone may have a slack member ID of @12345 which, when embedded in a Slack notification like <@12345> it will tag that person.

I do not think this is possible out of the box, but you could create a custom copy of the plugin and add the desired information.

How would one go about making a custom copy with working slack mentions ?

The official guide is your friend: Notifications - via Email, SMS, ticket system and more

1 Like

i’ve been reading this forward and back but can’t see anything about customizing slack contacts to actual slack usernames ?

True. Because we can of course not cover all specific use cases. But you will find the answer to making a custom copy in there. And from there it is up to you to update the plugin to fulfil your needs.

1 Like

Was digging deeper with our slack understanding person and figured out why the slack notifications don’t work, it currently says please take a look @username, when it needs to be "please take a look <@username> , could someone point me at where these modules are on the monitoring server and i could try change that small part for me

(also username needs to be your slack id, not name, planning to create notification users specifically for these)

I think you have to make changes in this file

/omd/versions/2.2.x/lib/python3/cmk/notification_plugins/slack.py

where 2.2.x will be your currently running version.

I still don’t get, however, how checkmk team tested this plugin and why it is not fixed already.
I have exactly the same issue, that notification sent to Slack do not mention users because of lacking <>

Do not change this file, as it is part of the Checkmk installation itself, and you will lose these changes upon upgrade. Please refer to the link I posted above and how to make custom copies of built-in notification plugins.

I am not sure, if I understand, what exactly the issue seems to be. Can you walk me through it?

Do not change this file, as it is part of the Checkmk installation itself, and you will lose these changes upon upgrade.

Yeap, I’m aware :slight_smile:
My first idea was to patch this file after every upgrade. On the other hand it may be problematic in the future, so I hesitate.

Sure thing :slight_smile:

/omd/versions/2.2.x/lib/python3/cmk/notification_plugins/slack.py
line 67 (cmk 2.2.0p17)

+ ", ".join(map("@{}".format, context["CONTACTNAME"].split(","))),

prepares user names to be mentioned on Slack but they are treated literally and user is not mentioned at all, i.e. @username is displayed as regular text, not a “mention”

When I changed the above line and add <> around @{} like this

+ ", ".join(map("<@{}>".format, context["CONTACTNAME"].split(","))),

notifications stared to properly mention user but only one of them (I don’t know why not both, I’m not skillful enough in python to catch this message):

(screen in my next message, as new users can only embed one media :roll_eyes: )

This mention is a proper mention which triggers Slack’s notification rules, display info on hover etc. and this is the mention I would like to have out of the box, without the need of writing my own notification plugin that will basically fix just that.

This is, however the issue discribed by @liliang in the above comment and not the feature question asked by @jonh3000 in first place.

Please, let me know if you have more questions.

Missing screen from my previous post

user_mention

Side note:
Sorry readers if this topic looks a bit messy. If I knew replies are placed also at the end of the topic I would rather just quote the question and reply to it. There are also some limitations for new users I wasn’t aware of. Hopefully, next time I’ll do it better :slight_smile:

just updating this file now and again feels like a lot easier task than making a constantly new plugin for a task the plugin is supposed to do anyway.

I probably need to reload the site from omd admin or something after i’ve changed the python file ?

I probably need to reload the site from omd admin or something after i’ve changed the python file ?

script is executed when notification rule triggers it, so reload is not necessary
at least in my case it worked without reloading

Hi @robin.gierse
Didn’t hear from you after my explanations. Are they clear enough? Will you process this issue somehow internally? Or maybe I should create a separate topic for this issue? If so - which category is the best one for reporting such things?

I figured out today why mentioning worked only for some people in my team. It was due to changes in Slack: A lingering farewell to the username | Slack

My colleague’s account was created before those changes so @username worked for him but I have to use @user-id for me to work.

Let me describe my way of “fixing” Slack mentions in checkmk notifications.
A way faster than writing a plugin but with one caveat.

!WARNING!
You will change internal chekcmk file that will be overwritten during update, but at the moment of writing (2024-03) this is the only way for Slack mentions to work properly.

  1. Open /omd/versions/2.x.y.crx/lib/python3/cmk/notification_plugins/slack.py
  2. Chagne line 67 (add <> to @{} and change CONTACTNAME to CONTACTPAGER)

original

+ ", ".join(map("@{}".format, context["CONTACTNAME"].split(","))),

changed

+ ", ".join(map("<@{}>".format, context["CONTACTPAGER"].split(","))),
  1. checkmk > Setup > Users > Edit user
  2. Pager address > paste Slack member ID (instruction below)
  3. Accept pending changes
  4. Mentions start to work properly

How to find a Slack user ID

  1. Click on a user name within Slack.
  2. Click on “View full profile” in the menu that appears.
  3. Click the ellipses (three dots).
  4. Click on “Copy Member ID”.

And once again: You have to remember, that this file must be corrected after every upgrade.

Hopefully checkmk team will add “SlackID” option in user profile and introduce CONTACTSLACK variable in future versions :slight_smile:

2 Likes

For Cthulhu’s sake!
Do not modify built-in Checkmk files!

I said it several times now, but I’ll spell it our for you, as apparently the article is not clear enough about it.

cp $OMD_ROOT/lib/python3/cmk/notification_plugins/slack.py $OMD_ROOT/local/share/check_mk/notifications/slack.py
vim $OMD_ROOT/local/share/check_mk/notifications/slack.py
# Make your modifications
chmod +x $OMD_ROOT/local/share/check_mk/notifications/slack.py

That way you get a custom copy, that will not be overwritten on update.

Alternatively you can override the built-in files like this:

cp $OMD_ROOT/lib/python3/cmk/notification_plugins/slack.py $OMD_ROOT/local/lib/python3/cmk/notification_plugins/slack.py
vim $OMD_ROOT/local/lib/python3/cmk/notification_plugins/slack.py

As for the fix itself, you could simply open a pull request against the Checkmk repository.

2 Likes

ph’nglui mglw’nafh Cthulhu R’lyeh wgah’nagl fhtagn :wink:

You are absolutely right. This article is not THAT clear as your short description. Thanks a lot for it :pray:

However, I’ve updated my checkmk instances a few days ago - tried both suggestions but neither of them worked (so I fall back to Cuthulu’s unholy solution :wink: ) - what do I miss?

Regarding PR - I can create one regarding the missing brackets, but I’m not skilled enough to add CONTACTSLACK variable to be visible in UI and be later passed to notification script. So maybe a better idea is to create feature request here - let me know what do you think.

1 Like

Yep, makes sense! :+1:

I did not test the second approach, but it should work. You just need to make sure, that the path below $OMD_ROOT/local/ is exactly the same as in the source. And that the file is executable. For the first approach you would probably need to modify the source code further, which is beyond my skills.