2017-02-28 26 views
1

我有我想有以下行爲msbot聊天對話框:如何應對Context.Done(R值)

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); 
    } 
} 

我」什麼m發現通常是當我做context.one(STRING)然後輸出給用戶,這對結束對話是非常有用的。當我想用一個英雄卡結束,其outputing類型名稱

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(); 
+0

這篇文章https://github.com/Microsoft/BotBuilder-Samples/blob/master/CSharp/core-MultiDialogs/Dialogs/HotelsDialog.cs顯示它們返回context.Done (null);我想知道這是否是最好的選擇:s – chris31389

+0

誰在給XDialog打電話? –

+0

從Chain.PostToChain()中調用它。我用我使用的代碼更新了這個問題 – chris31389

回答

2

我認爲這個問題是使用Chain的副作用。

正如您所知,context.Done不會向用戶發送任何內容,它只是以提供的值結束當前對話框。

Chain末尾.PostToUser()處發生的用戶信息實際上發生了變化。現在,通過查看PostToUser's code,我意識到在遊戲結束時,它正在執行context.PostAsyncitem.ToString(),在這種情況下將context.Done中提供的有效載荷作爲項目。見this

一個選項(我沒有測試過這一點),可以使用.PostToUser()和手動.Do,而不是執行PostToUserDialog做什麼,最後通過創建一個新IMessageActivity並添加HeroCard作爲附件進行context.PostAsync() 。

+0

謝謝你。根據你的回答,我決定擺脫對話的鏈接。我發現這個https://github.com/Microsoft/BotBuilder-Samples/blob/master/CSharp/core-MultiDialogs/Dialogs/RootDialog.cs,我現在用它來管理我的對話框流程 – chris31389

+1

是的,那更好我的觀點。 –

相關問題