Send Attachments to Bot in Teams - botframework

I'm working with BotBuilder in .NET C#.
I can't figure out how I can send an attachment to the bot using Teams client - I've tried using the Windows desktop app and the web client but neither shows an attachment button in a chat with the bot.
I also tried with the Android client and found that I could send image attachments but not other file types, which I then went back and found that I could do the same in desktop/web clients by pasting the image into the chat box.
Using this method I do get an item in Activity.Attachments with ContentType="image/*". Any other type of file that I try to attach in the Android client is not sent to the bot (nothing in the Activity.Attachments collection) and as I said the other clients won't allow me to even attach anything in 1:1 chat.
Attaching a file in a Teams Channel adds the file to the Channel but I don't get any reference to the attachment if I #mention the bot along with the attachment.
The only mention of consuming attachments in bot sent via Teams I can find is here where it's stated that you'll need to use a JwtToken to access the file. I'm guessing this is currently a limitation in Teams as I'm able to send/receive attachments from other channels, but I'd like to confirm that there isn't some nuance that I'm missing.

Currently, Microsoft Teams does not support the ability to send non-image files to bots.
We are currently working on delivering this feature, but we do not yet have an ETA.

Image attachment can be send through Teams by copy pasting them in the Chat window.Teams have pushed the new Build where you can have Attachment Functionality available in Chat BOT. Now you can attach any file in the teams channel but you need to continue to send the Jwt token.
You can explore the type FileDownloadInfo which can be used to know the file type, content and other required details after you send the attachment to the BOT.

To answer your first question. Microsoft Teams does not show "attachment" button by default. You can install "App Studio" in Teams, and create an app for your bot, specify your app allows upload attachments. And install it in your own Team account for testing.
The second question, you can't get the image attachment. The JSON from Microsoft Teams channel is different from other channels.
You may notice the "contentType" is different, and the "contentUrl" requires a login in order to download the image. You need to use "content.downloadUrl" instead.
"attachments": [
{
"contentType": "application/vnd.microsoft.teams.file.download.info",
"contentUrl": "https://xxx-xxx.sharepoint.com/personal/xxxx/Documents/Microsoft Teams Chat Files/Cloud section in Singapore.PNG",
"content": {
"downloadUrl": "https://xxxx-xx.sharepoint.com/personal/xxxx/_layouts/15/download.aspx?UniqueId=a3cf2177-1cc7-433b-8344-129140c0694e&Translate=false&tempauth=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvc2FnZTM2NS1teS5zaGFyZXBvaW50LmNvbUAzZTMyZGQ3Yy00MWY2LTQ5MmQtYTFhMy1jNThlYjAyY2Y0ZjgiLCJpc3MiOiIwMDAwMDAwMy0wMDAwLTxxxxx&ApiVersion=2.0",
"uniqueId": "xxxx-1cc7-433b-8344-xxxxxx",
"fileType": "png"
},
"name": "Cloud section in Singapore.PNG",
"thumbnailUrl": null
}
]

Related

A generic FileCard attachment for Microsoft Teams Bot Framework based bots for self-hosted files?

