Facebook Messenger API在c#asp.net mvc应用程序中的实现

node.js中实现

app.post('/webhook/', function(req, res) { console.log("message received " + req.body); if(!req.body){ console.log("no request body found"); res.sendStatus(200); return; } messaging_events = req.body.entry[0].messaging; for (i = 0; i < messaging_events.length; i++) { event = req.body.entry[0].messaging[i]; sender = event.sender.id; console.log("sender " + event.sender + ", message " + event.message.text); if (event.message && event.message.text) { text = event.message.text; // Handle a text message from this sender sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200)); } } res.sendStatus(200); }); 

如何在.net mvc应用程序中实现它。 特别是如何实现这一线

 app.post('/webhook/', function(req, res) 

在控制器在MVC。

Solutions Collecting From Web of "Facebook Messenger API在c#asp.net mvc应用程序中的实现"

最后search并收集一些数据

facebook发布这样的json数据

 { "entry": [{ "messaging":[{ "sender": { "id": "1126306517444636" }, "recipient": { "id": "514501872066958" }, "timestamp": 1469541638971, "message": { "mid": "mid.1469541638961:5d5d5d5bf1194eb824", "seq": 64, "text": "hi" } } ] } ] } 

所以我们必须像这样创build模型

 public class Sender { public string id { get; set; } } public class Recipient { public string id { get; set; } } public class Message { public string mid { get; set; } public int seq { get; set; } public string text { get; set; } } public class Messaging { public Sender sender { get; set; } public Recipient recipient { get; set; } public long timestamp { get; set; } public Message message { get; set; } } public class Entry { public List<Messaging> messaging { get; set; } } public class RootObject { public List<Entry> entry { get; set; } } 

和控制器类

 public ActionResult Webhook(RootObject model) { .................................... }