如何处理context.done(r值)



我有一个我想具有以下行为的msbot聊天对话框:

user -> get me some info about GARY
bot -> which gary, (prompt: choice options)
user -> gary peskett
bot -> sure, (hero card with gary's contact details)

我有这个代码

public class CustomerRepository
{
    private IList<Customer> _customerList = new List<Customer>
    {
        new Customer
        {
            Name = "Gary Peskett"
        },
        new Customer
        {
            Name = "Gary Richards"
        },
        new Customer
        {
            Name = "Barry White"
        }
    };
    public async Task<IEnumerable<Customer>> GetAll()
    {
        // usually calls a database (which is why async is on this method)
        return _customerList;
    }
}
public class XDialog : IDialog
{
    private readonly IIntent _intent;
    private readonly CustomerRepository _customerRepository;
    public XDialog(IIntent intent, CustomerRepository customerRepository)
    {
        // An intent is decided before this point
        _intent = intent;
        _customerRepository = customerRepository;
    }
    public async Task StartAsync(IDialogContext context)
    {
        // // An intent can provide parameters
        string name = _intent.Parameters["Name"] as string;
        IEnumerable<Customer> customers = await _customerRepository.GetAll();
        IList<Customer> limitedList = customers.Where(x => x.Name.Contains(name)).ToList();
        if (limitedList.Any())
        {
            if (limitedList.Count > 1)
            {
                PromptDialog.Choice(context, LimitListAgain, limitedList,
                    "Can you specify which customer you wanted?");
            }
            else
            {
                Customer customer = limitedList.FirstOrDefault();
                Finish(context, customer);
            }
        }
        else
        {
            context.Done("No customers have been found");
        }
    }
    private static async Task LimitListAgain(IDialogContext context, IAwaitable<Customer> result)
    {
        Customer customer = await result;
        Finish(context, customer);
    }
    private static void Finish(IDialogContext context, Customer customer)
    {
        HeroCard heroCard = new HeroCard
        {
            Title = customer?.Name
        };
        context.Done(heroCard);
    }
}

我发现的是,通常在我进行上下文时。done(字符串),然后输出给用户,这对于结束对话框非常有用。就像我想以英雄卡结束时,它输出了打字机

Microsoft.Bot.Connector.HeroCard

任何人都可以通过解释更好的使用上下文的方式来提供帮助。

调用对话框
Chain.PostToChain()
    .Select(msg => Task.Run(() => _intentionService.Get(msg.ChannelId, msg.From.Id, msg.Text)).Result)
    .Select(intent => _actionDialogFactory.Create(intent)) // returns IDialog based on intent
    .Unwrap()
    .PostToUser();

我认为问题是使用Chain的副作用。

您可能知道,context.Done没有向用户发布任何内容,它只是以提供的值结束当前对话框。

Chain末尾,在.PostToUser()中有效地发生了对用户的帖子。现在,通过查看postouser的代码,我意识到,在游戏结束时,它正在执行item.ToString()context.PostAsync,在这种情况下是context.Done中提供的有效负载。看到这个。

一个选项(我尚未对此进行测试),可以使用.Do代替.PostToUser(),并手动执行PostTouserDialog所做的内容,并最终通过创建新的IMessageActivity并添加HeroCard作为附件来执行CONTESTASYNC()。

相关内容

最新更新