page_type | description | products | languages | extensions | urlFragment | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
This sample bot archives Teams group chat messages and sends them as files to users. |
|
|
|
officedev-microsoft-teams-samples-bot-archive-groupchat-messages-csharp |
This sample demonstrates a bot that archives group chat messages in Microsoft Teams and sends them to users as downloadable files. It supports Teams SSO, Adaptive Cards, and Graph API integration for enhanced functionality.
- Teams SSO (bots)
- Adaptive Cards
- Graph API
-
.NET Core SDK version 6.0
determine dotnet version
dotnet --version
-
dev tunnel or Ngrok (For local environment testing) latest version (any other tunneling software can also be used)
-
Teams Microsoft Teams is installed and you have an account
The simplest way to run this sample in Teams is to use Teams Toolkit for Visual Studio.
- Install Visual Studio 2022 Version 17.10 Preview 4 or higher Visual Studio
- Install Teams Toolkit for Visual Studio Teams Toolkit extension
- In the debug dropdown menu of Visual Studio, select Dev Tunnels > Create A Tunnel (set authentication type to Public) or select an existing public dev tunnel.
- In the debug dropdown menu of Visual Studio, select default startup project > Microsoft Teams (browser)
- In Visual Studio, right-click your TeamsApp project and Select Teams Toolkit > Prepare Teams App Dependencies
- Using the extension, sign in with your Microsoft 365 account where you have permissions to upload custom apps.
- Select Debug > Start Debugging or F5 to run the menu in Visual Studio.
- In the browser that launches, select the Add button to install the app to Teams.
If you do not have permission to upload custom apps (sideloading), Teams Toolkit will recommend creating and using a Microsoft 365 Developer Program account - a free program to get your own dev environment sandbox that includes Teams.
-
Register a new application in the Microsoft Entra ID – App Registrations portal.
Enter the following to Click Expose An API Menu and add a scope details to configure the admin and user consent prompts with values that are appropriate for the access_as_user scope.
- Added the WebApplication Info Resource and Application ID URI in expose and API like: api://Tunnelbaseurl/App-id"
- Admin consent display name: Teams can access the user’s profile.
- Admin consent description: Teams can call the app’s web APIs as the current user.
- User consent display name: Teams can access your profile and make requests on your behalf.
- User consent description: Teams can call this app’s APIs with the same rights as you have.
-
In the Azure portal, select your resource group from the dashboard.
-
Select your bot channel registration link.
-
Open the resource page and select Configuration under Settings.
-
Select Add OAuth Connection Settings.
-
Complete the form as follows:
-
Name: Enter a name for the connection. You'll use this name in your bot in the appsettings.json file. For example BotTeamsAuthADv1.
-
Service Provider: Select Azure Active Directory V2. Once you select this, the Azure AD-specific fields will be displayed.
-
Client id: Enter the Application (client) ID .
-
Client secret: Enter the Application (client) secret.
-
Provide Scopes like "User.Read Chat.ReadWrite ChatMessage.Read"
- Select 'Accounts in any organizational directory (Any Azure AD directory - Multitenant)' under Supported account types and click "+Add a platform".
- On the flyout menu, Select "Web"
- Add
https://token.botframework.com/.auth/web/redirect
under Redirect URLs and click Configure button. - Once the flyout menu close, scroll bottom to section 'Implicit Grant' and select check boxes "Access tokens" and "ID tokens" and click "Save" at the top bar.
-
Setup for Bot
- Also, register a bot with Azure Bot Service, following the instructions here
- Ensure that you've enabled the Teams Channel
- While registering the bot, use
https://<your_tunnel_domain>/api/messages
as the messaging endpoint.
NOTE: When you create your app registration, you will create an App ID and App password - make sure you keep these for later.
-
Setup NGROK
-
Run ngrok - point to port 3978
ngrok http 3978 --host-header="localhost:3978"
Alternatively, you can also use the
dev tunnels
. Please follow Create and host a dev tunnel and host the tunnel with anonymous user access command as shown below:devtunnel host -p 3978 --allow-anonymous
- Setup for code
-
Clone the repository
git clone https://github.com/OfficeDev/Microsoft-Teams-Samples.git
-
Modify the
/appsettings.json
and fill in the following details: -
{{MICROSOFT-APP-ID}}
- Generated from Step 1 while doing Microsoft Entra ID app registration in Azure portal. -
{{ MICROSOFT-APP-PASSWORD}}
- Generated from Step 1, also referred to as Client secret -
{{ Connection Name }}
- Generated from Step 1, also referred as Instruction on setting connection. -
From a terminal, navigate to
samples/bot-archive-groupchat-messages/csharp
# run the bot dotnet run
Or from Visual Studio - Launch Visual Studio - File -> Open -> Project/Solution - Navigate to
bot-archive-groupchat-messages/csharp
folder - SelectFetchGroupChatMessages.sln
file - PressF5
to run the project
- Setup Manifest for Teams
-
This step is specific to Teams.
-
Edit the
manifest.json
contained in the ./appPackage folder to replace your Microsoft App Id (that was created when you registered your app registration earlier) everywhere you see the place holder string{{Microsoft-App-Id}}
(depending on the scenario the Microsoft App Id may occur multiple times in themanifest.json
) -
Edit the
manifest.json
forvalidDomains
and replace{{domain-name}}
with base Url of your domain. E.g. if you are using ngrok it would behttps://1234.ngrok-free.app
then your domain-name will be1234.ngrok-free.app
. -
Edit the
manifest.json
for"webApplicationInfo"
resource"api://botid-<<MICROSOFT-APP-ID>>"
with base Url of your domain. E.g. if you are using ngrok it would behttps://1234.ngrok-free.app
then your resource will beapi://botid-<<MICROSOFT-APP-ID>>
.- Zip up the contents of the
appPackage
folder to create amanifest.zip
(Make sure that zip file does not contains any subfolder otherwise you will get error while uploading your .zip package)
- Zip up the contents of the
-
-
Upload the manifest.zip to Teams (in the Apps view click "Upload a custom app")
- Go to Microsoft Teams. From the lower left corner, select Apps
- From the lower left corner, choose Upload a custom App
- Go to your project directory, the ./appPackage folder, select the zip folder, and choose Open.
- Select Add in the pop-up dialog box. Your app is uploaded to Teams.
-
Note: If you are facing any issue in your app, please uncomment this line and put your debugger for local debug.
- Sending archive chat messages text file of a groupchat to user
Select a groupchat and add the bot to chat.
Send getchat
message to the bot, you will recieve a consent card by the bot in your personal scope.
To learn more about deploying a bot to Azure, see Deploy your bot to Azure for a complete list of deployment instructions.