Access user data bag from scorable without IDialogContext

半世苍凉 提交于 2019-12-24 03:12:40

问题


Dialogs have the IDialogContext object to aaccess data bags.

Example:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)

How to access context.UserDataBag from scorable class global handler? It doesn't contain IDialogContext but I need user data from botDataBag in scorable class.


回答1:


The usual way of doing this is using IBotData in your scorable.

You need to modify the constructor of your IScorable so it receives an IBotData, save it in a local variable, and use it later.

private readonly IBotData botData;

public ExtractCodeScorable(IBotToUser botToUser, IBotData botData) 
{ 
    this.botData = botData
}

Then you can use it as:

this.botData.PrivateConversationData.SetValue(key, element.Value); (or any of the other databags)

You can check the demo-CardAttachments sample as it heavily uses scorables and saves data in the data bags.




回答2:


You can access BotState from the Activity object. here is an example of a Scorable implementing ScorableBase and accessing UserData

public class CancelScorable : ScorableBase<IActivity, string, double>
{
    private readonly IDialogTask task;

    public CancelScorable(IDialogTask task)
    {
        SetField.NotNull(out this.task, nameof(task), task);
    }

    protected override async Task<string> PrepareAsync(IActivity activity, CancellationToken token)
    {
        // Accessing info, for example here UserData:
        var userData = await activity.GetStateClient().BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);

        // ... add your treatment

        return null;
    }

    // ...
}


来源:https://stackoverflow.com/questions/45561504/access-user-data-bag-from-scorable-without-idialogcontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!