Toggle navigation
Toggle navigation
This project
Loading...
Sign in
최재은
/
밀당강의봇
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
김예미
2019-06-04 20:00:25 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c8967dc86bc1c429c09da7a0fc26e852ec5a412e
c8967dc8
1 parent
173a117e
Use Callback function
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
102 additions
and
63 deletions
app.js
controller.js
db.js
template.js
app.js
View file @
c8967dc
...
...
@@ -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) => {
}
});
...
...
controller.js
View file @
c8967dc
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
,
rec
ie
ved_postback
);
}
else
if
(
payload
.
substr
(
0
,
14
)
===
'CHOICE_BY_HELP'
){
handlePostback_help
(
sender_psid
,
rec
ie
ved_postback
);
}
else
if
(
firstchoice
===
'CHOICE_BY_LECT'
){
handlePostback_choiceByLect
(
sender_psid
,
rec
ei
ved_postback
);
}
else
if
(
firstchoice
===
'CHOICE_BY_HELP'
){
handlePostback_help
(
sender_psid
,
rec
ei
ved_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
);
}
});
...
...
db.js
deleted
100644 → 0
View file @
173a117
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
();
template.js
View file @
c8967dc
This diff is collapsed. Click to expand it.
Please
register
or
login
to post a comment