https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bots-filesv4 describes a consent-based file sending process where a bot receives a users consent to upload a file to their onedrive and is able to send a FileAttachment card which renders a nice File chicklet view in the users Teams client. The chicklet view lets the user view the file in the Teams built in browser or download it or save it etc. More sample code here: https://techcommunity.microsoft.com/t5/microsoft-teams-blog/working-with-files-in-your-microsoft-teams-bot-preview/ba-p/199441
The final file attachment JSON looks like this
{
"attachments": [{
"contentType": "application/vnd.microsoft.teams.card.file.info",
"contentUrl": "https://contoso.sharepoint.com/personal/johnadams_contoso_com/Documents/Applications/file_example.txt",
"name": "file_example.txt",
"content": {
"uniqueId": "1150D938-8870-4044-9F2C-5BBDEBA70C8C",
"fileType": "txt",
}
}]
}
The rendering on the Teams client looks like this (taken from a different file send). Clicking on the icon opens the Teams built in File Viewer and
In our case our bot is already hosting a set of files that it needs to send a user. We can obviously just send a URL to the file location using Markdown but that causes the Teams client to download the file, user then has to go locate the file in their Downloads folder, and then view the file. (A multi-step process to view the file)
We are hoping to find an attachment type that has the same UX as the built in FileAttachment above i.e does not rely on first uploading to user's onedrive etc. When we try to use the same FileCard format but use our own contentURL , the click actions, unsurprisingly, fail.
Q: Is there a similar attachment type available in the Bot Framework for Teams which will generate a similar experience for the user (nice chicklet view, leverage built in Teams viewer, with option to open in browser or download) but with a file that is hosted by the bot at a reachable web URL (rather than first uploading to a user's One Drive)? Anyone attempted to do this using an adpative card?
(p.s sending image files has a work around which causes images to render inline, so this is about other file types such as PDF or Powerpoint)
Unfortunately, there isn't a built-in card (or a version of the File info card) that supports downloading from arbitrary urls - this can only be used for files hosted in OneDrive or SharePoint (depending on the context).
One possible option is to display a "regular" adaptive card with an action that launches a task module and then handle the file download yourself from there using JS and the Teams SDK.

Sending a message to users as a bot in bot channel of Microsoft Teams using the Graph API

I have created a bot by following the steps mentioned in the doc.I have authenticated user using oauth 2.0 (auth code grant) as mentioned in the doc and in reverse I got a access token. But when I send message to channel in the teams using (/teams/{id}/channels/{id}/messages) API the message was sent on behalf of me. But I want my bot as the sender of message. Here is the image of the message that I have sent using the above API. and is there any way to send direct message to user as a bot?
Instead of using the Graph, there's another approach using the Bot Framework itself, to send a message to a team channel, a group chat, or a 1-1 conversation. The code doesn't even need to live inside the bot itself, it just needs to leverage the bot framework under the covers (for example, I have several Azure Functions that pro-actively message users). This idea is called "Proactive messaging" and you can read more about it in the docs here.
You do need to get certain fields when the user first installs the bot though, or any time the bot receives a message. I've described that more at Programmatically sending a message to a bot in Microsoft Teams. You haven't said what language you're using, but there are examples for a bunch of them - I can send you links if you let me know what you're using.

Programmatically sending a message to a bot in Microsoft Teams

I have created a proactive bot that basically asks certain questions to a user when a user starts conversation with the bot. The bot is deployed in Microsoft Teams environment. Is there any way that i can send automated message to a bot in a channel? I know messages can be sent using powershell by utilizing webhook url exposed by a particular team or using MS Flow. But I want to mention bot (e.g. #mybothandle) in the message so the bot starts asking questions by itself than requiring the user to start the conversation (by mentioning the bot manually) but not finding the way to mention.
Your suggestions are welcome.
Basically you want to message the user directly at a specific point in time (like 24 hours later). I'm doing this in a few different bots, so it's definitely possible. The link that Wajeed has sent in the comment to your question is exactly what you need - when the user interacts with your bot, you need to save important information like the conversation id, conversation type, service url, and To and From info. You can store this, for instance, in a database, and then you can actually have a totally separate application make the call AS IF IT WAS your bot. In my bots, for example, I have the bot hosted in a normal host (e.g. Azure Website) but then have an Azure Function that sends the messages, for example, 24 hours later. It just appears to the user as if it was a message from the bot, like normal.
You will also need the Microsoft App ID and App Password for your bot, which you should have already (if not, it's in the Azure portal).
In your "sending" application, you're going to need to create an instance of Microsoft. Bot.Connector.ConnectorClient, like follows:
var Connector = new ConnectorClient(serviceUrl, microsoftAppId: credentialProvider.AppId, microsoftAppPassword: credentialProvider.Password);
You also need to "trust" the service url you're calling, like this:
MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
Then you create an instance of Microsoft.Bot.Schema.Activity, set the required properties, and send it via the connector you created:
var activity = Activity.CreateMessageActivity();
activity.From = new ChannelAccount([FromId], [FromName];
activity.Recipient = new ChannelAccount([ToId], [ToName]);
activity.Conversation = new ConversationAccount(false, [ConversationType], [ConversationId]);
activity.Conversation.Id = [ConversationId];
activity.Text = "whatever you want to send from the bot...";
Connector.Conversations.SendToConversationAsync((activity as Activity)).Wait();
All the items in square braces are what you get from the initial conversation the user is having with the bot, except that the From and To are switched around (when the user sends your bot a message, the user is the FROM and your Bot is the TO, and when the bot is sending you switch them around.
Hope that helps
To all Future Visitors, Microsoft Graph API (Beta) now provides a way to send message and mention the bot/user using following endpoint:
https://graph.microsoft.com/beta/teams/{group-id-for-teams}/channels/{channel-id}/messages
Method: POST
Body:
"body": {
"contentType": "html",
"content": "Hello World <at id=\"0\">standupbot</at>"
},
"mentions": [
{
"id": 0,
"mentionText": "StandupBot",
"mentioned": {
"application": {
"id": "[my-bot-id]",
"displayName": "StandupBot",
"applicationIdentityType": "bot"
}
}
}
]
}
However, there is a bug that bot doesn't respond when receives the message:
Bot is not responding to #Mention when sending message using Graph API

How can I handle Facebook likes, gifs and stickers inside of my Bot Framework chatbot?

I have created a chatbot using the Microsoft Bot Framework. The only channel I'm interested in right now it's Facebook. When doing some tests on Facebook I've noticed that when I click on the "Like" button, and when I send an gif or audio message, the bot crashes with the following message:
"Value cannot be null. Parameter name: stringToEscape."
How can I handle this kind of message within my bot? I can't replicate this on the emulator, so I don't know what Facebook is sending to my bot when the user sends one of these kind of messages. I would like to just ignore them and avoid the bot crashing.

How to check credentials against botframework api

I'd like to check that my bot credentials (appId + appSecret) are ok to connect to https://api.botframework.com/bot/v1.0/messages.
I can't send a real message because i have no conversation running so I tried to post the following json message :
{ "type": "Ping"} but the response i got was
{
"error": {
"message": "Expression evaluation failed. Object reference not set to an instance of an object.",
"code": "ServiceError"
}
}
Is there any way to check if my access to the api is ok?
If you've registered your bot, you can visit the Bot Framework page, click on the My Bots menu, and select your registered bot. On your bot page, scroll down to the bottom left and there's a test box.
Also, you can use the emulator. It has a place in the upper right corner to replace the default credentials with your bot credentials. Then change the URL to where you have your bot deployed. Tip: remember to append 'api/messages' to the URL.
Download the BotFrameworkEmulator to test connectivity to your bot. It works on windows and OSX if you have mono installed. You can change the default settings that the emulator uses by typing '/settings' after running it. You will be prompted to enter your appId, appSecret and url endpoint for sending and receiving messages to/from your bot.
You can also use the directline rest api to initiate conversations and send messages to your bot

Resources