Skip to content

Commit

Permalink
Merge pull request #882 from OfficeDev/v-pritka/proactive-message-usi…
Browse files Browse the repository at this point in the history
…ng-aadId

Added support to send proactive messages using aad id
  • Loading branch information
Prithvi-MSFT authored Aug 2, 2023
2 parents 3ed0d87 + 1b3f2e0 commit 5e4a927
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
66 changes: 41 additions & 25 deletions samples/bot-conversation/csharp/Bots/TeamsConversationBot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -49,8 +49,10 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
await GetSingleMemberAsync(turnContext, cancellationToken);
else if (text.Contains("update"))
await CardActivityAsync(turnContext, true, cancellationToken);
else if (text.Contains("aadid"))
await MessageAllMembersAsync(turnContext, cancellationToken, true);
else if (text.Contains("message"))
await MessageAllMembersAsync(turnContext, cancellationToken);
await MessageAllMembersAsync(turnContext, cancellationToken, false);
else if (text.Contains("immersivereader"))
await SendImmersiveReaderCardAsync(turnContext, cancellationToken);
else if (text.Contains("delete"))
Expand All @@ -63,7 +65,7 @@ protected override async Task OnTeamsMembersAddedAsync(IList<TeamsChannelAccount
{
foreach (var teamMember in membersAdded)
{
if(teamMember.Id != turnContext.Activity.Recipient.Id && turnContext.Activity.Conversation.ConversationType != "personal")
if (teamMember.Id != turnContext.Activity.Recipient.Id && turnContext.Activity.Conversation.ConversationType != "personal")
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Welcome to the team {teamMember.GivenName} {teamMember.Surname}."), cancellationToken);
}
Expand All @@ -72,7 +74,7 @@ protected override async Task OnTeamsMembersAddedAsync(IList<TeamsChannelAccount

protected override async Task OnInstallationUpdateActivityAsync(ITurnContext<IInstallationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
if(turnContext.Activity.Conversation.ConversationType == "channel")
if (turnContext.Activity.Conversation.ConversationType == "channel")
{
await turnContext.SendActivityAsync($"Welcome to Microsoft Teams conversationUpdate events demo bot. This bot is configured in {turnContext.Activity.Conversation.Name}");
}
Expand All @@ -96,6 +98,12 @@ private async Task CardActivityAsync(ITurnContext<IMessageActivity> turnContext,
Text = "MessageAllMembers"
},
new CardAction
{
Type = ActionTypes.MessageBack,
Title = "Message all members using AADId",
Text = "MessageAllMembersUsingAADId"
},
new CardAction
{
Type = ActionTypes.MessageBack,
Title = "Who am I?",
Expand Down Expand Up @@ -165,7 +173,7 @@ private async Task DeleteCardActivityAsync(ITurnContext<IMessageActivity> turnCo
await turnContext.DeleteActivityAsync(turnContext.Activity.ReplyToId, cancellationToken);
}

private async Task MessageAllMembersAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
private async Task MessageAllMembersAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken, bool isAadId)
{
var teamsChannelId = turnContext.Activity.TeamsGetChannelId();
var serviceUrl = turnContext.Activity.ServiceUrl;
Expand All @@ -182,29 +190,37 @@ private async Task MessageAllMembersAsync(ITurnContext<IMessageActivity> turnCon
{
IsGroup = false,
Bot = turnContext.Activity.Recipient,
Members = new ChannelAccount[] { teamMember },
Members = isAadId ? new ChannelAccount[] { new ChannelAccount(teamMember.AadObjectId) } : new ChannelAccount[] { teamMember },
TenantId = turnContext.Activity.Conversation.TenantId,
};
try
{
await ((CloudAdapter)turnContext.Adapter).CreateConversationAsync(
credentials.MicrosoftAppId,
teamsChannelId,
serviceUrl,
credentials.OAuthScope,
conversationParameters,
async (t1, c1) =>
{
conversationReference = t1.Activity.GetConversationReference();
await ((CloudAdapter)turnContext.Adapter).ContinueConversationAsync(
_appId,
conversationReference,
async (t2, c2) =>
{
await t2.SendActivityAsync(proactiveMessage, c2);
},
cancellationToken);
},
cancellationToken);
}
catch (Exception e)
{
Console.WriteLine(e);
}


await ((CloudAdapter)turnContext.Adapter).CreateConversationAsync(
credentials.MicrosoftAppId,
teamsChannelId,
serviceUrl,
credentials.OAuthScope,
conversationParameters,
async (t1, c1) =>
{
conversationReference = t1.Activity.GetConversationReference();
await ((CloudAdapter)turnContext.Adapter).ContinueConversationAsync(
_appId,
conversationReference,
async (t2, c2) =>
{
await t2.SendActivityAsync(proactiveMessage, c2);
},
cancellationToken);
},
cancellationToken);
}

await turnContext.SendActivityAsync(MessageFactory.Text("All messages have been sent."), cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"accentColor": "#FFFFFF",
"bots": [
{
"id": "<<YOUR-MICROSOFT-APP-ID>>",
"botId": "<<YOUR-MICROSOFT-APP-ID>>",
"scopes": [
"personal",
"groupchat",
Expand Down Expand Up @@ -54,6 +54,10 @@
{
"title": "MessageAllMembers",
"description": "Send 1 to 1 message to all members of the current conversation"
},
{
"title": "MessageAllMembersUsingAadId",
"description": "Send 1 to 1 message to all members of the current conversation using their AADId"
}
]
}
Expand All @@ -65,4 +69,4 @@
"messageTeamMembers"
],
"validDomains": [ "*.ngrok-free.app" ]
}
}

0 comments on commit 5e4a927

Please sign in to comment.