Copilot Studio: Upload File To Chat Window In MS Teams

Copilot Studio: Upload File To Chat Window In MS Teams

In Copilot Studio you can upload a file to a chat window and then perform actions on it like: run a prompt or save the file to SharePoint. This method shows how to upload a file to an Agent used in Microsoft Teams. Each channel an Copilot Studio Agent is deployed to requires a different method for file upload so keep this in mind while building your Agent.

Table of Contents
• Introduction: The Request For Proposal Analyzer AgentAdd A New Topic To The Copilot Studio AgentUpload A File To The Copilot Studio Agent Chat WindowDeploy The Agent To The Microsoft Teams ChannelAdd The File Upload Agent To A Teams ChatBuild An Agent Flow To Analyze The Uploaded PDF FileFilter Teams Messages Array To Get Only The PDF FileGet The Uploaded File Content From OneDriveRun A Prompt Action With The Uploaded File As An InputSend The Prompt Output To The AgentDisplay A Message To The User




Introduction: The Request For Proposal Analyzer Agent

Employees at a construction firm use the Request For Proposal Analyzer Agent to get important details from an RFP. The employee can open the Copilot Studio Agent, upload the file to a Teams chat window and the Agent will return all project related deadlines.




Add A New Topic To The Copilot Studio Agent

Open Copilot Studio and create a new Agent named File Upload Agent.



Use this text in the Agent description.

The Agent will receive a file from the user in the chat window and perform actions on it.



Then go to the Topics tab and add a new Topic from blank named Upload A File To Copilot Studio (Teams).



We want the topic to trigger when the User asks to “analyze an RFP.”



Use this text to describe what the topic does.

This topic will analyze a request for proposal, also known as an RFP, and extract key information, such as deadlines.




Upload A File To The Copilot Studio Agent Chat Window

We want the Copilot Studio Agent to ask the User to upload a file in teams and then verify that a file was attached to the User’s response. To do this, add a Question node to Copilot Studio that looks File type entities.



Once the File is received we will analyze it with further actions. However, this takes a long time. Send a message “PDF File received. I am reading the request for proposal now” to let the User know what the Agent is doing.



Next, we will create a second message node to get further information about the JSON structure of the uploaded file attachments. This node is only temporary and we can delete it after we get the needed info. We cannot simply put this information in a variable and inspect the output because we must get the JSON format from Microsoft Teams. Every channel can have a slightly different structure for file attachments.



Use this formula in the message node:

JSON(
System.Activity.ChannelData.OriginalAttachments,
JSONFormat.IndentFour
)




Deploy The Agent To The Microsoft Teams Channel

Before we go any further with creating the Agent we must deploy it to Teams. The purpose of this is to get information about the file attachments JSON structure. Go to the Agent Settings, then navigate to Security > Authentication.



Choose the option named Authenticate With Microsoft.



Then go to the Channels tab and open the Teams and Microsoft 365 Copilot channel.



Press the Add Channel button.




Add The File Upload Agent To A Teams Chat

Once the Teams channel has been added to the Copilot Studio Agent open Microsoft Teams and search for the Agent. Click on the Agent to add it to a 1:1 chat.



Type the message Analyze RFP. Then attach a PDF document to the chat when the Agent asks the User to upload a file.



In response, the Agent will output the attachments data in JSON format. Later in the Agent build we will use the uniqueId to locate the file which needs to be processed. And we’ll use the contentType to determine which JSON objects are files vs. other chat messages.

It’s recommended to copy this JSON into Notepad or another text editor for future reference.




Build An Agent Flow To Analyze The Uploaded PDF File

The Agent will analyze a PDF file uploaded to the chat window and return all project deadlines from it. We will build an Agent flow to get the PDF file content from MS Teams, run a prompt action to determine the deadlines and return the output to the Agent.

Go to Add A Tool and select New Agent Flow.



Add a required Text input to the flow named OriginalAttachments_JSON. The Agent will take the file attachments JSON from MS Teams as input.




