2017-05-11 78 views
0

我是Bot Bot的新手,我正在使用C#編寫一個簡單的機器人程序,它應該返回一個樣本英雄卡作爲回覆。問題是英雄卡沒有出現在Bot Framework通道模擬器中。下面的代碼:英雄卡在Bot框架的模擬器中沒有顯示

public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> arg) 
    { 
     var referenceMessage = await arg as IMessageActivity; 
     var msg = (Activity)context.MakeMessage(); 
     Activity replyToConversation = msg.CreateReply($"Buscando resultados para {referenceMessage.Text}"); 
     replyToConversation.Recipient = msg.From; 
     replyToConversation.Type = "message"; 
     replyToConversation.ReplyToId = referenceMessage.Id; 
     replyToConversation.AttachmentLayout = "carousel"; 
     replyToConversation.Attachments = new List<Attachment>(); 
     List<CardImage> CardImages = new List<CardImage>(); 
     CardImages.Add(new CardImage() 
     { 
      Url = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/BMW-Z4_diagonal_front_at_IAA_2005.jpg/243px-BMW-Z4_diagonal_front_at_IAA_2005.jpg" 
     }); 

     CardAction btnWebsite = new CardAction() 
     { 
      Type = "openUrl", 
      Title = "Open", 
      Value = "http://bmw.com" 
     }; 

     HeroCard plCard = new HeroCard() 
     { 
      Title = $"{referenceMessage.Text}", 
      Subtitle = $"Resultados de busqueda para {referenceMessage.Text}", 
      Images = CardImages, 
      Tap = btnWebsite 
     }; 

     var attachment = plCard.ToAttachment(); 
     replyToConversation.Attachments.Add(attachment); 
     await context.PostAsync(replyToConversation); 

     //var connector = new ConnectorClient(new Uri(msg.ServiceUrl)); 
     //var reply = connector.Conversations.SendToConversationAsync(replyToConversation); 
    } 

正如你所看到的,我一直在試圖與這兩個上下文和連接器,但該卡不顯示up.I've調試的應用程序,我可以看到輸入的信息被正確捕獲

對此有何看法?

+0

有時來自問題圖像的網址。將圖片網址更改爲「https://upload.wikimedia.org/wikipedia/en/archive/a/a9/20151112035044!Banyan_Tree_(_Shiv_Bajrang_Dham_Kishunpur).jpeg」。如果有效,請告訴我們。 – OmG

回答

2

我會發布您的可能解決方案。

*如果你的功能在一些IDialog類,如果你期待的一些結果應該是這樣的:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument) 

*第二溶液(如果我是你,我會用這個)是創建來自當前上下文的消息。所以,你的代碼應該是:

public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> arg) 
    { 

     var replyToConversation= context.MakeMessage(); 

     replyToConversation.AttachmentLayout = "carousel"; 
     replyToConversation.Attachments = new List<Attachment>(); 
     List<CardImage> CardImages = new List<CardImage>(); 
     CardImages.Add(new CardImage() 
     { 
      Url = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/BMW-Z4_diagonal_front_at_IAA_2005.jpg/243px-BMW-Z4_diagonal_front_at_IAA_2005.jpg" 
     }); 

     CardAction btnWebsite = new CardAction() 
     { 
      Type = "openUrl", 
      Title = "Open", 
      Value = "http://bmw.com" 
     }; 

     HeroCard plCard = new HeroCard() 
     { 
      Title = $"{referenceMessage.Text}", 
      Subtitle = $"Resultados de busqueda para {referenceMessage.Text}", 
      Images = CardImages, 
      Tap = btnWebsite 
     }; 

     var attachment = plCard.ToAttachment(); 
     replyToConversation.Attachments.Add(attachment); 
     await context.PostAsync(replyToConversation); 
    } 

注:

而不是

replyToConversation.AttachmentLayout = "carousel", 

使用

replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel; 

希望它能幫助:)

+0

嗨鮑勃,非常感謝你的回答,它工作得很好,並幫助我簡化了我的代碼。乾杯! – user1659653

+0

很高興聽到這個消息。 :) –

相關問題