温馨提示:本文翻译自stackoverflow.com,查看原文请点击:botframework - Calling my .NET Core Teams Bot from Angular
botframework microsoft-teams

botframework - 从Angular给我的.NET Core团队打电话

发布于 2020-04-09 10:25:44

我已经按照以下示例在.NET Core中创建了一个Teams机器人:https : //github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/57.teams-conversation-bot

这正在工作,并且正在使用ngrok在本地运行。我有一个带有api / messages路由的控制器:

[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
    private readonly IBotFrameworkHttpAdapter Adapter;
    private readonly IBot Bot;

    public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
    {
        Adapter = adapter;
        Bot = bot;
    }

    [HttpPost]
    public async Task PostAsync()
    {
        // Delegate the processing of the HTTP POST to the adapter.
        // The adapter will invoke the bot.
        await Adapter.ProcessAsync(Request, Response, Bot);
    }
}

我现在想使用TypeScript从Angular客户端调用api /消息的POST,以向特定的Teams用户发送主动消息。

我确实TeamsConversationBot.cs通过执行以下操作弄清楚了如何将ConversationParameters设置为特定的Teams用户:

var conversationParameters = new ConversationParameters
{
    IsGroup = false,
    Bot = turnContext.Activity.Recipient,
    Members = new[] { new ChannelAccount("[insert unique Teams user guid here]") },
    TenantId = turnContext.Activity.Conversation.TenantId,
};

但是我正在努力的是如何构建一个JSON请求,该请求将Teams用户的guid(也许还有其他一些细节)从TypeScript发送到我的api /消息路由。

我该怎么做呢?我需要发送什么参数/身体?我一直无法在网上找到显示如何执行此操作的示例。


更新以下内容以进一步澄清

我正在使用Angular为我们的客户构建Web聊天应用程序。我要做的是,当客户通过聊天应用执行某些操作(发起对话,发送消息等)时,向使用Microsoft Teams的内部员工发送前瞻性消息。

我使用以下示例通过.NET Core构建了一个Teams机器人:https//kutt.it/ZCftjJ修改该示例后,我可以对Teams用户ID进行硬编码,并且主动消息已成功显示在Teams中:

var proactiveMessage = MessageFactory.Text($"This is a proactive message.");
var conversationParameters = new ConversationParameters
{
    IsGroup = false,
    Bot = turnContext.Activity.Recipient,
    Members = new[] { new ChannelAccount("insert Teams ID here") },
    TenantId = turnContext.Activity.Conversation.TenantId,
};

await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(teamsChannelId, serviceUrl, credentials, conversationParameters,
    async (t1, c1) =>
    {
        conversationReference = t1.Activity.GetConversationReference();
        await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(_appId, conversationReference,
            async (t2, c2) =>
            {
                await t2.SendActivityAsync(proactiveMessage, c2);
            },
            cancellationToken);
    },
cancellationToken);

我正在努力的是:

  1. 如何配置我的Angular应用程序以通知我的机器人我要发送的新的主动消息。
  2. 如何配置机器人以接受一些自定义参数(团队用户ID,消息)。

查看更多

提问者
Ryan Buening
被浏览
84
mdrichardson - MSFT 2020-02-05 03:49

希尔顿的答案仍然是好的,但是关于在没有事先交互的情况下主动向其发送消息的部分需要太长的响应时间。因此,回应您的最新评论:

是的,您需要为您要主动发送消息的用户所在的团队安装bot。否则将没有权限。


您无需重写OnMemberFyresAsync;只需查询花名册(见下文)。


您不需要对话ID即可执行此操作。我会让您的API接受他们的团队ID。您可以通过查询Teams Roster来获取此信息,您需要事先进行此操作并将其存储在哈希表或其他内容中……如果您的团队规模足够大,则可能是数据库。

就所需的信息而言,您需要足够的资源来构建ConversationParameters

var conversationParameters = new ConversationParameters
{
    IsGroup = false,
    Bot = turnContext.Activity.Recipient,
    Members = new ChannelAccount[] { teamMember },
    TenantId = turnContext.Activity.Conversation.TenantId,
};

...然后将其用于CreateConversationAsync

await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
    teamsChannelId,
    serviceUrl,
    credentials,
    conversationParameters,
    async (t1, c1) =>
    {
        conversationReference = t1.Activity.GetConversationReference();
        await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
            _appId,
            conversationReference,
            async (t2, c2) =>
            {
                await t2.SendActivityAsync(proactiveMessage, c2);
            },
            cancellationToken);
    },
    cancellationToken);

是的,您可以修改该样本。由于仅允许使用特定的架构,因此它将返回错误请求/api/messages您需要添加自己的端点。这是NotifyController的示例,我们的其他示例之一也使用了该示例。您可以看到它接受GET请求您只需要修改我们自己的构建即可接受POST请求。


综上所述,这似乎是比您准备好的更大的任务。没有错; 这就是我们学习的方式。与其直接跳入讨论,不如从以下内容开始:

  1. 真正了解API部件的工作原理之前,先使“ 主动样本”工作并深入研究代码

  2. 使“ 团队样本”工作,然后尝试使其传达给各个用户。

  3. 然后构建您的机器人,无需事先交互即可向用户发送消息。

如果您遇到麻烦,请随时浏览我的答案我已经回答了很多类似的问题。但是请注意,我们已经从我在一些答案中提到的Teams Middleware切换到了更集成到SDK中的东西。我们的团队样本(样本50-60)展示了如何进行所有操作。