如何处理Context.Done(R值)

时间:2017-02-28 10:11:42

标签: bots botframework chatbot

我有一个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);
    }
}

我发现的是,通常当我执行context.Done(STRING)然后输出给用户时,这对于结束对话非常有用。因为我想以英雄卡结束,它输出的是typename

Microsoft.Bot.Connector.HeroCard

任何人都可以帮助解释一个更好的方法来使用context.Done(R值)或者帮我回一个英雄卡来结束对话吗?

使用

调用对话框
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();

1 个答案:

答案 0 :(得分:2)

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

正如您所知,context.Done并未将任何内容发回给用户,它只会使用提供的值结束当前对话框。

用户的帖子实际上发生在.PostToUser()末尾的Chain。现在,通过查看PostToUser's code,我意识到在游戏结束时,它正在执行context.PostAsync item.ToString(),作为context.Done中提供的有效负载的项目1}}在这种情况下。请参阅this

一个选项(我尚未对此进行测试)可以使用.Do代替.PostToUser()并手动执行PostToUserDialog所执行的操作,最后执行context.PostAsync( )创建新的IMessageActivity并将HeroCard添加为附件。