gpt4 book ai didi

c# - 如何在 bot 框架 v4 中将对话框设置为欢迎消息

转载 作者:行者123 更新时间:2023-11-30 22:52:05 26 4
gpt4 key购买 nike

欢迎消息很酷,但您不一定需要它们。在我的例子中,我有多个对话框,其中一个 MainDialog 应该触发其他对话框。

我遇到的问题是(使用机器人模拟器)用户必须在触发主对话框之前输入一些内容。

我们不能在添加成员时触发对话框吗?感谢您通读 :)。这是我的整个类(class):

此机器人代码,源自 richcardsbot 示例 here :

  using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HackBot.Dialogs;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.BotBuilderSamples.Bots;
using Microsoft.Extensions.Logging;

namespace HackBot.Bots
{
public class RichCardsBot : DialogBot<MainDialog>
{
public RichCardsBot(ConversationState conversationState, UserState userState, MainDialog dialog, ILogger<DialogBot<MainDilaog>> logger)
: base(conversationState, userState, dialog, logger)
{

}

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
var attachments = new List<Attachment>();
// Greet anyone that was not the target (recipient) of this message.
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
if (member.Id != turnContext.Activity.Recipient.Id)
{
var reply = MessageFactory.Attachment(attachments);
//var reply= MessageFactory.Text("The following flight has been cancelled ."
// + " You have a Hotel booking for the Destination."
// + "what would you like to do with the booking ?.");
reply.Attachments.Add(Cards.FirstCard().ToAttachment());

await turnContext.SendActivityAsync(reply, cancellationToken);
}
}
}
}
}

最佳答案

检查ConversationUpdate事件:

 // innderDc is the **DialogContext**
var activity = innerDc.Context.Activity;

// Check activity type
switch (activity.Type) {
case ActivityTypes.ConversationUpdate:
{
if (activity.MembersAdded ? .Count > 0) {
foreach(var member in activity.MembersAdded) {

// do logic
await innerDc.BeginDialogAsync(nameof("yourDialog"));

}
}
break;
}

更新:尝试以下操作:

 protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
// Greet anyone that was not the target (recipient) of this message.
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
if (member.Id != turnContext.Activity.Recipient.Id)
{
//var reply = MessageFactory.Text("Welcome to CardBot."
// + " This bot will show you different types of Rich Cards."
// + " Please type anything to get started.");

//await turnContext.SendActivityAsync(reply, cancellationToken);
await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);


}
}
}

关于c# - 如何在 bot 框架 v4 中将对话框设置为欢迎消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58487605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com