Get user-id of user in direct message slack channel - slack

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.

Related

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

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.

DM to any user on Slack using Slack API

I'm trying to send Direct Messages (DMs) to a user on Slack using chat.postMessage using Bot token. But I'm only able to send messages to the users that are in my workspace.
How can I send message to any user on another workspaces?
When I try to do so, I get: "error": "channel_not_found"
I've that user's UserID (U02....), user's email and my Bot token.
When you create a bot/app in Slack, you grant it OAuth Scopes which provide the bot access to certain information in your Slack instance. So for example, I expect you have added the users:read Bot Token Scope to your Slack app, so that it can determine the users, and userId's in your workspace.
However, this scope restricts the bot to only see users in your workspace.
There's a couple of ways around this though:
Solution 1 - Slack Connect
Now in Slack, you can message users in other workspaces with a feature called Slack Connect.
You'll first need to establish a connection with the user you want the bot to message. This can be arranged via an invite process, and once completed that userId should become available to the bot. You can use that userId in the channel field of the chat.postMessage API to direct message the user from the other workspace.
Solution 2 - Org Level App
If you are on an Enterprise version of Slack, you should have multiple workspaces within a company, that are all linked by an enterpriseId.
In this case, a possible solution might be to create what is known as an Org Level App to have access to information across multiple workspaces. More information on Org Level apps can be found here.

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.

How to let a user join channel in Slack App

I am working on a slack app which has its bot also.A user can share a file from the bot to any public channel even if he is not in that channel. If user is not in the channel then firstly I will add the user in the channel and then share the file. For this slack provides api https://api.slack.com/methods/channels.join which helps a user join a channel. But it expects a user token corresponding to the user who wants to join the channel. Now the issue is I only have bot token(xoxb-) and api token(xoxp-) corresponding to the user which has installed the app. So how can I get the token corresponding to any user whom I want to join OR I am missing something here. Please help.
No, you do not need a token corresponding with the joining user.
When installing a Slack app with a bot user you receive two tokens: a bot token and an app token. The app token (but not the bot token) will work for inviting users to public channels, provided your app has the required scopes (channels:write).

Telegram API: doesn't send message to user until receive first message from that user

I use Telegram API to send a message to a user with my bot.
When the user sends me a message, that operation works but when I want to send a message to an user that haven't sent me any message to me before, then an error occurs (I have his user id):
{"ok":false,"error_code":400,"description":"Error: Bad Request: user not found"}
Is there a solution to solve this?
Telegram bots cannot be a conversation initiators - they can write to user only after that user sends a message to bot first: https://core.telegram.org/bots#4-how-are-bots-different-from-humans
If this error appears in a group, first you need add this bot to that group.

Resources