Skip to content
Open
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
2 changes: 1 addition & 1 deletion core/samples/A2ABot/A2A/A2AServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task ExecuteAsync(RequestContext context, AgentEventQueue eventQueu
.WithServiceUrl(serviceUrl)
.WithConversation(new TeamsConversation { Id = newConvId })
.Build();
SendActivityResponse? sent = await conversations.SendActivityAsync(proactive, cancellationToken: ct);
SendActivityResponse? sent = await conversations.SendActivityAsync(proactive, serviceUrl, cancellationToken: ct);
logger.LogInformation("[{Bot}/A2A] proactive greeting sent (conv={ConvId}, activityId={ActivityId})",
config.Name, newConvId, sent?.Id ?? "<none>");

Expand Down
6 changes: 2 additions & 4 deletions core/samples/AFBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,25 @@

CoreActivity typing = CoreActivity.CreateBuilder()
.WithType(ActivityType.Typing)
.WithServiceUrl(activity.ServiceUrl!)
.WithChannelId(activity.ChannelId!)
.WithConversation(activity.Conversation!)
.WithFrom(activity.Recipient)
.Build();
await botApp.SendActivityAsync(typing, cancellationToken: cancellationToken);
await botApp.SendActivityAsync(typing, activity.ServiceUrl!, cancellationToken: cancellationToken);

AgentRunResponse agentResponse = await agent.RunAsync(activity.Properties["text"]?.ToString() ?? "OMW", cancellationToken: timer.Token);

ChatMessage? m1 = agentResponse.Messages.FirstOrDefault();
Console.WriteLine($"AI:: GOT {agentResponse.Messages.Count} msgs");
CoreActivity replyActivity = CoreActivity.CreateBuilder()
.WithType(ActivityType.Message)
.WithServiceUrl(activity.ServiceUrl!)
.WithChannelId(activity.ChannelId!)
.WithConversation(activity.Conversation!)
.WithFrom(activity.Recipient)
.WithProperty("text", m1!.Text)
.Build();

SendActivityResponse? res = await botApp.SendActivityAsync(replyActivity, cancellationToken: cancellationToken);
SendActivityResponse? res = await botApp.SendActivityAsync(replyActivity, activity.ServiceUrl!, cancellationToken: cancellationToken);

Console.WriteLine("SENT >>> => " + res?.Id);
};
Expand Down
7 changes: 4 additions & 3 deletions core/samples/CompatBot/EchoBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
.WithProperty("text", "Hello TM !")
.WithRecipient(incomingFrom)
.WithFrom(incomingRecipient)
//.WithServiceUrl(activity.ServiceUrl!)
.WithServiceUrl("https://pilot1.botapi.skype.com/amer/9a9b49fd-1dc5-4217-88b3-ecf855e91b0e/")
.Build();

await teamsBotApp.ConversationClient.SendActivityAsync(tm, cancellationToken: cancellationToken);
await teamsBotApp.ConversationClient.SendActivityAsync(
tm,
new Uri("https://pilot1.botapi.skype.com/amer/9a9b49fd-1dc5-4217-88b3-ecf855e91b0e/"),
cancellationToken: cancellationToken);

ResourceResponse res = await turnContext.SendActivityAsync(
MessageFactory.Text("I'm going to add and remove reactions to this message."), cancellationToken);
Expand Down
3 changes: 1 addition & 2 deletions core/samples/CoreBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
CoreActivity replyActivity = CoreActivity.CreateBuilder()
.WithType(ActivityType.Message)
.WithChannelId(activity.ChannelId)
.WithServiceUrl(activity.ServiceUrl)
.WithConversation(activity.Conversation)
.WithFrom(activity.Recipient)
.WithProperty("text", replyText)
.Build();

await botApp.SendActivityAsync(replyActivity, cancellationToken: cancellationToken);
await botApp.SendActivityAsync(replyActivity, activity.ServiceUrl!, cancellationToken: cancellationToken);
};

webApp.Run();
6 changes: 3 additions & 3 deletions core/samples/McpServer/McpTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<NotifyResult> Notify(
.WithConversation(new Conversation(conversationId))
.WithText(message)
.Build();
await app.SendActivityAsync(notifyActivity, cancellationToken);
await app.SendActivityAsync(notifyActivity, state.ServiceUrl, cancellationToken);
return new NotifyResult(Notified: true, UserId: userId);
}

