0

Ok so I've googled around a bit and couldn't find many answers on this, so hopefully somebody here can help.

I'm basically trying to make a Slack app which people in my office can use to notify people in the office channel that they are waiting downstairs and need to be let in, since not everybody has a key. Before I started working on this little project an incoming webhook was used to send messages to a specific channel, however I recently changed this to use the Bolt framework instead, as it should simplify the whole process and let me easily add other formatting options in the future.

When a message is sent, it is supposed to mention the Slack app itself as well as the whole channel (@slackApp @channel somebody is downstairs). The channel mention should notify everyone in the channel and the Slack app has to be mentioned so that other processes listening for this event can happen.

However, when I tried to implement this it would send a message into the desired Slack channel but only mention the whole channel.

The code below is called by a lambda function:

def respond_slack(name, location="Placeholder"):
 channel_id = ourOfficeChannel # just a placeholder id for this post
 slack_token = os.environ["SLACK_BOT_TOKEN"]
 slackClient = WebClient(token=slack_token)
 try:
 result = slackClient.chat_postMessage(
 channel=channel_id,
 text=str('<@U041Z8F0TGE> <!channel> Somebody is downstairs!')
 )
 print(result)
 except SlackApiError as e:
 print(f"Error: {e}")

I tried sending two messages, one with the channel mention and one with the app mention, however it would only send the message containing the channel mention.

I'm only an intern working on this and I don't have any formal training like university or anything, so I don't really know what I'm doing. Any help would be appreciated :).

PS: This is my first StackOverflow post, feel free to let me know what other details I should include in the original post

2 Answers 2

0

Ok so for anyone wondering, this is how I solved the issue: The problem was not with the Bolt framework or with Slack, but rather that I just completely forgot to actually deploy any of the changes I had made, meaning the code in the lambda function (the code that was being executed) was still the same as before. I am an idiot.

answered Oct 13, 2023 at 10:17
Sign up to request clarification or add additional context in comments.

Comments

-1

To mention the Slack app in your message, you need to use the app's user ID instead of its name. You can find the user ID of your Slack app by going to the About section of your app's settings page.

Here's an updated version of your code that should mention both the Slack app and the channel:

def respond_slack(name, location="Placeholder"):
 channel_id = ourOfficeChannel # just a placeholder id for this post
 slack_token = os.environ["SLACK_BOT_TOKEN"]
 slackClient = WebClient(token=slack_token)
 try:
 result = slackClient.chat_postMessage(
 channel=channel_id,
 text=str(f'<@{slackClient.users_info(user="U041Z8F0TGE")["user"]["id"]}> <!channel> Somebody is downstairs!')
 )
 print(result)
 except SlackApiError as e:
 print(f"Error: {e}")

In this updated code, we are using the users_info method of the WebClient class to get the user ID of the Slack app, and then using that ID to mention the app in the message

HIMANSHU PANDEY
7221 gold badge12 silver badges24 bronze badges
answered Oct 10, 2023 at 10:51

3 Comments

Just tried this code but for some reason it still doesn't want to mention the slack app. Is there maybe some formatting going on regarding on how the mentions are being interpreted? Since the @channel mention technically includes everyone maybe it just skips mentioning the Slack app? That would be really weird though.
It's possible that the Slack app is not being mentioned because the @channel mention is taking precedence. You can try changing the order of the mentions in your message to see if that resolves the issue.
I just tried this but for some reason I'm getting the same result :/. I remember thinking this could be the issue with the webhook as well, but changing that also did nothing. I'll take a look to see if maybe this has something to do with all of this taking place as part of a lambda function, hopefully that will get me somewhere

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.