Filter Teams Messages Array To Get Only The PDF File

When the User attaches a PDF file and presses send in MS Teams the original attachments array output by the channel has two objects: one containing the file attachment info and another with any text the User entered in the chat window. We only want the file attachment info so we will need to filter the array.

Begin by adding an Initialize Variable action to the Agent Flow. Name the variable varOriginalAttachmentsArray and make it an array type variable.



Set the value to the OriginalAttachments_JSON trigger input and convert the text data into into a json. Otherwise we will not be able to perform a filter operation on the array.

json(triggerBody()?['text'])



Insert a Data Operations – Filter Array action and use the variable varOriginalAttachmentsArray in the From property.



We want to filter the array on the contentType column. Use this code in the left-side field of the Filter Query.

item()?['contentType']



The filtered results should contain only File Attachments. Use this value on the right-side of the Filter Query.

application/vnd.microsoft.teams.file.download.info




Get The Uploaded File Content From OneDrive

Files uploaded to a 1:1 chat or a group chat in MS Teams are stored in the OneDrive of the user who uploaded them. We want the PDF file content so we can use it as an input to a prompt action.

To do this, we need the unique identifier of the file in OneDrive. Create a new variable named varItemId with a string data type.




Then use this code in the Value property to store the uniqueId of the file in the variable.

first(body('Filter_array:_Teams_File_Download_Info'))?['content']?['uniqueId']



Strangely, we must use the uniqueId from teams to get the id for the file in OneDrive. Add an Office 365 Users – Send An HTTP Request action to the flow.



Use the GET method and supply this URI with the variable varItemId to target the file.

https://graph.microsoft.com/v1.0/me/drive/items/@{variables('varItemId')}



Now we can finally get the file content using the OneDrive For Business – Get File Content action.



Supply the id value in the File property by using this code.

body('Send_an_HTTP_request:_Get_File')?['id']




Run A Prompt Action With The Uploaded File As An Input

We will use a prompt action to get information from the uploaded file. Add the Run A Prompt action and create a new custom prompt.



Name the prompt Get Project Deadlines From A PDF and select the model GPT-4o.



Then copy and paste the instructions below into the prompt designer and add RFP document as a document type input.

Objective:
Your goal is to read a request for proposal document and identify all project related deadlines.


Instructions:
1. Read the entire request for proposal document and identify all deadlines
2. Determine what items must be delivered for each deadline
3. Output all deadlines in a bulleted list. Do not include any other text.


Output format:
deadline date e.g. yyyy-mm-dd
– deadline items


Prompt Inputs:
RFP Document: [RPF Document]



Test the prompt. The prompt output should look like this. Save and close the prompt designer.



Go back to Power Automate and select the prompt Get Project Deadlines From A PDF.



Then write this code in the document input field to convert the file content to base64.

base64(body('Get_file_content'))




Send The Prompt Output To The Agent

Once the prompt action is run we want to return its text to the Agent so it can be displayed to the User. Add the Respond To The Agent action to the flow. Create a new text type output variable with the name Prompt_Text. Populate the output variable with the Text dynamic value from the Run A Prompt action.



Then in Copilot Studio, select the Read An RFP Document (Teams) Agent Flow in the action node.



Add this formula to the Agent Flow input field named OriginalAttachments_JSON. And delete the message node with the same code directly above the action node.

JSON(
System.Activity.ChannelData.OriginalAttachments,
JSONFormat.IndentFour
)




Display A Message To The User

The final step is to display the message output by the Agent Flow to the User. Add a message node to the topic with the Agent Flow output variable in the message body.



Save the topic and publish the Agent. Then test the File Upload Agent in MS Teams. The Agent will now ask for the user to upload a file and send a list of project deadlines in the response message.





Questions?

If you have any questions or feedback about Copilot Studio: Upload File To Chat Window In MS Teams please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest

0 Comments
Oldest
Newest
Inline Feedbacks
View all comments