How to add # mention in response to slash commands in slack - slack-api

How can I make slack parse #someone mentions as links to the user instead of plaintext. I've been reading slack documentation on message formatting but still haven't figured it out. Here's an example of what I'm getting now:
{
"text": "*username:* #alexis",
"response_type": "ephemeral"
}

To make a proper "clickable" mention, you need to pass the unique user ID and not the plaintext name.
The format of the user ID is: U024BE7LHand a mention would look like this: <#U024BE7LH>
Ther user ID of the user that executed the slash command will be in the payload that slack sends to your endpoint. You can also look up user IDs by calling the users.list method, which will give you access to the user IDs of all the users in the team.
More information here

Pass the username inside <> quotes like this <#someone>
Sample
{
"text": "*username:* <#alexis>",
"response_type": "ephemeral"
}
Also if instead of user you want to notify channel then use !channel, !group , !here , or !everyone instead of #username
For eg.
{
"text": "*username:* <!channel>",
"response_type": "ephemeral"
}

slack_web_client.chat_postMessage(
channel="channel_name",
text="Hi! <#User_id>"
)
Place User ID in <> quotes and make sure that your app/bot has Scopes for commands.
Further details - https://api.slack.com/interactivity/slash-commands

Related

Replacing slack app mentions with usernames in the slack message log in Dataweave

I'm parsing the slack message log from conversations.history and any app mentions come in as <#XX12345>. I'm trying to parse the XX12345 part in a conversation and replace it with username and get rid of the < and >. For eg:
Hello <#UA12345> how are you?
I'm good <#UA67890>. How about you?
should become
Hello #lookup(UA12345) how are you?
I'm good #lookup(UA67890). How about you?
How do I achieve this using replace and regex in DataWeave? The lookup function is used to get the user name from Slack API. This function also needs to be triggered inside Dataweave (not sure if this is even possible). End result would be something like this:
Hello #Adam how are you?
I'm good #David. How about you?
Assuming the username in <#UA67890> is alphanumeric, you can use the following expression to get the required result
yourText replace /\<\#([a-zA-Z0-9]*)\>/ with "#$(getUsername($[1]))"
This matches the regex \<\#([a-zA-Z0-9]*)\> which captures alphanumeric value in between a <# and > in a group and then replace it with #$(getUsername($[1])), i.e. #getUsername(everything that was captured as in the above group)
You can create the function getUsername to actually call the lookup function and call the required flow. So your DataWeave will look something like this.
%dw 2.0
output text/plain
fun getUsername(userid) = lookup('get-user-name-flowname', userid) // Any other transformation that you may need for passing the required payload before calling loopup
var conversation =
"Hello <#UA12345> how are you?
I'm good <#UA67890>. How about you?"
---
conversation replace /\<\#([a-zA-Z0-9]*)\>/ with "#$(getUsername($[1]))"
Update: As mentioned in comment, you also need a flow get-user-name-flowname that will either use slack's REST API or <slack:get-usersprofileget> which will accept this ID and will return the username of the user

Gmail API, Reply to thread not working / forwarding

I'm using the google gmail api in swift. All is working well, it's compiling etc.
I'm now trying forward an email, the only way I see this possible so far is by using a thread id.
So I'm using the API tester found here to send tests. Will will focus on this. It can be found here1
So I've input this, the "raw" is Base64 URL encoded string.
{
"raw": "VG86ICBlbWFpbFRvU2VuZFRvQGdtYWlsLmNvbSAKU3ViamVjdDogIFRoZSBzdWJqZWN0IHRlc3QKSW4tUmVwbHktVG86ICBteUVtYWlsQGdtYWlsLmNvbQpUaHJlYWRJZDogIDE1YjkwYWU2MzczNDQ0MTIKClNvbWUgQ29vbCB0aGluZyBpIHdhbnQgdG8gcmVwbHkgdG8geW91ciBjb252by4u",
"threadId": "15b90ae637344412"
}
The "raw" in plain text is
To: emailToSendTo#gmail.com
Subject: The subject test
In-Reply-To: myEmail#gmail.com
ThreadId: 15b90ae637344412
Some Cool thing i want to reply to your convo..
when I execute it I get this back
{
"id": "15b944f6540396df",
"threadId": "15b90ae637344412",
"labelIds": [
"SENT"
]
}
But when I check both email account, from and to. None of them say the previous messages but are in the same "thread" or convo.
If anyone can help it would be much appreciated I've spent all day on this issue and half of yesterday and did TONS of research on it.
as stated here I should I'm adding the threaded and In-Reply-To in the right way I believe
The ID of the thread the message belongs to. To add a message or draft to a thread, the following criteria must be met:
The requested threadId must be specified on the Message or Draft.Message you supply with your request.
The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard.
The Subject headers must match.

Only I can see my output from custom slash commands

