问题
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