2016-05-10 21 views
9

Tôi đã bắt đầu làm việc trên chatbot của mình bằng cách sử dụng Bot Builder SDK trong C# và Microsoft Bot Framework. Tôi đã triển khai bot của mình và có thể tương tác với nó. Hiện tại tôi đang tập trung vào facebook messenger. Tôi đã có thể hiển thị thẻ và nút trên messenger bằng cách sử dụng Microsoft.Bot.Connector.Attachment. Bạn có thể tìm thấy nó trong Bot Connector Documentation. Bây giờ tôi đang cố gắng triển khai Tin nhắn tùy chỉnh trên Facebook nhưng không thành công. Đây là mã của tôi:Microsoft Bot Framework Tuỳ chỉnh Facebook Messages

public async Task<Message> Post([FromBody]Message message) 
    { 
     if (message.Type == "Message") 
     { 
      message.BotPerUserInConversationData = null; 


      if (message.Text.Equals("test", StringComparison.InvariantCultureIgnoreCase)) 
      { 
       var replyMessage = message.CreateReplyMessage(); 
       replyMessage.ChannelData = new 
       { 
        notification_type = "NO_PUSH", 
        attachment = new 
        { 
         type = "template", 
         payload = new 
         { 
          template_type = "receipt", 
          recipient_name = "Stephane Crozatier", 
          order_number = "12345678902", 
          currency = "USD", 
          payment_method = "Visa 2345", 
          order_url = "http://petersapparel.parseapp.com/order?order_id=123456", 
          timestamp = "1428444852", 
          elements = new[] 
          { 
           new { 
            title = "Classic White T-Shirt", 
            subtitle = "100% Soft and Luxurious Cotton", 
            quantity = 2, 
            price = 50, 
            currency = "USD", 
            image_url = "http://petersapparel.parseapp.com/img/whiteshirt.png" 
            }, 
           new 
            { 
             title = "Classic Gray T-Shirt", 
             subtitle = "100% Soft and Luxurious Cotton", 
             quantity = 1, 
             price = 25, 
             currency = "USD", 
             image_url = "http://petersapparel.parseapp.com/img/grayshirt.png" 
            } 
          }, 
          address = new 
          { 
           street_1 = "1 Hacker Way", 
           street_2 = "", 
           city = "Menlo Park", 
           postal_code = "94025", 
           state = "CA", 
           country = "US" 
          }, 
          summary = new 
          { 
           subtotal = 75.00, 
           shipping_cost = 4.95, 
           total_tax = 6.19, 
           total_cost = 56.14 
          }, 
          adjustments = new[] 
          { 
           new {name = "New Customer Discount", amount = 20}, 
           new {name = "$10 Off Coupon", amount = 10} 
          } 
         } 
        } 
       }; 

       return replyMessage; 

      } 

      // return our reply to the user 
      try 
      { 
       return await Conversation.SendAsync(message,() => new EchoDialog()); 
      } 
      catch (Exception exc) 
      { 
       return message.CreateReplyMessage(exc.Message); 
      } 
     } 
     else 
     { 
      return HandleSystemMessage(message); 
     } 
    } 

Ví dụ: EchoDialog đang hoạt động và tôi nhận được kết quả. Tôi có thể lấy dữ liệu trong giao diện khung bot. Đây là json tôi nhận được từ bot của tôi:

{ 
    "type": "Message", 
    "id": "CeBI3NS7w0k", 
    "conversationId": "D9b6pW1TY29TDUB5qx6CL5U59fv49oBQK5iKABIA0nFC0C8C", 
    "created": "2016-05-10T09:34:46.5131971Z", 
    "language": "en", 
    "text": "", 
    "attachments": [], 
    "from": { 
    "name": "TestBot", 
    "channelId": "test", 
    "address": "mytestbot", 
    "id": "mytestbot", 
    "isBot": true 
    }, 
    "to": { 
    "name": "devportal", 
    "channelId": "test", 
    "address": "devportal", 
    "id": "JMQ0KLCKN6R", 
    "isBot": false 
    }, 
    "replyToMessageId": "FUjcCRhTmu0", 
    "participants": [ 
    { 
     "name": "devportal", 
     "channelId": "test", 
     "address": "devportal", 
     "id": "JMQ0KLCKN6R", 
     "isBot": false 
    }, 
    { 
     "name": "TestBot", 
     "channelId": "test", 
     "address": "mytestbot", 
     "id": "mytestbot", 
     "isBot": true 
    } 
    ], 
    "totalParticipants": 2, 
    "mentions": [], 
    "channelConversationId": "mytestbot", 
    "channelData": { 
    "notification_type": "NO_PUSH", 
    "attachment": { 
     "type": "template", 
     "payload": { 
     "template_type": "receipt", 
     "recipient_name": "Stephane Crozatier", 
     "order_number": "12345678902", 
     "currency": "USD", 
     "payment_method": "Visa 2345", 
     "order_url": "http://petersapparel.parseapp.com/order?order_id=123456", 
     "timestamp": "1428444852", 
     "elements": [ 
      { 
      "title": "Classic White T-Shirt", 
      "subtitle": "100% Soft and Luxurious Cotton", 
      "quantity": 2, 
      "price": 50, 
      "currency": "USD", 
      "image_url": "http://petersapparel.parseapp.com/img/whiteshirt.png" 
      }, 
      { 
      "title": "Classic Gray T-Shirt", 
      "subtitle": "100% Soft and Luxurious Cotton", 
      "quantity": 1, 
      "price": 25, 
      "currency": "USD", 
      "image_url": "http://petersapparel.parseapp.com/img/grayshirt.png" 
      } 
     ], 
     "address": { 
      "street_1": "1 Hacker Way", 
      "street_2": "", 
      "city": "Menlo Park", 
      "postal_code": "94025", 
      "state": "CA", 
      "country": "US" 
     }, 
     "summary": { 
      "subtotal": 75, 
      "shipping_cost": 4.95, 
      "total_tax": 6.19, 
      "total_cost": 56.14 
     }, 
     "adjustments": [ 
      { 
      "name": "New Customer Discount", 
      "amount": 20 
      }, 
      { 
      "name": "$10 Off Coupon", 
      "amount": 10 
      } 
     ] 
     } 
    } 
    }, 
    "hashtags": [] 
} 

