Microsoft botframework and slack channel - botframework

How do I get the url/team name from a bot framework activity/context? Currently I can get the TeamId but can that be translated into the text string for the teams name?
For example testteam.slack.com, how can I extract the "testteam" part from bot framework messages?

As already mentioned you can call the Slack API method team.info to get the domain name for a team. However, that requires your token to have specific scopes, which you might not have.
It's therefore better to call auth.test, because it does not require any special scopes (except the bot scope for a bot token, but that is implicit). This API method will return the full URL of the Slack team along with other basic info for the provided Slack token.
Note that you need a Slack token corresponding with the team you want to get the info for. The team ID alone is not sufficient to get info about a team. (same for team.info btw). I am not familiar with the botframework, but since it works with Slack it must have a method to retrieve the current Slack token.
Example output:
{
"ok": true,
"url": "https:\/\/subarachnoid.slack.com\/",
"team": "Subarachnoid Workspace",
"user": "grace",
"team_id": "T12345678",
"user_id": "W12345678"
}

You are looking for team.info method in Slack API, that you can query with your Bot User OAuth Access Token (visible in OAuth & Permissions menu, and also available in each bot message in the ChannelData, property named ApiToken).
You can get details about this method here: https://api.slack.com/methods/team.info
In particular, have a look to domain field in the response sample:
{
"ok": true,
"team": {
"id": "Txxxxxx",
"name": "BotDemoCompany",
"domain": "botdemocompany",
"email_domain": "xxxxxxx.com",
"icon": {
"image_34": "https:\/\/a.slack-edge.com\/xxx.png",
"image_44": "https:\/\/a.slack-edge.com\/xxx.png",
"image_68": "https:\/\/a.slack-edge.com\xxx.png",
"image_88": "https:\/\/a.slack-edge.com\/xxx.png",
"image_102": "https:\/\/a.slack-edge.com\/xxx.png",
"image_132": "https:\/\/a.slack-edge.com\/xxx.png",
"image_230": "https:\/\/a.slack-edge.com\/xxx.png",
"image_default": true
}
}
}

Related

MS Teams Webhook ActionCard HttpPost get the user who submitted the post

I have a incoming webhook that I sent a messageCard with several actioncards. I know how to post the data and value back to my server endpoint. I need to get the username of the user who pushed the button to submit the httppost. Here is the actioncard part of the code. It works. I just need to also know who submitted it, the MS Teams username.
{
"#type": "ActionCard",
"name": "Skip",
"inputs": [{
"#type": "TextInput",
"id": "skip",
"isMultiline": True,
"title": "Add a skip reason here"
}],
"actions": [{
"#type": "HttpPOST",
"name": "skip",
"target": "",
"body":"{"action":"skip","body":"{{skip.value}}}"}"
}]
}
Currently UPN is not sent part of the JSON body/payload, however it can be retrieved by decoding JWT token in Authorization header part of sender verification:
Service can validate the JWT and then extract claims and get the UPN as per below:
Security requirements for actionable messages - Outlook Developer | Microsoft Docs.
Also if you go through the Connector documentation, you’ll see that the ‘sub’ parameter contains the Azure AD object ID. You can then call Get users Graph API to get the user details from AAD Id.

Unable to delete a message sent via Incoming Webhooks

I am using Slack Incoming Webhooks requests to post messages to a Slack channel from an app. I love it since I can send messages directly from shell scripts.
However going further I stumble into the problem that I seem to be unable to delete messages.
The app has two access tokens:
An OAuth access token xoxp-012345678901-012345678901-012345678901-0123456789abcdef0123456789abcdef (this is a fictitious token but the length is the same as the actual token).
Bot User OAuth access token xoxb-012345678901-012345678901-0123456789abcdef01234567.
Using the xoxp- OAuth access token I can retrieve channel history.
curl "https://slack.com/api/channels.history?token=xoxp-012345678901-012345678901-012345678901-0123456789abcdef0123456789abcdef&channel=CABCDABCD&count=20&pretty=1"
With the xoxb- token the channels.history request fails with
{
"ok": false,
"error": "not_in_channel"
}
In chat history I have a message that I would like to delete. The message was posted using Incoming Webhooks associated with the App.
{
"type": "message",
"subtype": "bot_message",
"text": ":heavy_check_mark:",
"ts": "1580968882.000800",
"bot_id": "BABCDABCD",
"blocks": [
{
"type": "section",
"block_id": "5Ov",
"text": {
"type": "mrkdwn",
"text": "text of the message to delete",
"verbatim": false
}
}
]
}
However neither token works with chat.delete. Both
curl "https://slack.com/api/chat.delete?token=xoxb-012345678901-012345678901-0123456789abcdef01234567&channel=CABCDABCD&ts=1580968882.000800&pretty=1"
and
curl "https://slack.com/api/chat.delete?token=xoxp-012345678901-012345678901-012345678901-0123456789abcdef0123456789abcdef&channel=CABCDABCD&ts=1580968882.000800&pretty=1"
fail with
{
"ok": false,
"error": "cant_delete_message"
}
And the question is: Is there a way to delete a message posted by an app via Incoming Webhooks requests?
PS. Both chat:write:bot and chat:write:user permissions are granted.
Yes, but the token owner needs to be admin in order to have the right to delete message of other users / apps.
To clarify: This has nothing to do with OAuth scopes, but with the Slack role of the user who owns the token.

