How to let a slack bot send a message as a user to another user? - slack

Consider two users A and B. I'm trying to make a bot that can send a message to user A on behalf of user B, not in the app channel, but in the personal IM channel between A and B. Once the message is sent, it should look like it was sent by user B only. Is there a way to make this happen?
I scoured the documentation, and here's what I tried:
chat.postMessage - This worked well, but the only issue is that when user B tells the bot to send a message to user A, user A gets this message in the app channel, not in the DM of user B.
chat.postMessage with user details - I passed in the username and icon_url of user B in chat.postMessage, so it would look like the user B sent the message. It kind of worked, but not really. User A gets the message in the default 'slackbot' channel. And in the message, although it showed user B's name and image, the "App" tag that's there for bots remained.
So is it actually possible to let a bot send a DM as user B and make it seem exactly like a normal DM to user A by user B? If not, (in the second case I tried) is it at least possible to receive the message in the IM channel between user A and B, rather than slackbot?
The documentation suggested using conversations.open to get the user ID, and then to pass this user ID in chat.postMessage. I tried this as well, but conversations.open gave me an error saying
Error: An API error occurred: user_not_found

The only way to send a message in the DM between User A and User B is to acquire the user token of either User A or B (depending on who you want the message to come from) and use that token to send the message.
It isn't possible to mimic a user and have it appear within the DM between two users.

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.

How to send direct message in Slackbot

I would like to send messages to users through Slackbot, just like integration apps such as Zapier.
If I set the channel ID to the user ID it sends a 1:1 message showing up in the Apps tab, but what I really would like to do is to send a message in Slackbot, as I want to create a seamless experience for a Workflow I'm creating.
Then, if I try adding the Slackbot channel ID it returns channel_not_found and there's no way to add the App there.
My current bot token scope to send messages is: chat:write and I'm sending a message using chat.postMessage
Do I need a different scope? Different API method? I've tried going through some other questions/answers, but they look outdated

Send user config to slack of an individual user

Related to my assigned project, I wrote a bash script to implement the task.
For the user account and secret/hash creation part, the manager told me to find a way to send the user config file content directly to the individual user's slack(not a channel with a group of people in it). How to do that?.
I can see, search gives me the result of sending to slack channel using curl with short text messages.
I am not sure how to do the following:
Send a config file content to an individual user's slack.
Suppose, if the user account, password, hashes generated in the file /root/user_config. I want to send the file content to the individual user's slack(not channel or group).
From purely Slack Conversations perspective,
You need to DM the user using following APIs :
Open Conversation (Get DM Id):
https://api.slack.com/methods/conversations.open
Post Message using DM Id :
https://api.slack.com/methods/chat.postMessage
What data you need to send is up to you.

Get user-id of user in direct message slack channel

We're developing a Slack bot and one of the slash commands should - when used in a direct message channel - be able to determine the Slack user-id of the target user in the direct message channel. So for example, imagine 3 Slack users User1, User2 and User3. If User1 would do a /someaction in his direct message channel with User3, the Slack server will post the following attributes to our implementation of the /someaction command:
token
team_id
team_domain
channel_id
channel_name=directmessage
user_id
user_name=User1
command=%2Fsomeaction
text=
response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2/........
trigger_id=....
user_id/user_name are the ones from the person issued the command, so User1 in this case.
To get the user_id of User3 we use the conversations.info API, where we pass in the channel_id that is provided to the /someaction command above.
Now the problem is that when User1 issue the command in Slack it does work, but when User2 does it (in his direct message channel with User3), it does not work. We did notice that the channel_id provided differs between the invocation of User1 and User2, which makes sense because both have their own direct message channel with User3
Response from User2
curl https://slack.com/api/conversations.info?token=TOKEN\&channel=D0123456G&pretty=1
{
"ok": false,
"error": "channel_not_found"
}
Questions:
Why does it work for User1, but not for User2? The only thing we can imagine is that User1 has deployed the Slack app in the Slack workspace. But that should not influence this behavior or should it?
Is there another way to get the user id of the target user of a direct message channel based on the channel_id of that direct message channel?
The reason why this approach does not work in the direct message channel between User 2 and User 3 is that your app has no access to that channel and therefore you get channel not found when calling conversations.info on that channel.
And it has no access because it has been installed by User 1 and therefore the app can only see channels that User 1 has access to. This is due to how the security architecture in Slack works.
To my knowledge it is not possible to get the user ID of the other member of a direct message channel from a slash command.

Slack Events API not working for normal users

I'm using slack api to send reply whenever there is any direct message to authenticated user. So I created app, enabled events api and verified webhook url and subscribed to messages.im event
I use regular oauth flow with scope(chat:write:user,users:read) to get the access token.
First I tried with the admin of the workspace and everything worked fine. Whenever there is a direct message between admin user and any other user, i'm receiving events to my callback.
NOW
when I tried same with normal user(user2) i'm not receiving any events back when there is direct message between user2 and some other user. I followed the same steps above.
User2 went through same oauth flow with same scopes and got the access token of his own. As I subscribed to events api, I should be able to received event callbacks to the url I mentioned.
Is there any issue here? Is this not how things work?
This is not supposed to work.
Your Slack app will only receive message events from channels / conversations, which the installing user is a member of (e.g. the admin is member of the direct messaging conversation between him and others). But it's not possible to get direct messages between other users.
This is how Slack's security architecture is designed. In general it is not possible for any Slack app to monitor all private and direct messaging channel, even if the installing use is an admin / owner.
A common workaround for private channels is to retrieve messages for a bot user and make sure that bot user is a member of all private channels you need to monitor. However, this workaround is not very practical for direct message conversations.
Turns out it is an issue with scopes, following is the message I received from slack support team, they are awesome.
Hi Sasikanth,
Thanks for getting back to me. I took a look and it seems that there was some change with the scopes requested when user2 installed the "My App Name" app.
Here are the scopes that each of these users received upon installing the "My App Name":
user1: im:history,users:read,chat:write:user
user2: users:read,chat:write:user
You'll notice that user 1 above has the im:history scope, whereas user 2 above does not. It's mentioned on the doc for the message.im event type (https://api.slack.com/events/message.im) that the im:history scope is needed.
That's the reason why you're not receiving the message.im event type for DMs sent to user 2.
I hope that helps to explain the issue. What you'll need to do is remove the authorization for user2 from: (my dev app url) and have that user reinstall the app with the appropriate scopes.

Resources