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-02 16:44:39 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c565f30fd0e67784a1c05af273f284898f7d9003
c565f30f
1 parent
6477c7a5
Enable webhook, Add Init code
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
85 additions
and
116 deletions
.gitignore
app.js
controller.js
package-lock.json
package.json
template.js
.gitignore
View file @
c565f30
node_modules
.idea/
config/
\ No newline at end of file
...
...
app.js
View file @
c565f30
'use strict'
;
let
express
=
require
(
"express"
),
bodyParser
=
require
(
"body-parser"
),
app
=
express
(),
config
=
require
(
'config'
),
controller
=
require
(
'./controller'
);
var
express
=
require
(
"express"
);
var
request
=
require
(
"request"
);
var
bodyParser
=
require
(
"body-parser"
);
var
controller
=
require
(
'./controller'
);
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
var
app
=
express
();
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
app
.
listen
((
process
.
env
.
PORT
||
5000
));
let
users
=
{};
app
.
listen
(
process
.
env
.
PORT
||
8989
,
()
=>
console
.
log
(
'Example app listening on por 8989!'
));
app
.
get
(
'/'
,
(
req
,
res
)
=>
res
.
send
(
'Hello World!'
));
// Adds support for GET requests to our webhook
app
.
get
(
'/webhook'
,
(
req
,
res
)
=>
{
// Your verify token, Should be a random string.
let
VERIFY_TOKEN
=
config
.
get
(
'facebook.page.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 correcct
if
(
mode
===
'subscribe'
&&
token
===
VERIFY_TOKEN
)
{
// Responds with the challenge token from the request
console
.
log
(
'WEBHOOK_VERIFIED'
);
res
.
status
(
200
).
send
(
challenge
);
// Server index page
app
.
get
(
"/"
,
function
(
req
,
res
)
{
res
.
send
(
"Deployed!"
);
});
// Facebook Webhook
// Used for verification
app
.
get
(
"/webhook"
,
function
(
req
,
res
)
{
if
(
req
.
query
[
"hub.verify_token"
]
===
process
.
env
.
VERIFICATION_TOKEN
)
{
console
.
log
(
"Verified webhook"
);
res
.
status
(
200
).
send
(
req
.
query
[
"hub.challenge"
]);
}
else
{
// Responds with '403 Forbidden' if verify tokens do not match
console
.
error
(
"Verification failed. The tokens do not match."
);
res
.
sendStatus
(
403
);
}
}
});
// Creates the endpoint for our webhook
app
.
post
(
'/webhook'
,
(
req
,
res
)
=>
{
...
...
@@ -63,6 +45,7 @@ app.post('/webhook', (req, res) => {
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
)
{
...
...
@@ -71,6 +54,7 @@ app.post('/webhook', (req, res) => {
controller
.
handlePostback
(
sender_psid
,
webhook_event
.
postback
);
}
});
// Returns a '200 OK' response to all requests
res
.
status
(
200
).
send
(
'EVENT_RECEIVED'
);
}
else
{
...
...
@@ -79,4 +63,3 @@ app.post('/webhook', (req, res) => {
}
});
...
...
controller.js
View file @
c565f30
let
request
=
require
(
'request'
),
template
=
require
(
'./template'
),
config
=
require
(
'config'
);
template
=
require
(
'./template'
);
// Views - handle Message, handle Postback
...
...
@@ -9,17 +8,13 @@ exports.handleMessage = (sender_psid, received_message) => {
let
response
;
if
(
received_message
.
text
){
// Create the payload for a basic text message
response
=
template
.
askTemplate
()
response
=
template
.
messageTemplate
();
}
// Sends the reponse message
callSendAPI
(
sender_psid
,
response
);
}
// Handle postback(=button) events
exports
.
handlePostback
=
(
sender_psid
,
received_postback
)
=>
{
let
response
;
...
...
@@ -27,45 +22,39 @@ exports.handlePostback = (sender_psid, received_postback) => {
let
payload
=
received_postback
.
payload
;
// Set the response based on the postback payload
if
(
payload
===
'CAT_PICS'
)
{
response
=
template
.
imageTemplate
(
'cats'
,
sender_psid
);
callSendAPI
(
sender_psid
,
response
,
function
(){
callSendAPI
(
sender_psid
,
template
.
askTemplate
(
'Show me more'
));
});
}
else
if
(
payload
===
'DOG_PICS'
)
{
response
=
template
.
imageTemplate
(
'dogs'
,
sender_psid
);
callSendAPI
(
sender_psid
,
response
,
function
(){
callSendAPI
(
sender_psid
,
template
.
askTemplate
(
'Show me more'
));
});
}
else
if
(
payload
===
'GET_STARTED'
){
if
(
payload
===
'Greeting'
){
response
=
template
.
greetingTemplate
();
callSendAPI
(
sender_psid
,
response
);
}
else
if
(
payload
===
'CHOICE_BY_PROF'
){
response
=
template
.
choicebyprofTemplate
();
callSendAPI
(
sender_psid
,
response
);
}
else
if
(
payload
===
'CHOICE_BY_LECT'
){
response
=
template
.
choicebylectTemplate
();
callSendAPI
(
sender_psid
,
response
);
}
else
if
(
payload
===
'HELP'
){
response
=
template
.
help
();
callSendAPI
(
sender_psid
,
response
);
}
else
if
(
payload
.
match
(
'rate'
)){
response
=
template
.
rate
(
payload
);
callSendAPI
(
sender_psid
,
response
);
}
// Send the message to acknowledge the postback
}
// Sends response messages via the Send API
const
callSendAPI
=
(
sender_psid
,
response
,
cb
=
null
)
=>
{
// Construct the message body
let
request_body
=
{
"recipient"
:
{
"id"
:
sender_psid
},
"message"
:
response
};
// Send the HTTP request to the Messenger Platform
request
({
"ur
i
"
:
"https://graph.facebook.com/v2.6/me/messages"
,
"qs"
:
{
"access_token"
:
config
.
get
(
'facebook.page.access_token'
)
},
"ur
l
"
:
"https://graph.facebook.com/v2.6/me/messages"
,
"qs"
:
{
"access_token"
:
process
.
env
.
PAGE_ACCESS_TOKEN
},
"method"
:
"POST"
,
"json"
:
request_body
},
(
err
,
res
,
body
)
=>
{
if
(
!
err
)
{
if
(
cb
){
cb
();
"json"
:
{
recipient
:
{
id
:
sender_psid
},
message
:
response
}
}
else
{
},
(
err
,
res
,
body
)
=>
{
if
(
err
)
{
console
.
error
(
"Unable to send message:"
+
err
);
}
});
...
...
package-lock.json
View file @
c565f30
This diff is collapsed. Click to expand it.
package.json
View file @
c565f30
{
"name"
:
"spbot"
,
"version"
:
"1.0.0"
,
"description"
:
"
SPBot Server
"
,
"description"
:
""
,
"main"
:
"app.js"
,
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
,
"start"
:
"node app.js"
},
"author"
:
"
WonJun Choi
"
,
"author"
:
""
,
"license"
:
"ISC"
,
"dependencies"
:
{
"body-parser"
:
"^1.19.0"
,
"emoji-strip"
:
"^1.0.1"
,
"express"
:
"^4.17.0"
,
"mongojs"
:
"^2.6.0"
,
"mysql"
:
"^2.17.1"
,
"puppeteer"
:
"^1.17.0"
,
"puppeteer-core"
:
"^1.17.0"
,
"express"
:
"^4.17.1"
,
"mongoose"
:
"^5.5.12"
,
"request"
:
"^2.88.0"
}
}
...
...
template.js
View file @
c565f30
let
images
=
require
(
"./pics"
);
exports
.
messageTemplate
=
(
text
)
=>
{
//메세지
}
let
greetingText
=
"안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."
;
// 인사말
let
greetingTitle
=
[
"교수명으로 검색"
,
"강의명으로 검색"
,
"HELP"
];
...
...
@@ -21,11 +25,11 @@ exports.greetingTemplate = () => {
"type"
:
"postback"
,
"title"
:
greetingTitle
[
1
],
"payload"
:
"CHOICE_BY_LECT"
}
}
,
{
"type"
:
"postback"
,
"title"
:
greetingTitle
[
2
],
"payload"
:
"
CHOICE_BY_
HELP"
"payload"
:
"HELP"
}
]
}
...
...
@@ -33,44 +37,43 @@ exports.greetingTemplate = () => {
}
}
exports
.
askTemplate
=
(
text
)
=>
{
return
{
"attachment"
:{
"type"
:
"template"
,
"payload"
:{
"template_type"
:
"button"
,
"text"
:
text
,
"buttons"
:[
{
"type"
:
"postback"
,
"title"
:
"Cats"
,
"payload"
:
"CAT_PICS"
},
{
"type"
:
"postback"
,
"title"
:
"Dogs"
,
"payload"
:
"DOG_PICS"
}
]
}
}
}
exports
.
choicebyprofTemplate
=
()
=>
{
return
{
text
:
"교수명을 입력하세요."
};
//교수님 성함 사용자 입력 받기
//교수님 목록 띄우기_버튼
//강의명 목록 띄우기_버튼
//->강의평가 띄우기
}
exports
.
choicebylectTemplate
=
()
=>
{
//강의명 사용자 입력 받기
//일치하는 강의 없으면 추측 강의 목록 띄우기_버튼
//max n개 강의 목록 띄우기_버튼
//->그 중에 없으면 교수명 입력으로
//->있으면 강의평가 띄우기
}
exports
.
help
=
()
=>
{
//소개, 뒤로가기
}
exports
.
imageTemplate
=
(
type
,
sender_id
)
=>
{
return
{
"attachment"
:{
"type"
:
"image"
,
"payload"
:{
"url"
:
getImage
(
type
,
sender_id
),
"is_reusable"
:
true
}
}
}
exports
.
rate
=
(
payload
)
=>
{
return
{
text
:
"강의평가입니다."
};
//강의평가
}
let
users
=
{};
const
getImage
=
(
type
,
sender_id
)
=>
{
...
...
Please
register
or
login
to post a comment