-3

I want to create an interview bot using azure bot service and want to use cosmos db for interview questions can it possible?Need help and suggestions for this.

1 Answers1

0

It is unclear what is your architecture to actually get the bot working and if there is any limitation, but I assume you are using C# as your language and hosting the Bot in a C# Web application.

You can use this article as base Bot conversation history with Azure Cosmos DB.

It not only shows how to store UserData, but also how to store the State in Cosmos DB (this actually is better because you get the performance benefits of Cosmos DB and you also go over the 32Kb limit that the Bot Framework State has).

Following that article, you will be storing in Cosmos DB:

  • User Data Store: To store data specific to a user.

  • Conversation Store: To store data specific to a conversation.

  • Private Conversation Store: To store data specific to a user in a conversation

If you want to store the chat lines, it's not done by default by the Bot Framework. You have to create a class that implements IActivityLogger and let the user know that you are storing the chat.

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);
    }
}

Then you have to add the logger wherever you are declaring your Bot Container, for example, in Global.asax:

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

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

More info on how to register the middleware here.

Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • I have used cosmos db for store bot conversion history.Now i want to use cosmos db for storing questions and answers for my interview bot but i dont have idea how to do that. – user3807114 Oct 05 '17 at 04:44
  • Can you describe what's your data schema more in detail? The questions and answers in a Bot are indeed part of the conversation history and you can save it in the `PrivateConversationData` as described in the article. – Matias Quaranta Oct 05 '17 at 15:28
  • 1
    Best not to start a q&a discussion in comments (especially below an answer). I suggest taking this to [chat](chat.stackoverflow.com) if you want to discuss further. – David Makogon Oct 05 '17 at 15:55
  • I agree David. @user3807114 if you add more detail to the question I can edit and provide more information on the answer, otherwise please refer to the article and the section about PrivateConversationData. – Matias Quaranta Oct 05 '17 at 16:55
  • Hi Matias Quaranta,I am creating a Interview Bot using Azure Bot Service.My need is to bot should take interview of candidates(just the initial screening).So whenever a user will interact with my bot the interview process will start it will ask some question by fetching from my cosmos db.I think now its clear to you.I need help how to fetch the questions from cosmos db. – user3807114 Oct 06 '17 at 04:23
  • Sorry David Makogon,but i dont have enough reputation to chat in Stack Overflow Chat(chat.stackoverflow.com) . – user3807114 Oct 06 '17 at 04:27
  • @user3807114 updated answer with code sample to store chat history – Matias Quaranta Oct 06 '17 at 10:53