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-05-31 23:47:07 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
93602080f5731d217610016e9e2773854cddf067
93602080
2 parents
d61dcc57
bf75d665
Merge branch 'master' of
http://khuhub.khu.ac.kr/2016104172/LINEBOT
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
229 additions
and
70 deletions
.gitignore
app.js
pics.js
.gitignore
View file @
9360208
node_modules
.idea/
config/
\ No newline at end of file
...
...
app.js
View file @
9360208
var
express
=
require
(
"express"
);
var
request
=
require
(
"request"
);
var
bodyParser
=
require
(
"body-parser"
);
'use strict'
;
let
express
=
require
(
"express"
),
bodyParser
=
require
(
"body-parser"
),
app
=
express
(),
request
=
require
(
'request'
),
config
=
require
(
'config'
),
images
=
require
(
"./pics"
);
var
app
=
express
();
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
app
.
listen
((
process
.
env
.
PORT
||
5000
));
// Server index page
app
.
get
(
"/"
,
function
(
req
,
res
)
{
res
.
send
(
"Deployed!"
);
});
let
users
=
{};
// 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
{
console
.
error
(
"Verification failed. The tokens do not match."
);
res
.
sendStatus
(
403
);
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
=
"2016104171"
;
// 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
);
}
else
{
// Responds with '403 Forbidden' if verify tokens do not match
res
.
sendStatus
(
403
);
}
}
});
// All callbacks for Messenger will be POST-ed here
app
.
post
(
"/webhook"
,
function
(
req
,
res
)
{
// Make sure this is a page subscription
if
(
req
.
body
.
object
==
"page"
)
{
// Iterate over each entry
// There may be multiple entries if batched
req
.
body
.
entry
.
forEach
(
function
(
entry
)
{
// Iterate over each messaging event
entry
.
messaging
.
forEach
(
function
(
event
)
{
if
(
event
.
postback
)
{
processPostback
(
event
);
}
});
// Creates the endpoint for our webhook
app
.
post
(
'/webhook'
,
(
req
,
res
)
=>
{
let
body
=
req
.
body
;
if
(
body
.
object
===
'page'
)
{
// Iterates over each entry - there may be multiple if batched
body
.
entry
.
forEach
(
function
(
entry
)
{
// 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
];
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
)
{
handleMessage
(
sender_psid
,
webhook_event
.
message
);
}
else
if
(
webhook_event
.
postback
)
{
handlePostback
(
sender_psid
,
webhook_event
.
postback
);
}
});
res
.
sendStatus
(
200
);
// 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
);
}
});
function
processPostback
(
event
)
{
var
senderId
=
event
.
sender
.
id
;
var
payload
=
event
.
postback
.
payload
;
if
(
payload
===
"Greeting"
)
{
// Get user's first name from the User Profile API
// and include it in the greeting
request
({
url
:
"https://graph.facebook.com/v2.6/"
+
senderId
,
qs
:
{
access_token
:
process
.
env
.
PAGE_ACCESS_TOKEN
,
fields
:
"first_name"
},
method
:
"GET"
},
function
(
error
,
response
,
body
)
{
var
greeting
=
""
;
if
(
error
)
{
console
.
log
(
"Error getting user's name: "
+
error
);
}
else
{
var
bodyObj
=
JSON
.
parse
(
body
);
name
=
bodyObj
.
first_name
;
greeting
=
"Hi "
+
name
+
". "
;
}
var
message
=
greeting
+
"My name is SP Movie Bot. I can tell you various details regarding movies. What movie would you like to know about?"
;
sendMessage
(
senderId
,
{
text
:
message
});
// Views - handle Message, handle Postback
// Handles message events
const
handleMessage
=
(
sender_psid
,
received_message
)
=>
{
let
response
;
if
(
received_message
.
text
){
// Create the payload for a basic text message
response
=
askTemplate
()
}
// Sends the reponse message
callSendAPI
(
sender_psid
,
response
;
}
const
handlePostback
=
(
sender_psid
,
received_postback
)
=>
{
let
response
;
// Get the payload for the postback
let
payload
=
received_postback
.
payload
;
// Set the response based on the postback payload
if
(
payload
===
'CAT_PICS'
)
{
response
=
imageTemplate
(
'cats'
,
sender_psid
);
callSendAPI
(
sender_psid
,
response
,
function
(){
callSendAPI
(
sender_psid
,
askTemplate
(
'Show me more'
));
});
}
else
if
(
payload
===
'DOG_PICS'
)
{
response
=
imageTemplate
(
'dogs'
,
sender_psid
);
callSendAPI
(
sender_psid
,
response
,
function
(){
callSendAPI
(
sender_psid
,
askTemplate
(
'Show me more'
));
});
}
else
if
(
payload
===
'GET_STARTED'
){
response
=
askTemplate
(
'Are you a Cat or Dog Person?'
);
callSendAPI
(
sender_psid
,
response
);
}
// Send the message to acknowledge the postback
}
// sends message to user
function
sendMessage
(
recipientId
,
message
)
{
request
({
url
:
"https://graph.facebook.com/v2.6/me/messages"
,
qs
:
{
access_token
:
process
.
env
.
PAGE_ACCESS_TOKEN
},
method
:
"POST"
,
json
:
{
recipient
:
{
id
:
recipientId
},
message
:
message
,
const
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"
}
]
}
}
},
function
(
error
,
response
,
body
)
{
if
(
error
)
{
console
.
log
(
"Error sending message: "
+
response
.
error
);
}
}
// 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
({
"uri"
:
"https://graph.facebook.com/v2.6/me/messages"
,
"qs"
:
{
"access_token"
:
config
.
get
(
'facebook.page.access_token'
)
},
"method"
:
"POST"
,
"json"
:
request_body
},
(
err
,
res
,
body
)
=>
{
if
(
!
err
)
{
if
(
cb
){
cb
();
}
}
else
{
console
.
error
(
"Unable to send message:"
+
err
);
}
});
}
const
imageTemplate
=
(
type
,
sender_id
)
=>
{
return
{
"attachment"
:{
"type"
:
"image"
,
"payload"
:{
"url"
:
getImage
(
type
,
sender_id
),
"is_reusable"
:
true
}
}
}
}
let
users
=
{};
const
getImage
=
(
type
,
sender_id
)
=>
{
// create user if doesn't exist
if
(
users
[
sender_id
]
===
undefined
){
users
=
Object
.
assign
({
[
sender_id
]
:
{
'cats_count'
:
0
,
'dogs_count'
:
0
}
},
users
);
}
let
count
=
images
[
type
].
length
,
// total available images by type
user
=
users
[
sender_id
],
// // user requesting image
user_type_count
=
user
[
type
+
'_count'
];
// update user before returning image
let
updated_user
=
{
[
sender_id
]
:
Object
.
assign
(
user
,
{
[
type
+
'_count'
]
:
count
===
user_type_count
+
1
?
0
:
user_type_count
+
1
})
};
// update users
users
=
Object
.
assign
(
users
,
updated_user
);
console
.
log
(
users
);
return
images
[
type
][
user_type_count
];
}
...
...
pics.js
0 → 100644
View file @
9360208
module
.
exports
=
{
cats
:
[
'https://i.imgur.com/Qbg7CeM.jpg'
,
'https://i.imgur.com/nUzkpJY.jpg'
,
'https://i.imgur.com/NpDcKph.jpg'
,
'https://i.imgur.com/oJtSDaO.jpg'
,
'https://i.redd.it/82ajpsrd17111.jpg'
,
'https://i.redd.it/00km1d2rt0111.jpg'
,
'https://i.redd.it/rdbavhp0y7111.jpg'
,
'https://i.redd.it/5hn3mg0n98111.jpg'
,
'https://i.redd.it/d23pb8mta6111.jpg'
,
'https://i.redd.it/d2gyrwgy7oz01.jpg'
,
'https://i.redd.it/z4sgl84q72z01.jpg'
,
'https://i.redd.it/wvykzo8n1cy01.jpg'
],
dogs
:
[
'https://i.redd.it/6tjihi2qe7111.jpg'
,
'https://i.imgur.com/etRCs56.jpg'
,
'https://i.redd.it/nibw50f8y4111.jpg'
,
'https://i.redd.it/izcvnvj1o7111.jpg'
,
'https://i.redd.it/eqs1g9dldz011.jpg'
,
'https://i.redd.it/civ9dnu9u1111.jpg'
,
'https://i.redd.it/kk03qwclkp011.jpg'
,
'https://i.redd.it/2694pupjne011.jpg'
,
'https://i.redd.it/qk49ls5y6oy01.jpg'
,
'https://i.imgur.com/oM3mKgB.jpg'
,
'https://i.redd.it/8kx2riaulux01.jpg'
]
};
Please
register
or
login
to post a comment