Make slack command to send response to issuer and to specific channel - slack

I would like to create a command on slack that after being invoked returns result to the issuer and also writes another message to specific channel.
Is this possible?
In the docs I've found that I can set in_channel property but I would like to specify response being sent to specific channel or even limit command invocation to specific channel.

Yes, you can achieve that with a standard slash commmand. The slash command triggers your app which can then return the results to the user and also send a message to another channel with chat.postMessage (provided your all has the necessary permissions).
Limiting the slash command invocation to specific channels is possible, but only works for public channels, because Slack will tell your app the name if the public channel it was invoked in, but not the name of a private channel.
For public channels your app will get the channel ID from where the slash command was invoked with every request. Your app can use that information to decide whether the command is permitted or not and respond accordingly.
For private channels you only get privategroup as channel name, but not the real name or group ID. You can of course use this info to exclude usage of the command in all private channels, but not for specific ones.
Here is an example of the request your app gets from Slack:
token=gIkuvaNzQIHg97ATvDxqgjtO
team_id=T0001
team_domain=example
enterprise_id=E0001
enterprise_name=Globular%20Construct%20Inc
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
response_url=https://hooks.slack.com/commands/1234/5678
trigger_id=13345224609.738474920.8088930838d88f008e0

Related

Slack Bot send DM message to the User

I want to send message as DM to the users, so I've come with some questions;
1- There are two ways to send DM to the user;
the first one open a conversation with user by using conversation.open, and send message. Then if I want to send the message to the same user, I use conversation.list and find the conversationId by userId, then send message to the same channel again.
Second one is just basically using userId as channelId parameter in chatPostMessageRequest.
I've tried both ways, and both of them are sending message as DM. So, what is the purpose of using conversation.open? I'm asking this because nearly all the answers say you should do this etc.
2- Seems like there is no way to get multiple users by emails except users.list?
3- Also seems like there is no way to send messages to the multiple user at the same time (the content of the messages are different, those are depends on user data).
Sending a direct message by user ID works with chat.postMessage as a convenience, a shortcut. But it's the only method/API that allows you to use a user ID as a channel ID, so Slack warns developers away from relying on it in totality -- if you use conversations.open and the resultant conversation ID, it'll work for other methods like chat.update should you need to edit the message after sending it.
Slack doesn't offer many "lookup" APIs -- most things you'll want to look up require fetching an entire data set (like a list of users with all pages of users.list) and then filtering the results yourself. There are no APIs to look up multiple users at once via email address.
There is also no way to send the same direct message content to multiple users with a single request. In most cases, when an app is sending the same message to multiple users the best practice would be to use a channel to broadcast the message a single time, with the intended recipients as members of the channel.

Slack channel not found. Private slack channel from a docker container

I have set up a slack app to allow me to send notifications from my code to a private slack channel.
I have invited the bot to the channel, and when I run the app from the command line, all is good.
However, when I try and run it from a docker container, I get channel not found. If I try and use exactly the same code, and send a message to a bot in a public channel, no issues all is good.
Both bots have permissions, chat:write and chat:write.public
Why would this only be working on some machines and not others?
I know this probably doesn't have enough detail, but I'm not sure what is needed to help diagnose this.
More information:
Thanks to #Suyash Gaur I didn't know about the groups scope. I've now added groups:write and groups:history to the app, but am still getting the same error from the docker version of the app.
The membership of the channel I'm trying to write too shows
cwpr_notifications on Aug 3, 2021
Can post messages to specific channels in slack, send messages as #cwprnotifications, send messages to channels #cwprnotifications isn't a member of, manage private channels that cwpr_notifications has been added to and create new ones, and view messages and other content in private channels that cwpr_notifications has been added to.
I am using a bottoken and channel ID to send the message. Do I need something else for a private channel.
When I first used the bot from the command line run, I had to invite the app to the channel to get it to work, but surely I only need to do that once?
I've confirmed that the environment variables are getting passed in correctly.
With limited set of information provided, I can only say that:
If the public channel is accessible and private channel is not,
It seems like an issue of scopes.
For public channels, scopes start with channels:*
For private channels, scopes start with groups:*
Can you look into the app's configuration and confirm the permissions are configured correctly?

How to get the name of the slack channel from conversation menu?

I have added a slack conversation dropdown and in the response I want to save/show channel name (or conversation name). I'm able to get the public channels information through conversation.info API but for the private channels, I am not able to fetch any details. Can someone help me how I can get the names along with channel id (or conversation id).
This is the response from the slack on selecting a channel
{'values': {'channel_block': {'selected_action_id': {'type': 'conversations_select', 'selected_conversation': 'G0*****JM'}}}}
The token you're using to call conversations.info must have access to the channel in question before you can access it. I'm assuming it doesn't, but without seeing the response from the API I can't be sure.

How do I make a text channel when the bot enters the server and when it detects there is not pre existing server with the channel name

I am making a private dm bot. I am trying to make the bot make a text channel when the bot joins the server called "private-dm" with the properties of "#everyone: read past messages = False" So it is private and when the bot doesn't see already see a channel names private-dm just in case the bot goes offline. I couldn't find any on_bot_join event in the docs and I also couldn't find anything to make a text channel when it has not detected any with the given name.
on_bot_join is known as on_guild_join.
For the second issue you can just loop over all existing channels using Guild.text_channels & check if any of them has that name. If not, make it.

Difference between Sash Command and Out going webhook

I know following about slack slash commands and out going webhooks
1) Slash Commands:
- When user types slash command, it Will trigger external URL (URL of external web service), with all required data and message
- Web service would process that as required, and would respond with HTTP Response.
2) Outgoing webhook:
- When user sends message with triggering words(optional) in specified channel, it will trigger external URL (URL of external web service), with all required data and message
- Web service would process that as required, and would respond with HTTP Response
Purpose of both these functionalities is same. Only difference I can see is - with slash command, there is no way to define triggering criteria. If this command is used, external URL is called irrespective of channel and words in the message. While with outgoing webhook, we can apply filters(matching criteria) to call external URL conditionally, whenever criteria matches.
Is there any other difference?
When should we use slash command over outgoing webhook and vice versa?
I want to listen to a particular channel, and sends its data to google sheet when they type "OOO, away, late".
I can create 3 slash commands or can create a outgoing webhook for these three triggering words for this channel.
What is the best approach to accomplish this? I think I should go with out going webhook, considering its capability of matching criteria.
The main difference is how they are triggered.
A slash command is only triggered when a user enters its specific command into a channel, e.g. /who to see members of a channel.
The outgoing webhook automatically sends all message of a channel to a web service or all messages that include a specific trigger word.
However, I would strongly recommend to use Slack events instead of outgoing webhooks for your use case, since the latter is marked as legacy integration. Also events are way more powerful, e.g. they also work in private channels.

Resources