[英]How to trigger CallBack method on carousel Hero Card action in Bot Framework C#
我正在使用BOT Framework(C#),但遇到一個問題,需要您的幫助。 下面的代碼用於顯示Hero卡的輪播,在選擇我要調用回調方法的Hero卡之一時,在下面的示例中,您可以看到OnCardSelection是我要在選擇Card時調用的方法,但是在執行行context.Wait(onCardSelection)我低於錯誤
無效需求:預期通話,等待
可能是由於這個原因,它結束了對話,並考慮將進一步的操作(點擊英雄卡)作為新的對話。 另外,我想通過OnCardSelection方法訪問卡的詳細信息。 如何實現這一目標。 謝謝。
[LuisModel("your sub key", "secret")]
[Serializable]
public class BotManager : LuisDialog<object>
{
public async Task RaiseTicket(IDialogContext context, LuisResult result)
{
PromptDialog.Confirm(
context: context,
resume: ResumeAndHandleConfirmRaiseTicketAsync,
prompt: "It looks like you want to raise a ticket. Do you want to continue?",
retry: "I didn't understand. Please try again.");
}
private async Task ResumeAndHandleConfirmRaiseTicketAsync(IDialogContext context, IAwaitable<bool> argument)
{
bool choicesAreCorrect = await argument;
if (choicesAreCorrect)
{
RaiseTicket objRaiseTicket = new RaiseTicket();
await objRaiseTicket.StartAsync(context);
}
else
{
await context.PostAsync("Okay");
}
}
}
[Serializable]
public class RaiseTicket
{
public async Task StartAsync(IDialogContext context)
{
TypeOfTicket typeOfTicket;
context.UserData.TryGetValue("TypeOfTicket", out typeOfTicket);
if (typeOfTicket == TypeOfTicket.None)
{
//RaiseTicket
}
else
{
await PickExactCategory(context);
}
}
public async Task PickExactCategory(IDialogContext context)
{
var message = context.MakeMessage();
message.AttachmentLayout = AttachmentLayoutTypes.Carousel;
message.Attachments = GetCardsAttachments(categoryList);
await context.PostAsync(message);
context.Wait(OnCardSelection);
}
protected async Task OnCardSelection(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var answer = await result;
context.Done(new object());
}
private IList<Attachment> GetCardsAttachments(Categorylist[] categoryList)
{
List<Attachment> lstAttachment = new List<Attachment>();
foreach (Categorylist item in categoryList)
{
lstAttachment.Add(GetHeroCard(
item.Title, item.SubTitle,item.Text,
new CardAction(ActionTypes.ImBack, "Select", value: item.Tier3)));
}
return lstAttachment;
}
private static Attachment GetHeroCard(string title, string subtitle, string text, CardAction cardAction)
{
var heroCard = new HeroCard
{
Title = title,
Subtitle = subtitle,
Text = text,
Buttons = new List<CardAction>() { cardAction },
};
return heroCard.ToAttachment();
}
}
問題是因為您如何嘗試調用子對話框RaiseTicket
。
首先,如果是對話框,則應實現IDialog
接口。
然后,您將必須調用消息或將消息轉發到對話框。 您不應該手動調用StartAsync
方法。
您可以閱讀Microsoft bot框架中的“處理多個對話框”或“從對話框中調用表單”,以了解更多有關Call
/ Forward
方法的工作方式。 另外,您可以查看文檔中的“ 使用對話框管理對話流”一文。
正如Ezequiel Jadib所說,問題是如何調用RaiseTicket
,而RaiseTicket
應該實現IDialog
接口。
您可以像下面這樣修改RaiseTicket
:
[Serializable]
public class RaiseTicket: IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(PickExactCategory);
return Task.CompletedTask;
}
public async Task PickExactCategory(IDialogContext context, IAwaitable<object> result)
{
var message = context.MakeMessage();
message.AttachmentLayout = AttachmentLayoutTypes.Carousel;
message.Attachments = GetCardsAttachments(categoryList);
await context.PostAsync(message);
context.Wait(OnCardSelection);
}
public async Task OnCardSelection(IDialogContext context, IAwaitable<object> result)
{
var answer = await result as IMessageActivity;
context.Done(new object());
}
private IList<Attachment> GetCardsAttachments(List<Categorylist> categoryList)
{
List<Attachment> lstAttachment = new List<Attachment>();
foreach (Categorylist item in categoryList)
{
lstAttachment.Add(GetHeroCard(
item.Title, item.SubTitle, item.Text,
new CardAction(ActionTypes.ImBack, "Select", value: item.Tier3)));
}
return lstAttachment;
}
private static Attachment GetHeroCard(string title, string subtitle, string text, CardAction cardAction)
{
var heroCard = new HeroCard
{
Title = title,
Subtitle = subtitle,
Text = text,
Buttons = new List<CardAction>() { cardAction },
};
return heroCard.ToAttachment();
}
}
然后在BotManager
調用此RaiseTicket
對話框,例如:
var dialog = new RaiseTicket();
await context.Forward(dialog, dialog.OnCardSelection, null, CancellationToken.None);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.