gpt4 book ai didi

c# - 我的团队机器人如何启动新的 1 :1 chat with a known user

转载 作者:太空宇宙 更新时间:2023-11-03 22:31:57 25 4
gpt4 key购买 nike

我正在开发一个 Teams 机器人,它需要能够与已知用户(即,我们知道 Teams 用户 ID)开始新的一对一对话。

我查看了 GitHub (https://github.com/OfficeDev/microsoft-teams-sample-complete-csharp) 上的“complete-csharp”OfficeDev 示例以及 Graph API 的 Teams 相关部分,但我没有看到任何开始新对话的可供性。

我们的目标是通过邀请已知用户进行一对一聊天并征求他们的反馈,让我们的机器人按计划与他们联系。机器人消息中的按钮将显示反馈表(通过任务模块)。

最佳答案

更新

Bot Framework 添加了特定于 Teams 的代码,这使得此答案中的很多代码没有实际意义或不正确。参见 this sample用于在 Teams 中发送主动消息,现在。

Teams 称之为“主动消息”。只要您获得 Teams 使用的用户 ID,就很容易做到。

根据文档,Proactive messaging for bots :

Bots can create new conversations with an individual Microsoft Teams user as long as your bot has user information obtained through previous addition in a personal, groupChat or team scope. This information enables your bot to proactively notify them. For instance, if your bot was added to a team, it could query the team roster and send users individual messages in personal chats, or a user could @mention another user to trigger the bot to send that user a direct message.

最简单的方法是通过 Microsoft.Bot.Builder.Teams中间件。

注意:Microsoft.Bot.Builder.Teams扩展仍在 V4 的预发行版中,这就是为什么很难找到它的示例和代码的原因。

添加中间件

Startup.cs :

var credentials = new SimpleCredentialProvider(Configuration["MicrosoftAppId"], Configuration["MicrosoftAppPassword"]);

services.AddSingleton(credentials);

[...]

services.AddBot<YourBot>(options =>
{
options.CredentialProvider = credentials;

options.Middleware.Add(
new TeamsMiddleware(
new ConfigurationCredentialProvider(this.Configuration)));
[...]

准备你的机器人

在你的主<YourBot>.cs :

private readonly SimpleCredentialProvider _credentialProvider;

[...]

public <YourBot>(ConversationState conversationState, SimpleCredentialProvider CredentialProvider)
{
_credentialProvider = CredentialProvider;

[...]

发送消息

var teamConversationData = turnContext.Activity.GetChannelData<TeamsChannelData>();
var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl), _credentialProvider.AppId, _credentialProvider.Password);

var userId = <UserIdToSendTo>;
var tenantId = teamConversationData.Tenant.Id;
var parameters = new ConversationParameters
{
Members = new[] { new ChannelAccount(userId) },
ChannelData = new TeamsChannelData
{
Tenant = new TenantInfo(tenantId),
},
};

var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
var message = Activity.CreateMessageActivity();
message.Text = "This is a proactive message.";
await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)message);

注意:如果需要获取用户ID,可以使用:

var members = (await turnContext.TurnState.Get<IConnectorClient>().Conversations.GetConversationMembersAsync(
turnContext.Activity.GetChannelData<TeamsChannelData>().Team.Id).ConfigureAwait(false)).ToList();

另外,我在测试中不需要这个,但是如果你得到 401 错误,你可能需要 trust the Teams ServiceUrl :

MicrosoftAppCredentials.TrustServiceUrl(turnContext.Activity.ServiceUrl); 

资源

关于c# - 我的团队机器人如何启动新的 1 :1 chat with a known user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57120193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com