controller.js
4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var request = require('request');
var template = require('./template.js');
exports.handleMessage=function(sender_psid, received_message) {
var response;
var text = received_message.text;
if(text === "처음으로"){
template.greetingTemplate(function(result, res){
if(result==true){
response=res;
callSendAPI(sender_psid, response)
}
});
}else if(text === "감사합니다"){
response={text: "미천한 재주입니다."};
callSendAPI(sender_psid, response)
}else{
template.choiceLectProfTemplate(text, function(result, res){
if(result==true){
response=res;
console.log("text2:",response)
callSendAPI(sender_psid, response);
}
});
}
}
exports.handlePostback =function(sender_psid, received_postback) {
var response;
// Get the payload for the postback
var payload = received_postback.payload;
var firstchoice = payload.substr(0,14);
// Set the response based on the postback payload
if(payload === 'Greeting'){
console.log('Greeting');
template.greetingTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
}
});
callSendAPI(sender_psid, response);
}else if(firstchoice === 'CHOICE_BY_PROF'){
handlePostback_choiceByProf(sender_psid, received_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'){
template.moreRateTemplate(payload, function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}
}
const handlePostback_choiceByProf = function(sender_psid, received){
var response;
var payload = received.payload;
var stat = payload.substr(14,6);
if(stat === 'stat_0'){
template.getProfNameTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}else if(stat === 'stat_2'){
template.moreProfTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}else if(stat === 'stat_3'){
template.rateTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}
}
const handlePostback_choiceByLect = function(sender_psid, received){
var response;
var payload = received.payload;
var stat = payload.substr(14,6);
if(stat === 'stat_0'){
template.getLectNameTemplate(function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}else if(stat === 'stat_2'){
template.moreLectTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}else if(stat === 'stat_3'){
template.rateTemplate(payload,function(result, res){
if(result==true){
console.log(res);
response=res;
callSendAPI(sender_psid, response);
}
});
}
}
const handlePostback_help = function(sender_psid, received) {
var response;
var payload = received.payload;
var stat = payload.substr(14,6);
if(stat === 'stat_0'){
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) => {
console.log('response:'+response);
// Send the HTTP request to the Messenger Platform
request({
"url": "https://graph.facebook.com/v2.6/me/messages",
"qs": { "access_token": process.env.PAGE_ACCESS_TOKEN },
"method": "POST",
"json": {
recipient: {id: sender_psid},
message: response
}
}, (err, res, body) => {
if (!err) {
console.log('message sent!');
console.log('res: ',response);
} else {
console.error("Unable to send message:" + err);
}
});
}