nodejsでチャットボットを開発し、herokuにデプロイしました。しかし、送信者 PSID が定義されていないというエラーが表示されるため、Webhook は正しく機能していません。私は何をすべきか?専門家の助けが必要です。プロジェクトで wit.ai を使用していませんが、node.js のみを使用しています。ここでは、ポストバックの処理が長すぎるため投稿していません。
以下のように私のcontrollers.js..(メッセージはシンハラ語です)
import dotenv from 'dotenv';
dotenv.config();
import request from 'request';
const PAGE_ACCESS_TOKEN = process.env.PAGE_ACCESS_TOKEN;
const MY_VERIFY_TOKEN = process.env.MY_VERIFY_TOKEN;
let test = (req, res) => {
return res.send("hello again")
}
let getWebhook = (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = MY_VERIFY_TOKEN
// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
}
let postWebhook = (req, res) => {
let body = req.body;
// Parse the request body from the POST
// Check the webhook event is from a Page subscription
if (body.object === 'page') {
// Iterate over each entry - there may be multiple if batched
body.entry.forEach(function (entry) {
// Gets the body of the webhook event
let webhook_event = entry.messaging[0];
console.log(webhook_event);
// Get the sender PSID
let sender_psid = webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
});
// Return a '200 OK' response to all events
res.status(200).send('EVENT_RECEIVED');
} else {
// Return a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
}
// Handles messages events
function handleMessage(sender_psid, received_message) {
let response;
// Checks if the message contains text
if (received_message.text) {
// Create the payload for a basic text message, which
// will be added to the body of our request to the Send API
response = {
"text": `ආයුබෝවන්! ඔබ මේ සම්බන්ද වන්නේ chatbot IN සේවාව වෙතයි.!ඔබට මේ පිළිබද වැඩිදුර දැනගැනීමට අවශ්යද?`,
"quick_replies":[
{
"content_type":"text",
"title":"Yes",
"payload":"<POSTBACK_PAYLOAD>",
},{
"content_type":"text",
"title":"No",
"payload":"<POSTBACK_PAYLOAD>",
}
]
}
}
}
// Send the response message
callSendAPI(sender_psid, response);
// Sends response messages via the Send API
function callSendAPI(sender_psid, response) {
// Construct the message body
let request_body = {
"recipient": {
"id": sender_psid
},
"message": response
}
// Send the HTTP request to the Messenger Platform
request({
"uri": "https://graph.facebook.com/v11.0/me/messages",
"qs": { "access_token": PAGE_ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log('message sent!')
} else {
console.error("Unable to send message:" + err);
}
});
}
module.exports = {
test: test,
getWebhook: getWebhook,
postWebhook: postWebhook
}
ありがとう!