-
Notifications
You must be signed in to change notification settings - Fork 65
Add Teams Header Propagation sample #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sw-joelmut
wants to merge
8
commits into
southworks/add/header-propagation
from
southworks/add/teams-header-propagation-sample
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9aa1e04
Add Teams Header Propagation sample
sw-joelmut a8d6cc7
Merge branch 'southworks/add/header-propagation' into southworks/add/…
sw-joelmut 9fcecce
Fix typos and add more description
sw-joelmut ef42f8a
Merge remote-tracking branch 'origin/southworks/add/header-propagatio…
sw-joelmut bda1e12
Change MS Teams sample to latest main changes
sw-joelmut 1b2ae4b
Merge remote-tracking branch 'origin/southworks/add/header-propagatio…
sw-joelmut ee5a091
Merge branch 'southworks/add/header-propagation' into southworks/add/…
sw-joelmut ae21877
Update src/samples/Teams/HeaderPropagation/appsettings.json
sw-joelmut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions
49
src/samples/Teams/HeaderPropagation/AppManifest/manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| { | ||
| "$schema": "https://developer.microsoft.com/json-schemas/teams/v1.16/MicrosoftTeams.schema.json", | ||
| "manifestVersion": "1.16", | ||
| "version": "1.0.0", | ||
| "id": "<<AAD_APP_CLIENT_ID>>", | ||
| "packageName": "com.Microsoft.Agents.Extensions.Teams.HeaderPropagation", | ||
| "developer": { | ||
| "name": "Microsoft", | ||
| "websiteUrl": "https://example.azurewebsites.net", | ||
| "privacyUrl": "https://example.azurewebsites.net/privacy", | ||
| "termsOfUseUrl": "https://example.azurewebsites.net/termsofuse" | ||
| }, | ||
| "icons": { | ||
| "outline": "outline.png", | ||
| "color": "color.png" | ||
| }, | ||
| "name": { | ||
| "short": "Header Propagation", | ||
| "full": "Header Propagation" | ||
| }, | ||
| "description": { | ||
| "short": "Teams bot with header propagation.", | ||
| "full": "This sample demonstrates how to enable the header propagation feature in a Teams bot." | ||
| }, | ||
| "accentColor": "#FFFFFF", | ||
| "bots": [ | ||
| { | ||
| "botId": "<<AAD_APP_CLIENT_ID>>", | ||
| "scopes": [ | ||
| "personal" | ||
| ], | ||
| "isNotificationOnly": false, | ||
| "supportsCalling": false, | ||
| "supportsVideo": false, | ||
| "supportsFiles": false | ||
| } | ||
| ], | ||
| "permissions": [ | ||
| "identity", | ||
| "messageTeamMembers" | ||
| ], | ||
| "validDomains": [ | ||
| "token.botframework.com" | ||
| ], | ||
| "webApplicationInfo": { | ||
| "id": "<<AAD_APP_CLIENT_ID>>", | ||
| "resource": "api://botid-<<AAD_APP_CLIENT_ID>>" | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions
20
src/samples/Teams/HeaderPropagation/ConversationStateExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.Builder.State; | ||
| using Microsoft.Agents.Core.Models; | ||
|
|
||
| namespace HeaderPropagation; | ||
|
|
||
| public static class ConversationStateExtensions | ||
| { | ||
| public static void SaveActivity(this ConversationState state, IActivity activity) | ||
| { | ||
| state.SetValue($"history/{activity.Id}", activity); | ||
| } | ||
|
|
||
| public static IActivity GetActivity(this ConversationState state, string activityId) | ||
| { | ||
| return state.GetValue<IActivity>($"history/{activityId}"); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/samples/Teams/HeaderPropagation/HeaderPropagation.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Remove="wwwroot\**" /> | ||
| <Content Remove="wwwroot\**" /> | ||
| <EmbeddedResource Remove="wwwroot\**" /> | ||
| <None Remove="wwwroot\**" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\libraries\Authentication\Authentication.Msal\Microsoft.Agents.Authentication.Msal.csproj" /> | ||
| <ProjectReference Include="..\..\..\libraries\Hosting\AspNetCore\Microsoft.Agents.Hosting.AspNetCore.csproj" /> | ||
| <ProjectReference Include="..\..\..\libraries\Extensions\Microsoft.Agents.Extensions.Teams\Microsoft.Agents.Extensions.Teams.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Content Update="appsettings.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </Content> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.Builder; | ||
| using Microsoft.Agents.Builder.App; | ||
| using Microsoft.Agents.Builder.State; | ||
| using Microsoft.Agents.Core.Models; | ||
| using Microsoft.Agents.Extensions.Teams.App; | ||
| using System.Threading.Tasks; | ||
| using System.Threading; | ||
|
|
||
| namespace HeaderPropagation; | ||
|
|
||
| public class MyAgent : AgentApplication | ||
| { | ||
| public MyAgent(AgentApplicationOptions options) : base(options) | ||
| { | ||
| RegisterExtension(new TeamsAgentExtension(this), (ext) => | ||
| { | ||
| ext.OnMessageEdit(OnMessageEditAsync); | ||
| }); | ||
| OnConversationUpdate(ConversationUpdateEvents.MembersAdded, WelcomeMessageAsync); | ||
| OnActivity(ActivityTypes.Message, OnMessageAsync, rank: RouteRank.Last); | ||
| } | ||
|
|
||
| private async Task OnMessageEditAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) | ||
| { | ||
| var prevActivity = turnState.Conversation.GetActivity(turnContext.Activity.Id); | ||
|
|
||
| await turnContext.SendActivityAsync($"Activity '{prevActivity.Id}' modified from '{prevActivity.Text}' to '{turnContext.Activity.Text}'", cancellationToken: cancellationToken); | ||
| } | ||
|
|
||
| private async Task WelcomeMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) | ||
| { | ||
| foreach (ChannelAccount member in turnContext.Activity.MembersAdded) | ||
| { | ||
| if (member.Id != turnContext.Activity.Recipient.Id) | ||
| { | ||
| await turnContext.SendActivityAsync(MessageFactory.Text("Hello and Welcome!"), cancellationToken); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task OnMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) | ||
| { | ||
| turnState.Conversation.SaveActivity(turnContext.Activity); | ||
|
|
||
| await turnContext.SendActivityAsync($"You said: {turnContext.Activity.Text}", cancellationToken: cancellationToken); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using HeaderPropagation; | ||
| using Microsoft.Agents.Builder; | ||
| using Microsoft.Agents.Hosting.AspNetCore; | ||
| using Microsoft.Agents.Storage; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Threading; | ||
|
|
||
| WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddControllers(); | ||
| builder.Services.AddHttpClient(); | ||
| builder.Logging.AddConsole(); | ||
|
|
||
| // Add AgentApplicationOptions from config. | ||
| builder.AddAgentApplicationOptions(); | ||
|
|
||
| // Add the Agent | ||
| builder.AddAgent<MyAgent>(); | ||
|
|
||
| // Register IStorage. For development, MemoryStorage is suitable. | ||
| // For production Agents, persisted storage should be used so | ||
| // that state survives Agent restarts, and operate correctly | ||
| // in a cluster of Agent instances. | ||
| builder.Services.AddSingleton<IStorage, MemoryStorage>(); | ||
|
|
||
| WebApplication app = builder.Build(); | ||
|
|
||
| // Configure the HTTP request pipeline. | ||
| app.UseRouting(); | ||
|
|
||
| // Add the HeaderPropagation middleware to propagate incoming request headers to outgoing ones. | ||
| app.UseHeaderPropagation(); | ||
|
|
||
| app.MapGet("/", () => "Microsoft Agents SDK Sample"); | ||
| app.MapPost("/api/messages", async (HttpRequest request, HttpResponse response, IAgentHttpAdapter adapter, IAgent agent, CancellationToken cancellationToken) => | ||
| { | ||
| await adapter.ProcessAsync(request, response, agent, cancellationToken); | ||
| }) | ||
| .AllowAnonymous(); | ||
|
|
||
| // Hardcoded for brevity and ease of testing. | ||
| // In production, this should be set in configuration. | ||
| app.Urls.Add($"http://localhost:3978"); | ||
|
|
||
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # HeaderPropagation Sample | ||
|
|
||
| This sample shows how to enable the Header Propagation feature to capture all incoming request headers and propagate them to outgoing requests. | ||
| To achieve this behavior, the header propagation functionality requires the bot to use the `Extensions.Teams` package, as it contains the necessary configuration to decide which headers are going to be propagated to outgoing requests. | ||
|
|
||
| Additionally, to enable this functionality, the Header Propagation middleware is required as follows: | ||
| ```cs | ||
| var builder = WebApplication.CreateBuilder(args); | ||
| var app = builder.Build(); | ||
|
|
||
| // Add the HeaderPropagation middleware to propagate incoming request headers to outgoing ones. | ||
| app.UseHeaderPropagation(); | ||
| ``` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Microsoft Teams is installed and you have an account (not a guest account) | ||
| - [.Net](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) version 8.0 | ||
| - [dev tunnel](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started?tabs=windows) | ||
| - [M365 developer account](https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/build-and-test/prepare-your-o365-tenant) or access to a Teams account with the appropriate permissions to install an app. | ||
|
|
||
| ## Running this sample | ||
|
|
||
| **To run the sample connected to Azure Bot Service, the following additional tools are required:** | ||
|
|
||
| 1. [Create an Azure Bot](https://aka.ms/AgentsSDK-CreateBot) | ||
| - Be sure to add the Teams Channel | ||
| - Record the Application ID, the Tenant ID, and the Client Secret for use below | ||
|
|
||
| 1. Configuring the token connection in the Agent settings | ||
| > The instructions for this sample are for a SingleTenant Azure Bot using ClientSecrets. The token connection configuration will vary if a different type of Azure Bot was configured. For more information see [DotNet MSAL Authentication provider](https://aka.ms/AgentsSDK-DotNetMSALAuth) | ||
|
|
||
| 1. Open the `appsettings.json` file in the root of the sample project. | ||
|
|
||
| 1. Find the section labeled `Connections`, it should appear similar to this: | ||
|
|
||
| ```json | ||
| "TokenValidation": { | ||
| "Audiences": [ | ||
| "{{ClientId}}" // this is the Client ID used for the Azure Bot | ||
| ], | ||
| "TenantId": "{{TenantId}}" | ||
| }, | ||
|
|
||
| "Connections": { | ||
| "ServiceConnection": { | ||
| "Settings": { | ||
| "AuthType": "ClientSecret", // this is the AuthType for the connection, valid values can be found in Microsoft.Agents.Authentication.Msal.Model.AuthTypes. The default is ClientSecret. | ||
| "AuthorityEndpoint": "https://login.microsoftonline.com/{{TenantId}}", | ||
| "ClientId": "{{ClientId}}", // this is the Client ID used for the connection. | ||
| "ClientSecret": "00000000-0000-0000-0000-000000000000", // this is the Client Secret used for the connection. | ||
| "Scopes": [ | ||
| "https://api.botframework.com/.default" | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| ``` | ||
|
|
||
| 1. Replace all **{{ClientId}}** with the AppId of the Azure Bot. | ||
| 1. Replace all **{{TenantId}}** with the Tenant Id where your application is registered. | ||
| 1. Set the **ClientSecret** to the Secret that was created for your identity. | ||
|
|
||
| > Storing sensitive values in appsettings is not recommended. Follow [AspNet Configuration](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-9.0) for best practices. | ||
|
|
||
| 1. Manually update the manifest.json | ||
| - Edit the `manifest.json` contained in the `/appManifest` folder | ||
| - Replace with your AppId (that was created above) *everywhere* you see the place holder string `<<AAD_APP_CLIENT_ID>>` | ||
| - Replace `<<BOT_DOMAIN>>` with your Agent url. For example, the tunnel host name. | ||
| - Zip up the contents of the `/appManifest` folder to create a `manifest.zip` | ||
| 1. Upload the `manifest.zip` to Teams | ||
| - Select **Developer Portal** in the Teams left sidebar | ||
| - Select **Apps** (top row) | ||
| - Select **Import app**, and select the manifest.zip | ||
|
|
||
| 1. Run `dev tunnels`. Please follow [Create and host a dev tunnel](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started?tabs=windows) and host the tunnel with anonymous user access command as shown below: | ||
| > NOTE: Go to your project directory and open the `./Properties/launchSettings.json` file. Check the port number and use that port number in the devtunnel command (instead of 3978). | ||
|
|
||
| ```bash | ||
| devtunnel host -p 3978 --allow-anonymous | ||
| ``` | ||
|
|
||
| 1. On the Azure Bot, select **Settings**, then **Configuration**, and update the **Messaging endpoint** to `{tunnel-url}/api/messages` | ||
|
|
||
| 1. Start the Agent, and select **Preview in Teams** in the upper right corner | ||
|
|
||
| 1. Send a message and the bot will respond with an Echo. | ||
|
|
||
| ## Further reading | ||
| To learn more about building Agents, see our [Microsoft 365 Agents SDK](https://github.com/microsoft/agents) repo. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear what is going on here, Why do you need this?