Slack API: Unable to get Chat:Write:Bot to send message as custom username

I'm trying to integrate Slack with our application using their web API. I need to use the chat.postMessage endpoint with a custom username and setting as_user = false. I'm able to post messages but when I set as_user=false it doesn't work.
Example:
{
"channel" : "1234689",
"text" : "Hello, It's me.",
"username": "DJDEPOLO",
"as_user" : false
}
Every time I make that call I'm getting back an error saying I'm missing chat:write:bot. But I can not figure out how to get that scope. I've tried everything I could think of and went over their documentation several times.
I tried requesting the scope using the OAuth route and when I add chat:write:bot to the scopes I get an error saying
Invalid permissions requested
Example:
https://slack.com/oauth/v2/authorize?scope=chat:write:bot&client_id=1234&redire....
It appears that I need to use the user token to perform this action but when I request my access token I'm getting back a bot token.
Has anyone ever had to work with chat:write:bot or any scope that ends with :bot? Or am I missing something here?
First, select your app at your apps for slack and navigate to 'OAuth & Permissions' page.
Then, click 'Update Scopes' in 'Scopes' section, scroll down, press 'Continue' and add chat:write to User Token Scopes. Then scroll down again and finish the process.
In order to get a user token, add user_scope param to your query instead of scope so that it looks like this https://slack.com/oauth/v2/authorize?user_scope=chat:write&redirect_uri=.... When you exchange the code for access token, you will receive something like this:
{
"ok": true,
"app_id": "A0KRD7HC3",
"team": {
"name": "Slack Softball Team",
"id": "T9TK3CUKW"
},
"authed_user": {
"id": "U1234",
"scope": "chat:write",
"access_token": "xoxp-1234",
"token_type": "user"
}
}
Pay attention to authed_user.access_token, as this is the token you need to send in your Authorization header.
Here is an example of POST body:
{
"channel": "Your channel id",
"as_user": true,
"text": "hi there",
"attachments": []
}
Hope it helps you.

Slack API - How to get bot_id when bot has no conversation history?

I am trying to get the bot_id value for a bot when it has no conversation history.
According to the documentation:
the bot_id fields appear in bot_message message event subtypes and in
the response of methods like conversations.history.
This makes sense and works.
But is it possible to get this bot_id value, when a bot has no conversation history?
For example, say the channel between Slackbot and my bot has no conversation history. The response of a conversations.history request to this channel is the following, which does not include the bot_id value.
{
"ok": true,
"channel": {
"id": "DC6N8Q1BK",
"created": 1534129098,
"is_im": true,
"is_org_shared": false,
"user": "USLACKBOT",
"last_read": "0000000000.000000",
"latest": null,
"unread_count": 0,
"unread_count_display": 0,
"is_open": true,
"priority": 0
}
}
Any other way to get the bot_id value?
If there is a bot user, you can get it from the normal userlist with users.list. But that will not include other apps.
Then there is the unofficial API method bots.list. That will give you a list of all bots, but its not officially supported and you will need a legacy token to use as with many undocumented methods.

Is botId that bot scoped? Is it permanent?

Is the botId that I receive in the webhook only bot scope or is it unique across all the bots found?
Is it permanent or can it be changed?
By botId I mean the id in recipient.id and replyToId that you fill in send message request to endpoint https://smba.trafficmanager.net/apis/v3/conversations/{{skype.idRecipient}}/activities:
{
"text": "God help us!",
"type": "message",
"from": {
"id": "{{skype.idBot}}",
"name": "bot"
},
"recipient": {
"id": "{{skype.idRecipient}}",
"name": "user"
},
"replyToId": "{{skype.idBot}}"
}
The ID you are talking about is unique only in the current channel (Skype/Facebook/Slack...) as it is the ID of ChannelAccount.
Here are some statements from documentation:
Every bot and user has an account within each channel. The account
contains an identifier (id) and other informative bot non-structural
data, like an optional name.
Also
Channel accounts have meaning only within their associated channel
So it's not excluded that id may be repeated on another channels.
And what about permanency, it depends on the channel you use as stated in documentation again:
The stability of associations between IDs, accounts, mailboxes, and
people depends on the channel
But if you want it to be "unique across all the bots found" then you can create an id by combining AppID, ChannelID and User ID.
Also here is a quite informative guide about IDs in Bot Framework which may be helpful to you

Resources