我的帖子webhook:
router.post('/webhook', function (req, res) {
var data = req.body;
// Make sure this is a page subscription
if (data.object == 'page') {
// Iterate
over each entry
// There may be multiple if batched
data.entry.forEach(function(pageEntry) {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
// Iterate over each messaging event
pageEntry.messaging.forEach(function(messagingEvent) {
if (messagingEvent.optin) {
receivedAuthentication(messagingEvent);
} else if (messagingEvent.message) {
receivedMessage(messagingEvent);
} else if (messagingEvent.delivery) {
receivedDeliveryConfirmation(messagingEvent);
} else if (messagingEvent.postback) {
receivedPostback(messagingEvent);
} else if (messagingEvent.read) {
receivedMessageRead(messagingEvent);
} else if (messagingEvent.account_linking) {
receivedAccountLink(messagingEvent);
} else {
console.log("Webhook received unknown messagingEvent: ", messagingEvent);
}
});
});
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know
// you've successfully received the callback. Otherwise, the request
// will time out and we will keep trying to resend.
res.sendStatus(200);
}
});
2.my receivedMessage功能:
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
var messageId = message.mid;
var messageText = message.text;
var messageAttachments = message.attachments;
if (messageText) {
// If we receive a text message, check to see if it matches a keyword
// and send back the example. Otherwise, just echo the text we received.
switch (messageText) {
case 'generic':
sendGenericMessage(senderID);
break;
default:
sendTextMessage(senderID, messageText);
}
} else if (messageAttachments) {
sendTextMessage(senderID, "Message with attachment received");
}
}
当我使用sendTextMessage运行我的代码时,这是服务器日志,调用callsend api,我认为我的代码失败了:
2017-03-17T10:06:11.296017 + 00:00 app [web.1]:收到用户留言 1367047720019479和第1334554393257327页,电话:1489745171043 消息:2017-03-17T10:06:11.298010 + 00:00 app [web.1]: {"中":"中期$ cAAS9xUXoM5lhDQCaY1a27tcMAHJS""序列":157393,"文本":"测试 消息"} 2017-03-17T10:06:11.298044 + 00:00 app [web.1]: {"收件人" {" ID":" 1367047720019479"}"消息" {"文本":& #34;检测 消息"}} 2017-03-17T10:06:11.298649 + 00:00 app [web.1]:POST / webhook / 200 3.244 ms - 2
现在当我包含sendtext和callSendAPI时:
function sendTextMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
console.log(JSON.stringify(messageData));
callSendAPI(messageData);
}
function callSendAPI(messageData) {
***console.log("inside the call api");***
request.post({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: access },
//method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
这是它给出的输出:
2017-03-17T10:09:38.141702 + 00:00 app [web.1]:收到用户留言 1367047720019479和第1334554393257327页,电话:1489745373029 消息:2017-03-17T10:09:38.141719 + 00:00 app [web.1]: {"中":"中期$ cAAS9xUXoM5lhDQOvZVa275xBT7LW""序列":157396,"文本":"测试 消息"} 2017-03-17T10:09:38.141757 + 00:00 app [web.1]: {"收件人" {" ID":" 1367047720019479"}"消息" {"文本":& #34;检测 消息"}} 2017-03-17T10:09:38.141778 + 00:00 app [web.1]: 致电api 2017-03-17T10:09:38.143120 + 00:00 app [web.1]: POST / webhook / 500 1.686 ms - 253
请帮助我如何解决此错误:所有令牌设置正确页面访问并验证token.i我是信使平台的新手