是否可以对多个Microsoft Bot使用相同的EndPoint [英] Is it possible to use the same EndPoint for several Microsoft Bots

查看:116
本文介绍了是否可以对多个Microsoft Bot使用相同的EndPoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试新的Microsoft Bot Framework来构建Skype Bot. 我想知道是否可以为多个机器人注册相同的端点?

I'm trying the new Microsoft Bot Framework to build Skype Bots. I was wondering if it's possible to register the same EndPoint for several Bots?

我看到了Microsoft的示例,它们使用web.config来存储以下内容:

I saw the Microsoft examples and they use the web.config to store the:

<add key="MicrosoftAppId" value="GUID" />
<add key="MicrosoftAppPassword" value="PASSWORD" />

并使用[BotAuthentication]属性来解决所有问题.

And using the [BotAuthentication] attribute to resolve everything.

如果同一终端可以重新用于多个Bot,该如何处理身份验证?

If the same endpoing can be reused for several Bots, how should I handle the authentication?

谢谢

推荐答案

多租户实现-可能的解决方案

Multi tenant implementation - possible solution

更改消息端点的路由以采用通配符,如下所示:

Change the route of the messages endpoint to take a wildcard like so:

    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    [Route("messages/{*wildcard}")]
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {

实施多租户bot身份验证属性,该属性将从最后一个url段中获取应用程序ID(或从数据库中查找以获取应用程序ID).

Implement a multi tenant bot authentication attribute which will take the application id from the last url segment (or a lookup to fetch it from a database).

例如 http://localhost:3979/messages/your-bot-app-id -或键

[MultiTentantBotAuthentication]
public class MessagesController : ApiController
{

实施

public class MultiTentantBotAuthenticationAttribute : BotAuthentication
{
    public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        this.MicrosoftAppId = actionContext.Request.RequestUri.Segments.Last();

        await base.OnAuthorizationAsync(actionContext, cancellationToken);
    }
}

如果您需要bot中的代码来知道它正在运行的应用程序,则可以通过userdata传递它,如下所示:

If you need the code in your bot to know which app it is operating in you could pass it in via userdata like so:

    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    [Route("messages/{*wildcard}")]
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        var botId = Request.RequestUri.Segments.Last(); // Validate against your database / configuration

        try
        {
            if (activity.Type == ActivityTypes.Message)
            {
                // https://docs.botframework.com/en-us/csharp/builder/sdkreference/stateapi.html

                var stateClient = activity.GetStateClient();
                BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
                userData.SetProperty("ApplicationAccountBotId", botId);
                await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);

                await Conversation.SendAsync(activity, () => actionDialog);
            }
            else
            {
                HandleSystemMessage(activity);
            }
        }
        catch (Exception exception)
        {
            await logger.ErrorAsync(exception);
        }

        return Request.CreateResponse(HttpStatusCode.OK);
    }

然后,您可以通过对话框/机器人代码访问用户数据.

You can then access user data in a dialog / bot code.

这篇关于是否可以对多个Microsoft Bot使用相同的EndPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