我通過 Teams 在我的組織中安裝了 Azure Bot。與機器人的互動運行正常。
我們有一個場景,我們需要從外部行程(在 Azure 中運行的 C# 應用程式)向我們的用戶發送通知。
我嘗試使用Bot Framework REST API創建與用戶的對話,然后向他們發送此處概述的通知
此方案不起作用,因為我無法獲取未使用全域 Bot Framework 租戶的機器人的訪問令牌。我們的 Bot 作為 SingleTenant BotType 安裝在 Azure 租戶上,因此出現以下錯誤:
Application with identifier 'BOT-APP-ID' was not found in the directory 'Bot Framework'
我傾注了各種選擇,包括 DirectLine 通道、BotConnector SDK 和 Power Automate - 但似乎沒有什么適合我的需要。
有沒有辦法針對我們安裝的 Bot 使用 Rest API 來創建對話和發送訊息?這將是理想的,因為這樣我就可以直接從觸發它們的事件中啟動這些通知。
更新
希爾頓在下面的回答為這種情況帶來了一些急需的澄清。最終,我不得不部署一個新的 Bot 集來使用Multitenant BotType。
這配置了一個系結到 Bot 集的 AppRegistration 以使用 MultiTenant。
此外,在托管api/messages端點的 WebApp 中,您必須包含一個名為MicrosoftAppType的屬性,并將其設定為MultiTenant。以下是 WebApp 所需的完整配置:
MicrosoftAppId: [The AppId of the AppRegistration]
MicrosoftAppPassword: [The Secret of the AppRegistration]
MicrosoftAppTenantId: [Your Tenant Id]
MicrosoftAppType: MultiTenant
為了捕獲 ConversationId、ServiceUrl 和 UserId,我將以下內容添加到OnMembersAddedAsync
foreach (var member in membersAdded)
{
if (!string.IsNullOrEmpty(member.AadObjectId))
{
//This is a user in our AAD. Store the conversation id reference:
var userId = member.AadObjectId;
var serviceUrl = turnContext.Activity.ServiceUrl;
var conversationId = turnContext.Activity.Conversation.Id;
//Save variables to your state store here...
}
else
{
// This is likely a test outside of Teams
//var userId = member.Id; //<-- Not used in my scenario
}
}
然后在我的外部應用程式(本例中的控制臺)中,我可以使用這些變數(連同 AppRegistration 憑據)創建一個 Bot ConnectorClient 并更新對話:
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
var botAppId = "[BOT_ID/APP_REG_ID]";
var botAppKey = "[APP_REG_SECRET]";
var conversationId = "[CONVERSATION_ID]";
var serviceUrl = "[SERVICE_URL]";
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
var connector = new Microsoft.Bot.Connector.ConnectorClient(new Uri(serviceUrl), botAppId, botAppKey);
var activity = new Activity()
{
Text = "Proactively saying **Hello**",
Type = ActivityTypes.Message,
Conversation = new ConversationAccount(false, "personal", conversationId)
};
try
{
var result = await connector.Conversations.SendToConversationAsync(conversationId, activity);
Console.WriteLine("Notification sent!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
uj5u.com熱心網友回復:
主動訊息傳遞絕對是您要尋找的,但有一些重要的事情需要注意。這是一個示例https://github.com/pnp/teams-dev-samples/tree/main/samples/bot-proactive-messaging希望有用 - 我包括 C# 和 Node 版本以及一些進一步閱讀的鏈接,這里是一個視頻會議的鏈接,我在其中更多地討論了這個概念:https ://www.youtube.com/watch?v=mM7-fYdcJhw&t=1398s 。
簡而言之,請記住 Bot Framework 可以在許多情況下使用,Teams 只是其中之一。重要的是,與其他背景關系不同,當你在 Teams 中時,沒有“創建”與用戶對話的概念。只有一次“對話”,而您基本上是在“繼續”對話。結果,您想呼叫continueConversation. 在我上面鏈接的同一個示例中,這里是相關行。在幕后,這確實是在呼叫一個 REST API,但這樣包裝起來更容易。
但是,如示例所示,由于您無法開始對話,只能繼續對話,因此您需要確保已經擁有對話背景關系,這也可能意味著確保用戶已將機器人安裝到個人背景(實際上是開始對話的內容)。這是示例中發生這種情況的地方。
如果您的用戶已經安裝了機器人,那么它只是存盤對話背景關系的一種情況,就像我在示例中顯示的那樣。如果沒有,并且您想了解如何預安裝機器人,請參閱以下問題:Proactively Install / Push Apps in Teams for Multiple Users
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446802.html
標籤:天蓝色 机器人框架 微软团队 天蓝色机器人服务 微软图形团队
