Skip to content
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

[Teams] adding helper to send messages to channel #3292

Merged
merged 3 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions libraries/Microsoft.Bot.Builder/Teams/TeamsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Connector.Teams;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -49,6 +51,54 @@ public static Task<IEnumerable<TeamsChannelAccount>> GetMembersAsync(ITurnContex
}
}

public static async Task<Tuple<ConversationReference, string>> SendMessageToTeamsChannelAsync(ITurnContext turnContext, IActivity activity, string teamsChannelId, MicrosoftAppCredentials credentials, CancellationToken cancellationToken = default)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}

if (turnContext.Activity == null)
{
throw new ArgumentNullException(nameof(turnContext.Activity));
}

if (string.IsNullOrEmpty(teamsChannelId))
{
throw new ArgumentNullException(nameof(teamsChannelId));
}

if (credentials == null)
{
throw new ArgumentNullException(nameof(credentials));
}

ConversationReference conversationReference = null;
var newActivityId = string.Empty;
var serviceUrl = turnContext.Activity.ServiceUrl;
var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new { channel = new { id = teamsChannelId } },
Activity = (Activity)activity,
};

await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
teamsChannelId,
serviceUrl,
credentials,
conversationParameters,
(t, ct) =>
{
conversationReference = t.Activity.GetConversationReference();
newActivityId = t.Activity.Id;
return Task.CompletedTask;
},
cancellationToken).ConfigureAwait(false);

return new Tuple<ConversationReference, string>(conversationReference, newActivityId);
}

private static async Task<IEnumerable<TeamsChannelAccount>> GetMembersAsync(IConnectorClient connectorClient, string conversationId, CancellationToken cancellationToken)
{
if (conversationId == null)
Expand Down
62 changes: 61 additions & 1 deletion tests/Microsoft.Bot.Builder.Tests/Teams/TeamsInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ namespace Microsoft.Bot.Builder.Teams.Tests
[TestClass]
public class TeamsInfoTests
{
[TestMethod]
public async Task TestSendMessageToTeamsChannelAsync()
{
var baseUri = new Uri("https://test.coffee");
var customHttpClient = new HttpClient(new RosterHttpMessageHandler(), false);

// Set a special base address so then we can make sure the connector client is honoring this http client
customHttpClient.BaseAddress = baseUri;
var connectorClient = new ConnectorClient(new Uri("https://test.coffee"), new MicrosoftAppCredentials("big-guid-here", "appPasswordHere"), customHttpClient);

var activity = new Activity
{
Type = "message",
Text = "Test-SendMessageToTeamsChannelAsync",
ChannelId = Channels.Msteams,
ChannelData = new TeamsChannelData
{
Team = new TeamInfo
{
Id = "team-id",
},
},
};

var turnContext = new TurnContext(new BotFrameworkAdapter(new SimpleCredentialProvider("big-guid-here", "appPasswordHere"), customHttpClient: customHttpClient), activity);
turnContext.TurnState.Add<IConnectorClient>(connectorClient);
turnContext.Activity.ServiceUrl = "https://test.coffee";
var handler = new TestTeamsActivityHandler();
await handler.OnTurnAsync(turnContext);
}

[TestMethod]
public async Task TestGetTeamDetailsAsync()
{
Expand Down Expand Up @@ -129,11 +160,11 @@ public async Task TestGetChannelsAsync()
Id = "team-id",
},
},
ServiceUrl = "https://test.coffee",
};

var turnContext = new TurnContext(new SimpleAdapter(), activity);
turnContext.TurnState.Add<IConnectorClient>(connectorClient);

var handler = new TestTeamsActivityHandler();
await handler.OnTurnAsync(turnContext);
}
Expand All @@ -158,12 +189,29 @@ public override async Task OnTurnAsync(ITurnContext turnContext, CancellationTok
case "Test-GetChannelsAsync":
await CallGetChannelsAsync(turnContext);
break;
case "Test-SendMessageToTeamsChannelAsync":
await CallSendMessageToTeamsChannelAsync(turnContext);
break;
default:
Assert.IsTrue(false);
break;
}
}

private async Task CallSendMessageToTeamsChannelAsync(ITurnContext turnContext)
{
var message = MessageFactory.Text("hi");
var channelId = "channelId123";
var creds = new MicrosoftAppCredentials("big-guid-here", "appPasswordHere");
var cancelToken = new CancellationToken();
var reference = await TeamsInfo.SendMessageToTeamsChannelAsync(turnContext, message, channelId, creds, cancelToken);

Assert.AreEqual(reference.Item1.ActivityId, "activityId123");
Assert.AreEqual(reference.Item1.ChannelId, "channelId123");
Assert.AreEqual(reference.Item1.ServiceUrl, "https://test.coffee");
Assert.AreEqual(reference.Item2, "activityId123");
}

private async Task CallGetTeamDetailsAsync(ITurnContext turnContext)
{
var teamDetails = await TeamsInfo.GetTeamDetailsAsync(turnContext);
Expand Down Expand Up @@ -239,6 +287,18 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
response.Content = new StringContent(content.ToString());
}

// SendMessageToThreadInTeams
else if (request.RequestUri.PathAndQuery.EndsWith("v3/conversations"))
{
var content = new JObject
{
new JProperty("id", "id123"),
new JProperty("serviceUrl", "https://serviceUrl/"),
new JProperty("activityId", "activityId123")
};
response.Content = new StringContent(content.ToString());
}

// GetChannels
else if (request.RequestUri.PathAndQuery.EndsWith("team-id/conversations"))
{
Expand Down
48 changes: 11 additions & 37 deletions tests/Teams/ReplyToChannel/Bots/ReplyToChannel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -28,34 +29,8 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
{
var teamsChannelId = turnContext.Activity.TeamsGetChannelId();
var message = MessageFactory.Text("good morning");

var serviceUrl = turnContext.Activity.ServiceUrl;
var credentials = new MicrosoftAppCredentials(_appId, _appPassword);

// there are two scenarios for create conversation

// (1) starting a thread within a channel

var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new { channel = new { id = teamsChannelId } },
Activity = (Activity)message,
};

ConversationReference conversationReference = null;

await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
teamsChannelId,
serviceUrl,
credentials,
conversationParameters,
(t, ct) =>
{
conversationReference = t.Activity.GetConversationReference();
return Task.CompletedTask;
},
cancellationToken);
var creds = new MicrosoftAppCredentials(this._appId, this._appPassword);
Tuple<ConversationReference, string> tuple = await TeamsInfo.SendMessageToTeamsChannelAsync(turnContext, message, teamsChannelId, creds);

//// (2) starting a one on one chat

Expand Down Expand Up @@ -84,15 +59,14 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
// },
// cancellationToken);

//await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
// _appId,
// conversationReference,
// async (t, ct) =>
// {
// await t.SendActivityAsync(MessageFactory.Text("good afternoon"), ct);
// await t.SendActivityAsync(MessageFactory.Text("good night"), ct);
// },
// cancellationToken);
await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
this._appId,
tuple.Item1,
async (t, ct) =>
{
await t.SendActivityAsync(MessageFactory.Text($"this will be the first reply. The Activity.Id of the top level message is {tuple.Item2}"), ct);
},
cancellationToken);
}
}
}