代码之家  ›  专栏  ›  技术社区  ›  Ein2012

Botframework如何根据输入请求在启动时更改表存储连接字符串

  •  1
  • Ein2012  · 技术社区  · 7 年前

    我正在使用与LUIS集成的BotFramework版本(v4)。在里面 配置服务(IServiceCollection服务) 方法 创业。反恐精英 我在中间件中分配存储和LUIS的文件。下面是示例代码。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(configuration);
    
        services.AddBot<ChoiceBot>(options =>
        {    
            options.CredentialProvider = new ConfigurationCredentialProvider(configuration);
            var (luisModelId, luisSubscriptionKey, luisUri) = GetLuisConfiguration(configuration, "TestBot_Dispatch");//
            var luisModel = new LuisModel(luisModelId, luisSubscriptionKey, luisUri);
            var luisOptions = new LuisRequest { Verbose = true };
            options.Middleware.Add(new LuisRecognizerMiddleware(luisModel, luisOptions: luisOptions));
    
            //azure storage emulater
            //options.Middleware.Add(new ConversationState<Dictionary<string, object>>(new AzureTableStorage("UseDevelopmentStorage=true", "conversationstatetable")));
    
            IStorage dataStore = new AzureTableStorage("DefaultEndpointsProtocol=https;AccountName=chxxxxxx;AccountKey=xxxxxxxxx;EndpointSuffix=core.windows.net", "TableName");
    
            options.Middleware.Add(new ConversationState<Dictionary<string,object>>(new MemoryStorage()));
            options.Middleware.Add(new UserState<UserStateStorage>(dataStore));
        }
    }
    

    我的机器人将收到来自不同角色的用户的请求,例如(管理员、销售人员等)。 我想根据从传入请求中提取的角色更改传递给中间件的表存储连接字符串 .我将通过从传入请求的当前TurnContext对象中提取的用户名查询DB来获取用户角色。我可以在家里做这件事 安图恩 方法,但由于它们已经在中间件中声明,我想在中间件本身初始化时更改它们。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Drew Marsh    7 年前

    在里面净核心, Startup 逻辑只在启动时执行一次。

    如果我理解正确,您需要能够做的是:在运行时,在多个存储提供程序之间切换,在您的情况下,这些存储提供程序通过其基础连接字符串进行区分。

    没有什么“盒子里的”可以为您启用此场景,但是 这是可能的 如果使用正确的扩展点,并为自己编写正确的管道。具体来说,您可以在 IStatePropertyAccessor<T> 层和您的上游代码将继续在该抽象级别上工作,而且一点也不明智。

    Here's an implementation I've started that includes something I'm calling the ConditionalStatePropertyAccessor .它允许你创建一种复合材料 IStatePropertyAccessor<T> 配置了默认/回退实例以及 N 与选择器功能一起提供的其他实例,允许它们查看传入的 ITurnContext 并且,根据本回合任何部分的一些细节,指出这是本回合范围内应该使用的实例。 Take a look at the tests and you can see how I configure a sample that chooses an implementation based on the ChannelId for example.

    我现在有点忙,现在无法发货,但我打算打包并最终发货。但是,如果您认为这会有所帮助,请随意复制代码供您自己使用。