Search code examples
botframeworkstate-management

How to share a context between bots?


I'm using the Microsoft bot framework in order to implement a simple bot-to-bot communication scenario. In my case, I have a master-bot and a skill-bot. I have completed their integration so that the master dot can pass a conversation to the skill and the skill can continue. I need to share some state between bots. So, I've created a state property accessor:

    public static readonly string UserContextPropertyName = $"{typeof(RootBot<T>).FullName}.UserContextProperty";

    private readonly IStatePropertyAccessor<PatientResponce> _userContext;

and use it in the OnTurnAsync method

    await _userContext.SetAsync(turnContext, patientResponce);

All good, except this new property is not available in the OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) turnContext of the Skill

Why it does not persist/pass-through?

P.S. I do save the state before I pass a conversation to the skill by calling:

  await _conversationState.SaveChangesAsync(turnContext, force: true, cancellationToken: cancellationToken);

P.S.S I also use

  var userState = new UserState(new BlobsStorage("..."))
  services.AddSingleton(userState);

in Startup.cs as a storage model


Solution

  • OK, there is a workaround but it is ok for my case - maybe someone finds it useful: there is a property

    turnContext.Activity.Value
    

    seem like it's been designed for attachments; it has no defined type - it is just an Object.

    So what I did is just store my context there as a serialized json string and catch it up on the other side. I consider this as a sub-optimal solution - so beware of boxing-undoxing overhead if you go that way