김예미

Use Callback function

......@@ -29,7 +29,7 @@ app.get("/webhook", function (req, res) {
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
let body = req.body;
var body = req.body;
if (body.object === 'page') {
......@@ -38,11 +38,11 @@ app.post('/webhook', (req, res) => {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
let webhook_event = entry.messaging[0];
var webhook_event = entry.messaging[0];
console.log(webhook_event);
// Get the sender PSID
let sender_psid = webhook_event.sender.id;
var sender_psid = webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
......@@ -63,4 +63,3 @@ app.post('/webhook', (req, res) => {
}
});
......
let request = require('request'),
template = require('./template');
var request = require('request');
var template = require('./template.js');
// Handles message events
exports.handleMessage = (sender_psid, received_message) => {
let response;
if(received_message.text === "처음으로"){
response = template.goToGreetTemplate(received_message);
exports.handleMessage=function(sender_psid, received_message) {
var response;
var text = received_message.text;
if(text === "처음으로"){
template.greetingTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
callSendAPI(sender_psid, response);
}else{
response = template.choiceLectProfTemplate(received_message);
template.choiceLectProfTemplate(text, function(result, res){
if(result==true){
response=res;
}
});
}
callSendAPI(sender_psid, response);
}
exports.handlePostback = (sender_psid, received_postback) => {
let response;
exports.handlePostback =function(sender_psid, received_postback) {
var response;
// Get the payload for the postback
let payload = received_postback.payload;
var payload = received_postback.payload;
var firstchoice = payload.substr(0,14);
// Set the response based on the postback payload
if(payload === 'Greeting'){
response = template.greetingTemplate();
console.log('Greeting');
template.greetingTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
callSendAPI(sender_psid, response);
}else if(payload.substr(0,14)=== 'CHOICE_BY_PROF'){
}else if(firstchoice === 'CHOICE_BY_PROF'){
handlePostback_choiceByProf(sender_psid, received_postback);
}else if(payload.substr(0,14)=== 'CHOICE_BY_LECT'){
handlePostback_choiceByLect(sender_psid, recieved_postback);
}else if(payload.substr(0,14) ==='CHOICE_BY_HELP'){
handlePostback_help(sender_psid, recieved_postback);
}else if(firstchoice === 'CHOICE_BY_LECT'){
handlePostback_choiceByLect(sender_psid, received_postback);
}else if(firstchoice === 'CHOICE_BY_HELP'){
handlePostback_help(sender_psid, received_postback);
}else if(payload.substr(0,8) === 'MoreRate'){
response = template.moreRateTemplate(received_postback);
template.moreRateTemplate(payload, function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
callSendAPI(sender_psid, response);
}
}
const handlePostback_choiceByProf = (sender_psid, received) => {
let response;
let stat = received.payload.substr(14,6);
const handlePostback_choiceByProf = function(sender_psid, received){
var response;
var payload = received.payload;
var stat = payload.substr(14,6);
if(stat === 'stat_0'){
response = template.getProfNameTemplate(received);
template.getProfNameTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}else if(stat === 'stat_2'){
response = template.moreProfTemplate(received);
template.moreProfTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}else if(stat === 'stat_3'){
response = template.rateTemplate(received);
template.rateTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}
callSendAPI(sender_psid, response);
}
const handlePostback_choiceByLect = (sender_psid, recieved) => {
let response;
let stat = recieved.payload.substr(14,6);
const handlePostback_choiceByLect = function(sender_psid, received){
var response;
var payload = received.payload;
var stat = payload.substr(14,6);
if(stat === 'stat_0'){
response = template.getLectNameTemplate(received);
template.getLectNameTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}else if(stat === 'stat_2'){
response = template.moreLectTemplate(received);
template.moreLectTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}else if(stat === 'stat_3'){
response = template.rateTemplate(received);
template.rateTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}
callSendAPI(sender_psid, response);
}
const handlePostback_help = (sender_psid, received) => {
let response;
let stat = received.payload.substr(14);
const handlePostback_help = function(sender_psid, received) {
var response;
var payload = received.payload;
var stat = payload.substr(14,6);
if(stat === 'stat_0'){
response = template.howToTemplate(received);
template.howToTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
}
callSendAPI(sender_psid, response);
}
// Sends response messages via the Send API
const callSendAPI = (sender_psid, response, cb = null) => {
const callSendAPI = (sender_psid, response, cb=null) => {
console.log('response:'+response);
// Send the HTTP request to the Messenger Platform
request({
......@@ -88,7 +146,9 @@ const callSendAPI = (sender_psid, response, cb = null) => {
message: response
}
}, (err, res, body) => {
if (err) {
if (!err) {
console.log('message sent!')
} else {
console.error("Unable to send message:" + err);
}
});
......
const mysql = require('mysql');
const db_config = require('./config/db-config.json');
let db;
function connectDB() {
if (!db) {
db = mysql.createConnection(db_config);
db.connect(function(err) {
if (!err) {
console.log('Database is connected');
} else {
console.log('Error: ', err);
}
});
}
return db;
}
module.exports = connectDB();
This diff is collapsed. Click to expand it.