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

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

Related

How to use the Gmail API to read the subjects of emails sent from a yahoo group

I'm using the Gmail API with Oauth 2 to read all of my emails and print out the subject and the date, like so:
for m in email_list:
msg = service.users().messages().get(userId="me", id=m["id"], format="full").execute()
header_list = msg["payload"]["headers"]
for i in header_list:
nameindict=i["name"]
if nameindict=="Subject":
print(i["value"])
if nameindict=="Date":
print(i["value"])
this section is pasted into the main() function of the gmail API example, found here:
https://developers.google.com/gmail/api/quickstart/python
However, there's two problems:
-Sometimes, it misses the subject. I checked the full output and found that some outputs didn't have a subject attached, but couldn't find out why.
-This doesn't seem to work with email sent from a Yahoo group. The "full" option returns a jumble of letters and numbers, but no subject or readable date and time sent. Does anyone know if this is possible, and how I can do it?

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

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

"Exception Occurred" with addSiteAccount1

After having gotten a site's login form from getSiteLoginForm, I'm attempting to add a site, but I'm receiving
{ :errorOccurred=>"true", :exceptionType=>"Exception Occurred", :referenceCode=>"_fa9ede97-1792-45ca-b147-005ec4002d33" }
The URL I'm POSTing to (in Rails) is:
https://consolidatedsdk.yodlee.com/yodsoap/srest/private-fairshare/v1.0/jsonsdk/SiteAccountManagement/addSiteAccount1
and this is the POST data:
cobSessionToken=REDACTED
userSessionToken=REDACTED
siteId=11671
credentialFields.enclosedType=com.yodlee.common.FieldInfoSingle
credentialFields[0][displayName]=User Name
credentialFields[0][fieldType.typeName]=TEXT
credentialFields[0][isEditable]=true
credentialFields[0][name]=LOGIN
credentialFields[0][value]=testuser
credentialFields[0][valueIdentifier]=LOGIN
credentialFields[0][valueMask]=LOGIN_FIELD
credentialFields[1][displayName]=Password
credentialFields[1][fieldType.typeName]=IF_PASSWORD
credentialFields[1][isEditable]=true
credentialFields[1][name]=PASSWORD
credentialFields[1][value]=testpass
credentialFields[1][valueIdentifier]=PASSWORD
credentialFields[1][valueMask]=LOGIN_FIELD
I've triple checked the parameters, and they seem to match up with the documentation.
Is there something I'm missing?
Looking at the documentation it looks like there's a mismatch in the format of your parameters.
For example, you have the field credentialFields[0][displayName], but in the documentation it's referred to as credentialFields[0].displayName. Is is possible that the API expects fields in this format?
If the API does expect fields in the credentialFields[0][displayName] then it would make sense for the credentialFields.enclosedType field to follow the same format. In that case it should be credentialFields[enclosedType].

How to Add Tag via Asana API

I am trying to do a simple Salesforce-Asana integration. I have many functions working, but I am having trouble with adding a tag to a workspace. Since I can't find documentation on the addTag method, I'm sort of guessing at what is required.
If I post the following JSON to https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tasks:
{"data":{"name":"MyTagName","notes":"Test Notes"}}
The tag gets created in Asana, but with blank notes and name fields. If I try to get a bit more fancy and post:
{"data":{"name":"MyTagName","notes":"Test Notes","followers":[{"id":"MY_USER_ID"}]}}
I receive:
{"errors":[{"message":"Invalid field: {\"data\":{\"name\":\"MyTagName\",\"notes\":\"Test Notes\",\"followers\":[{\"id\":\"MY_USER_ID\"}]}}"}]}
I'm thinking the backslashes may mean that my request is being modified by the post, though debug output shows a properly formatted json string before the post.
Sample Code:
JSONGenerator jsongen = JSON.createGenerator(false);
jsongen.writeStartObject();
jsongen.writeFieldName('data');
jsongen.writeStartObject();
jsongen.writeStringField('name', 'MyTagName');
jsongen.writeStringField('notes', 'Test Notes');
jsongen.writeFieldName('followers');
jsongen.writeStartArray();
jsongen.writeStartObject();
jsongen.writeStringField('id', 'MY_USER_ID');
jsongen.writeEndObject();
jsongen.writeEndArray();
jsongen.writeEndObject();
jsongen.writeEndObject();
String requestbody = jsongen.getAsString();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://app.asana.com/api/1.0/workspaces/WORKSPACEID/tags');
req.setMethod('POST');
//===Auth header created here - working fine===
req.setBody(requestbody);
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
Any help appreciated. I am inexperienced using JSON as well as the Asana API.
The problem was that I was posting to the wrong endpoint. Instead of workspaces/workspaceid/tags, I should have been using /tags and including workspaceid in the body of the request.
Aha, so you can add tags and even set followers despite the API not mentioning that you can or claiming that followers are read-only.
So to sum up for anyone else interested: POSTing to the endpoint https://app.asana.com/api/1.0/tags you can create a tag like this:
{ "data" : { "workspace": 1234567, "name" : "newtagname", "followers": [45678, 6789] } }
where 1234567 is your workspace ID and 45678 and 6789 are your new followers.
Since you posted this question, Asana's API and developer has introduced Tags. You documentation lays out the answer to your question pretty clearly:
https://asana.com/developers/api-reference/tags
I'm a bit confused by your question. Your ask "how to add a tag" but the first half of your question talks about adding a task. The problem with what you describe there is that you are trying to set a task's followers but the followers field is currently read-only according to Asana's API documentation. That is why you are getting an error. You can not set followers with the API right now.
The second part of your question - with the sample code - does look like you are trying to add a tag. However, right now the Asana API does not support this (at least according to the API documentation). You can update an existing tag but you can't add one.
So, to sum up: at this time the API does not allow you to add followers to a task or to create new tags.

Google OAuth 2.0 Response

I need help figuring out why my OAuth 2.0 response is always something like:
https://accounts.google.com/o/oauth2/approval?as=[BUNCH OF LETTERS AND NUMBERS]&xsrfsign=[BUNCH OF LETTERS AND NUMBERS]
This is the response after I login to my Google account and click allow access to the application.
It's nothing like the response I'm expecting according to the documentation (http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#handlingtheresponse)
First of all, you did not specified the language you are coding.
Secondly, make sure you specify the corect RedirectUri. You can get or set if from google console (https://code.google.com/apis/console/).
Then, for this kind of url https://accounts.google.com/o/oauth2/approval?as=.....&xsrfsign=..... you won't get the authorization code from the url but from the content of the page:
I have extracted it from the raw code of the page: [C# code]
string []s=webBrowser1.Document.ActiveElement.InnerHtml.Split (new string [] {"value=\"", "\""}, StringSplitOptions.None);
string authCode=s[165];

Resources