sender_psid объявлен внутри функции app.post. Есть ли способ объявить это глобально, чтобы вызовы функций внизу могли получить к нему доступ?
Примечание. Объявление его вне функции не поможет, поскольку webhook.event не существует вне его.
/ Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates 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);
// Check if the event is a message or postback and
// pass the event to the appropriate handler function
if (webhook_event.message) {
msg.handleMessage(sender_psid, webhook_event.message);
} else if (webhook_event.postback) {
msg.handlePostback(sender_psid, webhook_event.postback);
}
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
//Imports functions from other files
let msg = require('./msg.js'),
handleMessage = msg.handleMessage(sender_psid, received_message),
handlePostback = msg.handlePostback(sender_psid, received_postback),
callSendAPI = msg.callSendAPI(sender_psid, response);