Skip to content

Commit

Permalink
[C#] feat: add message extension auth sample (#946)
Browse files Browse the repository at this point in the history
## Linked issues

closes: #861

## Details

Add message extension auth example

## Attestation Checklist

- [x] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (we use
[TypeDoc](https://typedoc.org/) to document our code)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes

### Additional information

The sample is based on latest code. So people need to provide a private
build to try the sample.
  • Loading branch information
blackchoey authored Nov 30, 2023
1 parent b156a49 commit 0ac7167
Show file tree
Hide file tree
Showing 30 changed files with 1,543 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dotnet/samples/06.auth.teamsSSO.messageExtension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# VS
.vs

# User-specific files
*.user

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Teams Toolkit
env/.env.*.user
appPackage/build
build
.deployment
# Teams Toolkit Local Development
appsettings.Development.json
env/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.TraceExtensions;
using Microsoft.Bot.Connector.Authentication;

namespace MessageExtensionAuth
{
public class AdapterWithErrorHandler : CloudAdapter
{
public AdapterWithErrorHandler(BotFrameworkAuthentication auth, ILogger<CloudAdapter> logger)
: base(auth, logger)
{
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
// NOTE: In production environment, you should consider logging this to
// Azure Application Insights. Visit https://aka.ms/bottelemetry to see how
// to add telemetry capture to your bot.
logger.LogError(exception, "[OnTurnError] unhandled error : {message}", exception.Message);

// Send a message to the user
await turnContext.SendActivityAsync($"The bot encountered an unhandled error: {exception.Message}");
await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");

// Send a trace activity
await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
};
}
}
}
13 changes: 13 additions & 0 deletions dotnet/samples/06.auth.teamsSSO.messageExtension/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MessageExtensionAuth
{
public class ConfigOptions
{
public string BOT_ID { get; set; }
public string BOT_PASSWORD { get; set; }
public string BOT_DOMAIN { get; set; }
public string AAD_APP_CLIENT_ID { get; set; }
public string AAD_APP_CLIENT_SECRET { get; set; }
public string AAD_APP_TENANT_ID { get; set; }
public string AAD_APP_OAUTH_AUTHORITY_HOST { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;

namespace MessageExtensionAuth.Controllers
{
[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
private readonly CloudAdapter _adapter;
private readonly IBot _bot;

public BotController(CloudAdapter adapter, IBot bot)
{
_adapter = adapter;
_bot = bot;
}

[HttpPost]
public async Task PostAsync(CancellationToken cancellationToken = default)
{
await _adapter.ProcessAsync
(
Request,
Response,
_bot,
cancellationToken
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!-- Use Teams Toolkit Visual Studio Extension for development -->
<ItemGroup>
<ProjectCapability Include="TeamsFx" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AdaptiveCards.Templating" Version="1.4.0" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.21.1" />
<PackageReference Include="Microsoft.Graph" Version="4.54.0" />
<PackageReference Include="Microsoft.Identity.Web.TokenCache" Version="2.16.0" />
<PackageReference Include="Microsoft.Teams.AI" Version="1.0.*-*" />
</ItemGroup>

<!-- Exclude Teams Toolkit files from build output, but can still be viewed from Solution Explorer -->
<ItemGroup>
<Content Remove="appPackage/**/*" />
<None Include="appPackage/**/*" />
<None Include="env/**/*" />
<Content Remove="infra/**/*" />
<None Include="infra/**/*" />
</ItemGroup>

<!-- Exclude local settings from publish -->
<ItemGroup>
<Content Remove="appsettings.Development.json" />
<Content Include="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>None</CopyToPublishDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.33906.173
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessageExtensionAuth", "MessageExtensionAuth.csproj", "{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}.Release|Any CPU.Build.0 = Release|Any CPU
{2523B6D1-DFE1-4512-ADB7-C8084E27A0AE}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {436748E9-F64E-4E1D-9BEE-1AE35954FA4F}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Newtonsoft.Json;

namespace MessageExtensionAuth.Model
{
/// <summary>
/// The strongly typed NuGet package model for Adaptive Card
/// </summary>
public class CardPackage
{
[JsonProperty("id")]
public string? Id { get; set; }

[JsonProperty("version")]
public string? Version { get; set; }

[JsonProperty("description")]
public string? Description { get; set; }

[JsonProperty("tags")]
public string? Tags { get; set; }

[JsonProperty("authors")]
public string? Authors { get; set; }

[JsonProperty("owners")]
public string? Owners { get; set; }

[JsonProperty("licenseUrl")]
public string? LicenseUrl { get; set; }

[JsonProperty("projectUrl")]
public string? ProjectUrl { get; set; }

[JsonProperty("nugetUrl")]
public string? NuGetUrl { get; set; }

public static CardPackage Create(Package package)
{
return new CardPackage
{
Id = package.Id ?? string.Empty,
Version = package.Version ?? string.Empty,
Description = package.Description ?? string.Empty,
Tags = package.Tags == null ? string.Empty : string.Join(", ", package.Tags),
Authors = package.Authors == null ? string.Empty : string.Join(", ", package.Authors),
Owners = package.Owners == null ? string.Empty : string.Join(", ", package.Owners),
LicenseUrl = package.LicenseUrl ?? string.Empty,
ProjectUrl = package.ProjectUrl ?? string.Empty,
NuGetUrl = $"https://www.nuget.org/packages/{package.Id}"
};
}
}
}
40 changes: 40 additions & 0 deletions dotnet/samples/06.auth.teamsSSO.messageExtension/Model/Package.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;

namespace MessageExtensionAuth.Model
{
/// <summary>
/// The strongly typed NuGet package search result
/// </summary>
public class Package
{
[JsonProperty("id")]
public string? Id { get; set; }

[JsonProperty("version")]
public string? Version { get; set; }

[JsonProperty("description")]
public string? Description { get; set; }

[JsonProperty("tags")]
public string[]? Tags { get; set; }

[JsonProperty("authors")]
public string[]? Authors { get; set; }

[JsonProperty("owners")]
public string[]? Owners { get; set; }

[JsonProperty("iconUrl")]
public string? IconUrl { get; set; }

[JsonProperty("licenseUrl")]
public string? LicenseUrl { get; set; }

[JsonProperty("projectUrl")]
public string? ProjectUrl { get; set; }

[JsonProperty("packageTypes")]
public object[]? PackageTypes { get; set; }
}
}
Loading

0 comments on commit 0ac7167

Please sign in to comment.