I have a need to reset or kill (using context.Done) the current IDialogContext instance and create a new instance and use it in a different function. How can I create such an instance, IDialogContext being an interface. Is it possible?
This is the function from which am tried to call a function "selfredirect"
private async Task ApprovalConfirm(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
xxxticket xxxticketobject = new xxxticket();
if (message.Text.Contains("yes"))
{
xxxticketobject = context.ConversationData.Get<xxxticket>("xxxcard");
if (xxxticketobject.action == "Approve")
await context.PostAsync("Accepted");
else
await context.PostAsync("Rejected");
context.ConversationData.RemoveValue("xxxfinalcard");
context.Reset();// *This is what I want to do...clear context and then call the following function*
await SelfRedirect(context, context.UserData.Get<string>("CurrentQuery"), "Approval");
}
else if (message.Text == "no")
{
await context.PostAsync("Thank you");
context.Done(context);
}
else
await SelfRedirect(context, context.UserData.Get<string>("CurrentQuery"), null);
}
The selfredirect function
public static async Task SelfRedirect(IDialogContext context, string msg, string intent=null)
{
try
{
DialogLuis LUISDialog = new DialogLuis();
IMessageActivity message = context.MakeMessage();
message.Text = msg;
var tasks = LUISDialog.services.Select(s => s.QueryAsync(message.Text, context.CancellationToken)).ToArray();
var results = await Task.WhenAll(tasks);
var winners = from result in results.Select((value, index) => new { value, index })
let resultWinner = LUISDialog.BestIntentFrom(result.value)
where resultWinner != null
select new LuisServiceResult(result.value, resultWinner, LUISDialog.services[result.index]);
if (intent != null)
{
winners = from result in results.Select((value, index) => new { value, index })
let resultWinner = new IntentRecommendation(intent)
where resultWinner != null
select new LuisServiceResult(result.value, resultWinner, LUISDialog.services[result.index]);
}
var winner = LUISDialog.BestResultFrom(winners);
if (winner == null)
{
throw new InvalidOperationException("No winning intent selected from Luis results.");
}
if (winner.Result.Dialog?.Status == DialogResponse.DialogStatus.Question)
{
var childDialog = await LUISDialog.MakeLuisActionDialog(winner.LuisService,
winner.Result.Dialog.ContextId,
winner.Result.Dialog.Prompt);
context.Call(childDialog, LUISDialog.LuisActionDialogFinished);
}
else
{
IAwaitable<IMessageActivity> item = null;
await LUISDialog.DispatchToIntentHandler(context, item, winner.BestIntent, winner.Result);
}
}
catch (Exception ex) { CommonMethods.LogTime("", ex, context); }
}
But I cant use the redirect function after reset as the stack is empty. Please help
Let me elaborate why I had to use the selfredirect function instead of context.wait(MessageReceived) through this small function
private async Task ApprovalConfirm(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
if (message.Text == "no")
{
await context.PostAsync("Thank you");
context.Done(context);
}
else if (message.Text == "yes")
{
await context.PostAsync("yes");
context.Done(context);
}
else
{
context.Wait(MessageReceived);
}
}
My use case is like, if the user types yes then bot should return yes, if user types no then bot should return thank you. and if the user types "Hi" the bot should find the answer for Hi through Luis which will hit on a new Greeting intent and return Hi user.
Here if I user context.Wait(MessageReceived) then the bot would simply wait for the next query from user
The structure of dialogs!
[LuisIntent("Approval")]
public async Task Approval(IDialogContext context, LuisResult result)
{
context.ConversationData.SetValue("currentIntent", "Approval");
.
.
.
PromptDialog.Choice<string>(context, ApprovalList, ...);
}
private async Task ApprovalList(IDialogContext context, IAwaitable<string> result)
{
try
{
var request_type = await result;
.
.
context.Wait(ApprovalResponse);
}
}
catch (Exception ex)
{
CommonMethods.LogTime("ApprovalList", ex, context);
}
}
private async Task ApprovalResponse(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
.
.
.
context.Wait(AfterApprovalComment);
}
catch (Exception ex)
{
CommonMethods.LogTime("ApprovalResponse", ex, context);
await context.PostAsync("out of approvals...");
context.Done(context);
}
}
private async Task ApprovalConfirm(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
.
.
.
await SelfRedirect(context, context.UserData.Get<string>("CurrentQuery"), "Approval");
}