使用azurebot服务和cosmos db的访谈机器人的问题

时间:2017-10-04 04:20:28

标签: botframework azure-cosmosdb azure-bot-service

我想使用azure bot服务创建一个面试机器人,并希望使用cosmos db来获取面试问题吗?需要帮助和建议。

1 个答案:

答案 0 :(得分:0)

目前还不清楚您的架构实际上是什么让机器人工作,如果有任何限制,但我假设您使用C#作为您的语言并在C#Web应用程序中托管Bot。

您可以将此文章用作基础Bot conversation history with Azure Cosmos DB

它不仅展示了如何存储UserData,还展示了如何在Cosmos DB中存储State(这实际上更好,因为您获得了Cosmos DB的性能优势,并且您还超过了{{3}的32Kb限制}))。

在该文章之后,您将存储在Cosmos DB中:

  • 用户数据存储:存储特定于用户的数据。

  • 对话存储:存储特定于对话的数据。

  • 私人对话商店:在对话中存储特定于用户的数据

如果您想存储聊天行,Bot Framework默认不会这样做。您必须创建一个实现IActivityLogger的类,并让用户知道您正在存储聊天。

public class CosmosDBActivityLogger : IActivityLogger
{
    private readonly DocumentClient _client;
    private readonly string _collectionUri;

    public ServiceBusActivityLogger(DocumentClient client, string databaseName, string collectionName)
    {
        this._client = DocumentClient;

        // This is the collection where you want to store the chat
        this._collectionUri = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);
    }

    public async Task LogAsync(IActivity activity)
    {
        var message = activity.AsMessageActivity();
        // At this point you might want to handle your own Activity schema or leave the default

        // Not handling errors for simplicity's sake, but you should

        this._client.CreateDocumentAsync(this._collectionUri, message);
    }
}

然后,您必须在声明Bot容器的位置添加记录器,例如,在Global.asax中:

protected void Application_Start()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<CosmosDBActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
        builder.Update(Conversation.Container);    

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

有关如何注册中间件Bot Framework State的更多信息。