I created a custom slash command in Slack. The backend code, not that it's important, is a Lambda function in Python in AWS.
My problem is that when I enter the slash command, I am the only one who can see the message. Otherwise, it works perfectly. Is there a way to get others to see the output from my custom slash command?
See "'In Channel' vs. 'Ephemeral' responses" here: https://api.slack.com/slash-commands#responding_to_a_command.
By default, the response messages sent to commands will only be
visible to the user that issued the command (we call these "ephemeral"
messages). However, if you would like the response to be visible to
all members of the channel in which the user typed the command, you
can add a response_type of in_channel to the JSON response, like this:
{
"response_type": "in_channel",
"text": "It's 80 degrees right now.",
"attachments": [
{
"text":"Partly cloudy today and tomorrow"
}
]
}
When the response_type is in_channel, both the response message and the initial message typed by the user will be shared in
the channel.
If you have a block in your JSON payload to slack (you used slacks block-kit) e.g
`"blocks": []`
you'll have to put the "response_type": "in_channel" above blocks for it to work :) e.g
{
"response_type": "in_channel",
"blocks": [....]
}

Slack Slash Command - Image Not Unfurling

I have written a custom Slash command that takes in a query from the user and returns an image.
The Server side which receives the Slash command retrieves the query from the user, and forms the Image URL http://example.com/file1.png and it sends back the response as <http://example.com/file1.png>. This is shown as a link in the response and is not unfurled. What could be the problem?
I even tried the following:
1) I sent back a JSON payload as given below:
{
"text":"http://example.com/file1.gif","unfurl_media":true
}
But that displayed the link again and did not unfurl it.
2) I tried
{
"text":"<http://example.com/file1.gif>","unfurl_media":true
}
But same results.
What could be the problem? Do I absolutely need a incoming webhook integration and send the message there ?
Did it work the first time, or had the URL previously been unfurled?
Slack will only automatically unfurl a URL once per hour in a given channel. If a user manually posts the URL and it is not unfurled because of this limit they will get an ephemeral message from SlackBot about it, however the unfurl just silently fails for slash commands or webhooks. I've hit this before in testing and had to make sure to change either the URL or the channel to verify things are working.
You should not need a manual attachment or a webhook or even the unfurl_media flag in the response (it's on by default for messages posted via webhooks/slash command).
I was having a similar issue with slash commands returning the text of my image url.
What you need to change is to use an object with an image_url for your image, and put that inside an attachments array.
Below is an example that returns the link of the image as text and the image itself.
{
"parse": "full",
"response_type": "in_channel",
"text": "http://example.com/file1.png",
"attachments":[
{
"image_url": "http://example.com/file1.png"
}
],
"unfurl_media":true,
"unfurl_links":true
}
{
"parse": "full",
"text": "http://example.com/file1.png",
"attachments":[
{
"image_url": "http://example.com/file1.png"
}
],
"unfurl_media":true,
"unfurl_links":true
}

How to get a slack user by email using users.info API?

I'm trying to use Slack's users.info API to retrieve users information, but I need to find users by email, is there a way to do that?
Yes!
https://slack.com/api/users.lookupByEmail
Using this we can find a user if email id is available.
More : https://api.slack.com/methods/users.lookupByEmail
Currently you can only look up users with users.info by their ID.
An alternative solution to your problem would be to call users.list and filter within your client by the profile.email for whichever email you're looking for.
An undocumented API can do this job:
https://slack.com/api/auth.findUser?team=&email=&token=xoxp-XXXXXXXXX
If this is being done on behalf of a Slack slash command, one can configure the command to expand #username, #channels, etc...
This can be done under the command section of the Slack app. See the following screenshot:
You should use this scope users:read.email, the users:read is no longer a sufficient scope for email field.
Check this to get more infos: https://api.slack.com/scopes/users:read.email
That's worked for me as wanted !
This was useful for me.
My setup: I am part of an enterprise, so the legacy token does not have users:read.email scope to it.
Solution: I created an app with users:read.email scope and other scopes needed. Got the app approved from my admin, installed the app to my workspace, retrieved the OAuth token, used it with https://slack.com/api/users.lookupByEmail.
you can get the userid with message.user from main calling method
getUsername(userID).then((output) => { username = output.user.name });
function getUsername(userid){
return new Promise((resolve, reject) => {
//get token from https://api.slack.com/methods/users.info
options.uri = "https://slack.com/api/users.info?token=********&userid=" +userid+ "&pretty=";
rp(options).then(function (body) {
resolve(body);
console.log('Retrieved Info slack --- ' + JSON.stringify(body));
})
.catch(function (err) {
resolve(err);
console.log('aborted - slack ' + JSON.stringify(err));
});
});
}
refer link : https://github.com/hassifow/Slack.API-User.info/blob/master/LambdaFunction.js
https://api.slack.com/methods/users.lookupByEmail
POST https://slack.com/api/users.lookupByEmail?email=seunggabi#gmail.com
form-data
token=xoxb-############-#############-$$$$$$$$$$$$$$$$$$$$$$$$

Resources