【问题标题】:How can my Teams bot start a new 1:1 chat with a known user我的 Teams 机器人如何与已知用户开始新的 1:1 聊天
【发布时间】:2020-03-11 01:10:04
【问题描述】:

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

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

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

【问题讨论】:

  • 是的!非常感谢您提供全面而详细的回答。

标签: c# botframework microsoft-teams


【解决方案1】:

更新

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

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

根据文档,Proactive messaging for bots

只要您的 bot 具有通过先前添加到个人、groupChat 或团队范围中获得的用户信息,Bots 就可以与单个 Microsoft Teams 用户创建新对话。此信息使您的机器人能够主动通知他们。例如,如果您的机器人被添加到团队中,它可以查询团队名册并在个人聊天中向用户发送个人消息,或者用户可以@提及另一个用户以触发机器人向该用户发送直接消息。

最简单的方法是通过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)));
[...]

准备你的机器人

在你的主要&lt;YourBot&gt;.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); 

资源

【讨论】:

  • 什么是 TeamsChannelData,它是用户定义的类还是我们需要任何库来实现这一点?
  • @VikashRanjanJha 自去年 Bot Framework 开始为 Teams 开发特定适配器以来,此答案已过时。现在,请参阅 this sample 在 Teams 中发送主动消息。
  • @mdrichardson-MSFT - 您使用新的 GetSingleMemberAsync 方法引用的样本是否有助于从外部系统/事件发送主动消息,但是否正确?我一直在使用主动消息sample。是否有更新的方法可以从外部系统向 Teams 中的用户(已知其 Teams ID)发送主动消息?可能不需要构建 ConversationReference 对象?
  • @RyanBuening master 分支中的样本是我们获得的最新版本。您可以通过某种外部事件发送它,但您需要手动构建 ConversationReference 对象。为此,您可能需要先get a list of your team membersThis answer 可能会对此有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2022-12-11
相关资源
最近更新 更多