From a7ef4d770dde5cba7b8ac5c19645563b0746e1f1 Mon Sep 17 00:00:00 2001 From: Eric Dahlvang Date: Tue, 29 Jun 2021 09:38:38 -0700 Subject: [PATCH] Update SendMessageToTeamsChannelAsync to work with CloudAdapter --- .../Actions/SendMessageToTeamsChannel.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/Teams/dotnet/Actions/SendMessageToTeamsChannel.cs b/packages/Teams/dotnet/Actions/SendMessageToTeamsChannel.cs index 912b8beecf..5aa027bba1 100644 --- a/packages/Teams/dotnet/Actions/SendMessageToTeamsChannel.cs +++ b/packages/Teams/dotnet/Actions/SendMessageToTeamsChannel.cs @@ -3,6 +3,7 @@ using System; using System.Runtime.CompilerServices; +using System.Security.Claims; using System.Threading; using System.Threading.Tasks; using AdaptiveExpressions.Properties; @@ -118,22 +119,19 @@ public SendMessageToTeamsChannel([CallerFilePath] string callerPath = "", [Calle teamsChannelId = dc.Context.Activity.TeamsGetChannelId(); } - if (!(dc.Context.Adapter is BotFrameworkAdapter)) + // Retrieve the bot appid from TurnState's ClaimsIdentity + string appId; + if (dc.Context.TurnState.Get(BotAdapter.BotIdentityKey) is ClaimsIdentity botIdentity) { - throw new InvalidOperationException($"{Kind} is not supported by the current adapter."); + appId = JwtTokenValidation.GetAppIdFromClaims(botIdentity.Claims); } - - // TODO: this will NOT work with certificate app credentials - - // TeamsInfo.SendMessageToTeamsChannelAsync requires AppCredentials - var credentials = dc.Context.TurnState.Get()?.Credentials as MicrosoftAppCredentials; - if (credentials == null) + else { - throw new InvalidOperationException($"Missing credentials as {nameof(MicrosoftAppCredentials)} in {nameof(IConnectorClient)} from TurnState"); + throw new InvalidOperationException($"Missing {BotAdapter.BotIdentityKey} in {nameof(ITurnContext)} TurnState"); } // The result comes back as a tuple, which is used to set the two properties (if present). - var result = await TeamsInfo.SendMessageToTeamsChannelAsync(dc.Context, activity, teamsChannelId, credentials, cancellationToken: cancellationToken).ConfigureAwait(false); + var result = await TeamsInfo.SendMessageToTeamsChannelAsync(dc.Context, activity, teamsChannelId, appId, cancellationToken: cancellationToken).ConfigureAwait(false); if (ConversationReferenceProperty != null) {