1
votes
  public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        if (activity.Type == ActivityTypes.Message)
        {
            Activity isTypingReply = activity.CreateReply();
            isTypingReply.Type = ActivityTypes.Typing;
            await connector.Conversations.ReplyToActivityAsync(isTypingReply);
            StateClient sc = activity.GetStateClient();
            BotData userData = await sc.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); // have to check on monday

            if (!userData.GetProperty<bool>("ChannelUserData"))
            { // fetch user details    

Above snippet from MS Bot SDK 1.x application to update user data in the state. Now I'm trying to upgrade old bot application to Microsoft Bot SDK 3.x. I'm using Cosmos Db to store state value.

How can I store or update user data with channelId and From Id? Once I fetch user details, I'm updating it as
await sc.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);

After a year, am reopening Microsoft application and a lot of changes in it. So, I couldn't fetch proper documentation. Can someone help/guide me the state storage process using Cosmos DB?

Followed Basic Setup https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-state-azure-cosmosdb?view=azure-bot-service-3.0

1
take a look at this as well it may be helpful - D4RKCIDE

1 Answers

2
votes

Be careful: activity.GetStateClient(); is returning the old StateClient which is deprecated.

To use your CosmosDb, follow the documentation you mention (https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-state-azure-cosmosdb?view=azure-bot-service-3.0#modify-your-bot-code) where you registered your CosmosDb as an IBotDataStore<BotData>

Then when you want to use it, for example to get a property:

using (ILifetimeScope scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
    var address = Address.FromActivity(activity);
    IBotDataStore<BotData> botDataStore = scope.Resolve<IBotDataStore<BotData>>();
    BotData botData = await botDataStore.LoadAsync(address, destination, CancellationToken.None);
    return botData.GetProperty<T>(propertyName);
}

Here the method Address.FromActivity(activity); is automatically getting the right keys to point to the right item.