Verwendung von Karten in "Dialog"
Bruno Klein
Ich versuche, die fließende API zu verwenden, um einen einfachen Ablauf zu erstellen. Aber anstatt einfachen Text zu verwenden, möchte ich reichhaltige visuelle Komponenten verwenden. Hier ist ein Beispiel.
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
var yn = Chain
.PostToChain()
.Select(m => CreateYesNoPrompt(activity)) //This is a dialog which should provide some buttons to the user
.PostToUser()
.WaitToBot()
.Select(x => x.Text)
.Switch
(
Chain.Case
(
s => s == "S",
new ContextualSelector<string, IDialog<string>>((context, item) => Chain.Return("Yes"))
),
Chain.Default<string, IDialog<string>>((context, text) => Chain.Return("No"))
)
.Unwrap()
.PostToUser();
await Conversation.SendAsync(activity, () => yn);
return Request.CreateResponse(HttpStatusCode.OK);
}
private static Activity CreateYesNoPrompt(Activity activity)
{
var reply = activity.CreateReply();
var ybutton = new CardAction(type: "postBack", title: "Yes", value: "S");
var nbutton = new CardAction(type: "postBack", title: "No", value: "N");
var buttons = new List<CardAction>() { ybutton, nbutton };
var card = new HeroCard("Would you like to start an order?", "Subtitle", buttons: buttons);
reply.Attachments = new List<Attachment> { card.ToAttachment() };
return reply;
}
Anstelle der erwarteten Ausgabe wird die Bot ausgibt Microsoft.Bot.Connector.Activity
, welche das ist ToString()
Rückkehr des Activity
Objekts.
Wie verwende ich Karten in Dialogen?
Michael Washington
Dies zeigt eine Karte, wenn ein DialogContext übergeben wird:
private static Activity ShowButtons(IDialogContext context, string strText)
{
// Create a reply Activity
Activity replyToConversation = (Activity)context.MakeMessage();
replyToConversation.Text = strText;
replyToConversation.Recipient = replyToConversation.Recipient;
replyToConversation.Type = "message";
// Call the CreateButtons utility method
// that will create 5 buttons to put on the Here Card
List<CardAction> cardButtons = CreateButtons();
// Create a Hero Card and add the buttons
HeroCard plCard = new HeroCard()
{
Buttons = cardButtons
};
// Create an Attachment
// set the AttachmentLayout as 'list'
Attachment plAttachment = plCard.ToAttachment();
replyToConversation.Attachments.Add(plAttachment);
replyToConversation.AttachmentLayout = "list";
// Return the reply to the calling method
return replyToConversation;
}
Siehe: Verwenden von Bildern, Karten, Karussells und Schaltflächen im Microsoft Bot Framework