* Id và tên đã thay đổi.

Có ai đã quản lý FB Messenger để hiển thị các mẫu bằng cách sử dụng ChannelData không?

Cập nhật: Tôi đã kiểm tra từ tài khoản fb khác và đã thành công để nhận mẫu nhưng chỉ lần đầu tiên. Mọi nỗ lực tiếp theo vẫn là tôi mà không trả lời.

Trả lời

6

Mã của bạn kể từ khi tốt, bạn có ý nghĩa gì rằng "mọi nỗ lực tiếp theo vẫn cho tôi phản hồi wiathout".

tôi cũng đã tạo một BOT cho Messenger và nó hoạt động giống như bạn. Đây là mã của tôi

public async Task<Message> Post([FromBody]Message message) 
    { 
     if (message.Type == "Message"){ 
      Message ReplyMessage = message.CreateReplyMessage(); 
      ReplyMessage.ChannelData = getFBFunctionMenu(); 
      return ReplyMessage; 
     }else{return HandleSystemMessage(message);} 
    } 

và Chức năng của tôi này làm:

public object getFBFunctionMenu(){ 
     Models.Messenger fbmsg = new Models.Messenger(); 
     fbmsg.ChannelData = new MessengerChannelData { notification_type = "NO_PUSH", attachment = new MessengerAttachment { payload = new MessengerPayload() } }; 
     fbmsg.ChannelData.attachment.type = "template"; 
     fbmsg.ChannelData.attachment.payload.template_type = "generic"; 
     List<MessengerElement> e = new List<MessengerElement>(); 
     List<MessengerButton> bs = new List<MessengerButton>(); 
     bs.Add(new MessengerButton{type = "web_url",title = "Facebook",url ="http://www.facebook.com/"}); 
     bs.Add(new MessengerButton{type = "web_url",title = "Google",url ="http://www.google.com/"}); 
     bs.Add(new MessengerButton{type = "web_url",title = "Amazon",url ="http://www.amazon.com/"}); 
     e.Add(new MessengerElement 
     { 
      title = "My Favorte Site", 
      subtitle = "some descript", 
      item_url = "http://localhost/", 
      image_url = "http://loalhost/img.png", 
      buttons = bs.ToArray() 
     }); 
     fbmsg.ChannelData.attachment.payload.elements = e.ToArray(); 
     return fbmsg.ChannelData; 
    } 

Mẫu Mã của tôi:

namespace WiAdvance.BotConnector.Messenger.Models 
{ 

public class Messenger 
{ 
    public MessengerChannelData ChannelData { get; set; } 
} 
public class MessengerChannelData 
{ 
    public string notification_type { get; set; } 
    public MessengerAttachment attachment { get; set; } 
} 
public class MessengerAttachment 
{ 
    public string type { get; set; } 
    public MessengerPayload payload { get; set; } 
} 

public class MessengerPayload 
{ 
    public string template_type { get; set; } 
    public MessengerElement[] elements { get; set; } 
} 

public class MessengerElement 
{ 
    public string title { get; set; } 
    public string subtitle { get; set; } 
    public string item_url { get; set; } 
    public string image_url { get; set; } 
    public MessengerButton[] buttons { get; set; } 
} 

public class MessengerButton 
{ 
    public string type { get; set; } 
    public string url { get; set; } 
    public string title { get; set; } 
    public string payload { get; set; } 
} 
} 
Các vấn đề liên quan