Expand All @@ -53,7 +53,7 @@ public async Task<AskResult> Ask(
.Build();
try
{
await app.SendActivityAsync(askActivity, cancellationToken);
await app.SendActivityAsync(askActivity, state.ServiceUrl, cancellationToken);
}
catch
{
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task<ApprovalRequestResult> RequestApproval(
state.Approvals[approvalId] = ApprovalStatus.Pending;
try
{
await app.SendActivityAsync(activity, cancellationToken);
await app.SendActivityAsync(activity, state.ServiceUrl, cancellationToken);
}
catch
{
Expand Down
3 changes: 1 addition & 2 deletions core/samples/Proactive/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
if (logger.IsEnabled(LogLevel.Information))
{
CoreActivity proactiveMessage = CoreActivity.CreateBuilder()
.WithServiceUrl(new Uri(ServiceUrl))
.WithConversation(new(ConversationId))
.Build();
proactiveMessage.From = new ChannelAccount { Id = FromId };
proactiveMessage.Properties["text"] = $"Proactive hello at {DateTimeOffset.Now}";
SendActivityResponse? aid = await conversationClient.SendActivityAsync(proactiveMessage, cancellationToken: stoppingToken);
SendActivityResponse? aid = await conversationClient.SendActivityAsync(proactiveMessage, new Uri(ServiceUrl), cancellationToken: stoppingToken);
logger.LogInformation("Activity {Aid} sent", aid?.Id ?? "unknown");
}
await Task.Delay(1000, stoppingToken);
Expand Down
2 changes: 1 addition & 1 deletion core/samples/TabApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
.WithServiceUrl(serviceUrl)
.WithConversation(new TeamsConversation { Id = conversationId! })
.Build();
await conversations.SendActivityAsync(activity, cancellationToken: ct);
await conversations.SendActivityAsync(activity, serviceUrl, cancellationToken: ct);

return Results.Json(new PostToChatResult(Ok: true));
}).RequireAuthorization();
Expand Down
2 changes: 1 addition & 1 deletion core/samples/TeamsBot/WelcomeMessageMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task OnTurnAsync(BotApplication botApplication, CoreActivity activi
.WithConversationReference(TeamsActivity.FromActivity(activity))
.Build();

await botApplication.SendActivityAsync(welcomeActivity, cancellationToken: cancellationToken);
await botApplication.SendActivityAsync(welcomeActivity, activity.ServiceUrl!, cancellationToken);

_hasSentWelcomeMessage = true;
}
Expand Down
37 changes: 17 additions & 20 deletions core/src/Microsoft.Teams.Apps.BotBuilder/CompatConversations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,10 @@ public async Task<HttpOperationResponse<ResourceResponse>> ReplyToActivityWithHt

CoreActivity coreActivity = activity.FromBotFrameworkActivity();

// Default to the ServiceUrl from the adapter if it's not set on the activity, as ConversationClient requires it for sending activities
if (!string.IsNullOrWhiteSpace(ServiceUrl) && coreActivity.ServiceUrl == null)
{
coreActivity.ServiceUrl = new Uri(ServiceUrl);
}

coreActivity.ReplyToId = activityId;
coreActivity.Conversation = new Microsoft.Teams.Core.Schema.Conversation(conversationId);

SendActivityResponse? response = await _client.SendActivityAsync(coreActivity, requestContext: RequestContext, customHeaders: convertedHeaders, cancellationToken: cancellationToken).ConfigureAwait(false);
SendActivityResponse? response = await _client.SendActivityAsync(coreActivity, ResolveActivityServiceUrl(activity, "reply to activities"), requestContext: RequestContext, customHeaders: convertedHeaders, cancellationToken: cancellationToken).ConfigureAwait(false);

ResourceResponse resourceResponse = new()
{
Expand Down Expand Up @@ -307,15 +301,9 @@ public async Task<HttpOperationResponse<ResourceResponse>> SendToConversationWit

CoreActivity coreActivity = activity.FromBotFrameworkActivity();

// Default to the ServiceUrl from the adapter if it's not set on the activity, as ConversationClient requires it for sending activities
if (!string.IsNullOrWhiteSpace(ServiceUrl) && coreActivity.ServiceUrl == null)
{
coreActivity.ServiceUrl = new Uri(ServiceUrl);
}

coreActivity.Conversation = new Microsoft.Teams.Core.Schema.Conversation(conversationId);

SendActivityResponse? response = await _client.SendActivityAsync(coreActivity, requestContext: RequestContext, customHeaders: convertedHeaders, cancellationToken: cancellationToken).ConfigureAwait(false);
SendActivityResponse? response = await _client.SendActivityAsync(coreActivity, ResolveActivityServiceUrl(activity, "send activities"), requestContext: RequestContext, customHeaders: convertedHeaders, cancellationToken: cancellationToken).ConfigureAwait(false);

ResourceResponse resourceResponse = new()
{
Expand Down Expand Up @@ -347,16 +335,11 @@ public async Task<HttpOperationResponse<ResourceResponse>> UpdateActivityWithHtt

CoreActivity coreActivity = activity.FromBotFrameworkActivity();

// Default to the ServiceUrl from the adapter if it's not set on the activity, as ConversationClient requires it for updating activities
if (!string.IsNullOrWhiteSpace(ServiceUrl) && coreActivity.ServiceUrl == null)
{
coreActivity.ServiceUrl = new Uri(ServiceUrl);
}

UpdateActivityResponse response = await _client.UpdateActivityAsync(
conversationId,
activityId,
coreActivity,
ResolveActivityServiceUrl(activity, "update activities"),
requestContext: RequestContext,
customHeaders: convertedHeaders,
cancellationToken: cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -422,6 +405,20 @@ public async Task<HttpOperationResponse<ResourceResponse>> UploadAttachmentWithH
return convertedHeaders;
}

private Uri ResolveActivityServiceUrl(Activity activity, string operation)
{
string? serviceUrl = !string.IsNullOrWhiteSpace(activity.ServiceUrl)
? activity.ServiceUrl
: ServiceUrl;

if (string.IsNullOrWhiteSpace(serviceUrl))
{
throw new InvalidOperationException($"ServiceUrl is required to {operation}.");
}

return new Uri(serviceUrl);
}

public async Task<HttpOperationResponse<Microsoft.Bot.Schema.ChannelAccount>> GetConversationMemberWithHttpMessagesAsync(string userId, string conversationId, Dictionary<string, List<string>> customHeaders = null!, CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(ServiceUrl);
Expand Down
20 changes: 14 additions & 6 deletions core/src/Microsoft.Teams.Apps.BotBuilder/TeamsBotAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ public override async Task<ResourceResponse[]> SendActivitiesAsync(ITurnContext

CoreActivity coreActivity = activity.FromBotFrameworkActivity();

// Ensure ServiceUrl is set from turn context if not already present
if (coreActivity.ServiceUrl == null && !string.IsNullOrWhiteSpace(inboundActivity.ServiceUrl))
string? serviceUrlString = !string.IsNullOrWhiteSpace(activity.ServiceUrl)
? activity.ServiceUrl
: inboundActivity.ServiceUrl;
if (string.IsNullOrWhiteSpace(serviceUrlString))
{
coreActivity.ServiceUrl = new Uri(inboundActivity.ServiceUrl);
throw new InvalidOperationException("ServiceUrl is required to send activities.");
}
Uri serviceUrl = new(serviceUrlString);

coreActivity.Conversation ??= new Microsoft.Teams.Core.Schema.Conversation(
inboundActivity.Conversation?.Id
?? throw new InvalidOperationException("Conversation ID is required to send activities."));
SendActivityResponse? resp = await botApplication.ConversationClient.SendActivityAsync(
coreActivity,
serviceUrl,
requestContext,
cancellationToken: cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -143,16 +147,20 @@ public override async Task<ResourceResponse> UpdateActivityAsync(ITurnContext tu

CoreActivity coreActivity = activity.FromBotFrameworkActivity();

// Ensure ServiceUrl is set from turn context if not already present
if (coreActivity.ServiceUrl == null && !string.IsNullOrWhiteSpace(turnContext.Activity.ServiceUrl))
string? serviceUrlString = !string.IsNullOrWhiteSpace(activity.ServiceUrl)
? activity.ServiceUrl
: turnContext.Activity.ServiceUrl;
if (string.IsNullOrWhiteSpace(serviceUrlString))
{
coreActivity.ServiceUrl = new Uri(turnContext.Activity.ServiceUrl);
throw new InvalidOperationException("ServiceUrl is required to update activities.");
}
Uri serviceUrl = new(serviceUrlString);

UpdateActivityResponse res = await botApplication.ConversationClient.UpdateActivityAsync(
activity.Conversation.Id,
activity.Id,
coreActivity,
serviceUrl,
requestContext: BotRequestContext.FromInboundActivity(turnContext.Activity?.FromBotFrameworkActivity()),
cancellationToken: cancellationToken).ConfigureAwait(false);
return new ResourceResponse() { Id = res.Id };
Expand Down
15 changes: 5 additions & 10 deletions core/src/Microsoft.Teams.Apps/Api/Clients/ActivityClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ internal ActivityClient(Uri serviceUrl, CoreConversationClient client)
public Task<SendActivityResponse?> CreateAsync(string conversationId, CoreActivity activity, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(activity);
activity.ServiceUrl ??= _serviceUrl;
activity.Conversation ??= new Conversation(conversationId);
return _client.SendActivityAsync(activity, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
return _client.SendActivityAsync(activity, _serviceUrl, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
}

/// <summary>
Expand All @@ -43,8 +42,7 @@ internal ActivityClient(Uri serviceUrl, CoreConversationClient client)
public Task<UpdateActivityResponse> UpdateAsync(string conversationId, string id, CoreActivity activity, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(activity);
activity.ServiceUrl ??= _serviceUrl;
return _client.UpdateActivityAsync(conversationId, id, activity, requestContext: BotRequestContext.FromActivity(activity), customHeaders: additionalHeaders, cancellationToken: cancellationToken);
return _client.UpdateActivityAsync(conversationId, id, activity, _serviceUrl, requestContext: BotRequestContext.FromActivity(activity), customHeaders: additionalHeaders, cancellationToken: cancellationToken);
}

/// <summary>
Expand All @@ -54,9 +52,8 @@ public Task<UpdateActivityResponse> UpdateAsync(string conversationId, string id
{
ArgumentNullException.ThrowIfNull(activity);
activity.ReplyToId = id;
activity.ServiceUrl ??= _serviceUrl;
activity.Conversation ??= new Conversation(conversationId);
return _client.SendActivityAsync(activity, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
return _client.SendActivityAsync(activity, _serviceUrl, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
}

/// <summary>
Expand All @@ -75,14 +72,13 @@ public Task DeleteAsync(string conversationId, string id, AgenticIdentity? agent
public Task<SendActivityResponse?> CreateTargetedAsync(string conversationId, CoreActivity activity, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(activity);
activity.ServiceUrl ??= _serviceUrl;
activity.Conversation ??= new Conversation(conversationId);
// Ensure recipient is marked as targeted
if (activity.Recipient is not null)
{
activity.Recipient.IsTargeted = true;
}
return _client.SendActivityAsync(activity, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
return _client.SendActivityAsync(activity, _serviceUrl, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
}

/// <summary>
Expand All @@ -92,8 +88,7 @@ public Task DeleteAsync(string conversationId, string id, AgenticIdentity? agent
public Task<UpdateActivityResponse> UpdateTargetedAsync(string conversationId, string id, CoreActivity activity, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(activity);
activity.ServiceUrl ??= _serviceUrl;
return _client.UpdateTargetedActivityAsync(conversationId, id, activity, requestContext: BotRequestContext.FromActivity(activity), customHeaders: additionalHeaders, cancellationToken: cancellationToken);
return _client.UpdateTargetedActivityAsync(conversationId, id, activity, _serviceUrl, requestContext: BotRequestContext.FromActivity(activity), customHeaders: additionalHeaders, cancellationToken: cancellationToken);
}

/// <summary>
Expand Down
Loading
Loading