김예미

Enable webhook, Add Init code

1 node_modules 1 node_modules
2 -.idea/
3 -config/
...\ No newline at end of file ...\ No newline at end of file
......
1 -'use strict'; 1 +var express = require("express");
2 -let express = require("express"), 2 +var request = require("request");
3 - bodyParser= require("body-parser"), 3 +var bodyParser = require("body-parser");
4 - app = express(), 4 +var controller = require('./controller');
5 - config = require('config'),
6 - controller = require('./controller');
7 5
8 -app.use(bodyParser.urlencoded({ extended : false})); 6 +var app = express();
7 +app.use(bodyParser.urlencoded({extended: false}));
9 app.use(bodyParser.json()); 8 app.use(bodyParser.json());
9 +app.listen((process.env.PORT || 5000));
10 10
11 -let users = {}; 11 +// Server index page
12 - 12 +app.get("/", function (req, res) {
13 -app.listen(process.env.PORT || 8989, () => console.log('Example app listening on por 8989!')); 13 + res.send("Deployed!");
14 - 14 +});
15 -app.get('/', (req, res) => res.send('Hello World!'));
16 -
17 -
18 -// Adds support for GET requests to our webhook
19 -app.get('/webhook', (req, res) => {
20 -
21 - // Your verify token, Should be a random string.
22 - let VERIFY_TOKEN = config.get('facebook.page.verify_token');
23 -
24 - // Parse the query params
25 - let mode = req.query['hub.mode'];
26 - let token = req.query['hub.verify_token'];
27 - let challenge = req.query['hub.challenge'];
28 -
29 -
30 - // Checks if a token and mode is in the query string of the request
31 - if(mode && token) {
32 -
33 - // Checks the mode and token sent is correcct
34 - if (mode === 'subscribe' && token === VERIFY_TOKEN) {
35 -
36 - // Responds with the challenge token from the request
37 - console.log('WEBHOOK_VERIFIED');
38 - res.status(200).send(challenge);
39 15
16 +// Facebook Webhook
17 +// Used for verification
18 +app.get("/webhook", function (req, res) {
19 + if (req.query["hub.verify_token"] === process.env.VERIFICATION_TOKEN) {
20 + console.log("Verified webhook");
21 + res.status(200).send(req.query["hub.challenge"]);
40 } else { 22 } else {
41 - // Responds with '403 Forbidden' if verify tokens do not match 23 + console.error("Verification failed. The tokens do not match.");
42 res.sendStatus(403); 24 res.sendStatus(403);
43 } 25 }
44 - }
45 }); 26 });
46 27
28 +
47 // Creates the endpoint for our webhook 29 // Creates the endpoint for our webhook
48 app.post('/webhook', (req, res) => { 30 app.post('/webhook', (req, res) => {
49 31
...@@ -63,6 +45,7 @@ app.post('/webhook', (req, res) => { ...@@ -63,6 +45,7 @@ app.post('/webhook', (req, res) => {
63 let sender_psid = webhook_event.sender.id; 45 let sender_psid = webhook_event.sender.id;
64 console.log('Sender PSID: ' + sender_psid); 46 console.log('Sender PSID: ' + sender_psid);
65 47
48 +
66 // Check if the event is a message or postback and 49 // Check if the event is a message or postback and
67 // pass the event to the appropriate handler function 50 // pass the event to the appropriate handler function
68 if (webhook_event.message) { 51 if (webhook_event.message) {
...@@ -71,6 +54,7 @@ app.post('/webhook', (req, res) => { ...@@ -71,6 +54,7 @@ app.post('/webhook', (req, res) => {
71 controller.handlePostback(sender_psid, webhook_event.postback); 54 controller.handlePostback(sender_psid, webhook_event.postback);
72 } 55 }
73 }); 56 });
57 +
74 // Returns a '200 OK' response to all requests 58 // Returns a '200 OK' response to all requests
75 res.status(200).send('EVENT_RECEIVED'); 59 res.status(200).send('EVENT_RECEIVED');
76 } else { 60 } else {
...@@ -79,4 +63,3 @@ app.post('/webhook', (req, res) => { ...@@ -79,4 +63,3 @@ app.post('/webhook', (req, res) => {
79 } 63 }
80 }); 64 });
81 65
82 -
......
1 let request = require('request'), 1 let request = require('request'),
2 - template = require('./template'), 2 + template = require('./template');
3 - config = require('config');
4 3
5 // Views - handle Message, handle Postback 4 // Views - handle Message, handle Postback
6 5
...@@ -9,17 +8,13 @@ exports.handleMessage = (sender_psid, received_message) => { ...@@ -9,17 +8,13 @@ exports.handleMessage = (sender_psid, received_message) => {
9 let response; 8 let response;
10 9
11 if(received_message.text){ 10 if(received_message.text){
12 - 11 + response = template.messageTemplate();
13 - // Create the payload for a basic text message
14 - response = template.askTemplate()
15 } 12 }
16 13
17 // Sends the reponse message 14 // Sends the reponse message
18 callSendAPI(sender_psid, response); 15 callSendAPI(sender_psid, response);
19 } 16 }
20 17
21 -
22 -// Handle postback(=button) events
23 exports.handlePostback = (sender_psid, received_postback) => { 18 exports.handlePostback = (sender_psid, received_postback) => {
24 let response; 19 let response;
25 20
...@@ -27,45 +22,39 @@ exports.handlePostback = (sender_psid, received_postback) => { ...@@ -27,45 +22,39 @@ exports.handlePostback = (sender_psid, received_postback) => {
27 let payload = received_postback.payload; 22 let payload = received_postback.payload;
28 23
29 // Set the response based on the postback payload 24 // Set the response based on the postback payload
30 - if (payload === 'CAT_PICS') { 25 + if(payload === 'Greeting'){
31 - response = template.imageTemplate('cats', sender_psid);
32 - callSendAPI(sender_psid, response, function(){
33 - callSendAPI(sender_psid, template.askTemplate('Show me more'));
34 - });
35 - } else if (payload === 'DOG_PICS') {
36 - response = template.imageTemplate('dogs', sender_psid);
37 - callSendAPI(sender_psid, response, function(){
38 - callSendAPI(sender_psid, template.askTemplate('Show me more'));
39 - });
40 - } else if(payload === 'GET_STARTED'){
41 response = template.greetingTemplate(); 26 response = template.greetingTemplate();
42 callSendAPI(sender_psid, response); 27 callSendAPI(sender_psid, response);
28 + }else if(payload === 'CHOICE_BY_PROF'){
29 + response = template.choicebyprofTemplate();
30 + callSendAPI(sender_psid, response);
31 + }else if(payload === 'CHOICE_BY_LECT'){
32 + response = template.choicebylectTemplate();
33 + callSendAPI(sender_psid, response);
34 + }else if(payload === 'HELP'){
35 + response = template.help();
36 + callSendAPI(sender_psid, response);
37 + }else if(payload.match('rate')){
38 + response = template.rate(payload);
39 + callSendAPI(sender_psid, response);
43 } 40 }
44 - // Send the message to acknowledge the postback
45 } 41 }
46 42
43 +
47 // Sends response messages via the Send API 44 // Sends response messages via the Send API
48 const callSendAPI = (sender_psid, response, cb = null) => { 45 const callSendAPI = (sender_psid, response, cb = null) => {
49 - // Construct the message body
50 - let request_body = {
51 - "recipient": {
52 - "id": sender_psid
53 - },
54 - "message": response
55 - };
56 46
57 // Send the HTTP request to the Messenger Platform 47 // Send the HTTP request to the Messenger Platform
58 request({ 48 request({
59 - "uri": "https://graph.facebook.com/v2.6/me/messages", 49 + "url": "https://graph.facebook.com/v2.6/me/messages",
60 - "qs": { "access_token": config.get('facebook.page.access_token') }, 50 + "qs": { "access_token": process.env.PAGE_ACCESS_TOKEN },
61 "method": "POST", 51 "method": "POST",
62 - "json": request_body 52 + "json": {
63 - }, (err, res, body) => { 53 + recipient: {id: sender_psid},
64 - if (!err) { 54 + message: response
65 - if(cb){
66 - cb();
67 } 55 }
68 - } else { 56 + }, (err, res, body) => {
57 + if (err) {
69 console.error("Unable to send message:" + err); 58 console.error("Unable to send message:" + err);
70 } 59 }
71 }); 60 });
......
This diff is collapsed. Click to expand it.
1 { 1 {
2 "name": "spbot", 2 "name": "spbot",
3 "version": "1.0.0", 3 "version": "1.0.0",
4 - "description": "SPBot Server", 4 + "description": "",
5 "main": "app.js", 5 "main": "app.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1", 7 "test": "echo \"Error: no test specified\" && exit 1",
8 "start": "node app.js" 8 "start": "node app.js"
9 }, 9 },
10 - "author": "WonJun Choi", 10 + "author": "",
11 "license": "ISC", 11 "license": "ISC",
12 "dependencies": { 12 "dependencies": {
13 "body-parser": "^1.19.0", 13 "body-parser": "^1.19.0",
14 - "emoji-strip": "^1.0.1", 14 + "express": "^4.17.1",
15 - "express": "^4.17.0", 15 + "mongoose": "^5.5.12",
16 - "mongojs": "^2.6.0",
17 - "mysql": "^2.17.1",
18 - "puppeteer": "^1.17.0",
19 - "puppeteer-core": "^1.17.0",
20 "request": "^2.88.0" 16 "request": "^2.88.0"
21 } 17 }
22 } 18 }
......
1 let images = require("./pics"); 1 let images = require("./pics");
2 2
3 +exports.messageTemplate = (text) => {
4 + //메세지
5 +}
6 +
3 let greetingText = "안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."; // 인사말 7 let greetingText = "안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."; // 인사말
4 let greetingTitle = ["교수명으로 검색", "강의명으로 검색", "HELP"]; 8 let greetingTitle = ["교수명으로 검색", "강의명으로 검색", "HELP"];
5 9
...@@ -21,11 +25,11 @@ exports.greetingTemplate = () => { ...@@ -21,11 +25,11 @@ exports.greetingTemplate = () => {
21 "type":"postback", 25 "type":"postback",
22 "title":greetingTitle[1], 26 "title":greetingTitle[1],
23 "payload":"CHOICE_BY_LECT" 27 "payload":"CHOICE_BY_LECT"
24 - } 28 + },
25 { 29 {
26 "type":"postback", 30 "type":"postback",
27 "title":greetingTitle[2], 31 "title":greetingTitle[2],
28 - "payload":"CHOICE_BY_HELP" 32 + "payload":"HELP"
29 } 33 }
30 ] 34 ]
31 } 35 }
...@@ -33,44 +37,43 @@ exports.greetingTemplate = () => { ...@@ -33,44 +37,43 @@ exports.greetingTemplate = () => {
33 } 37 }
34 } 38 }
35 39
36 -exports.askTemplate = (text) => { 40 +exports.choicebyprofTemplate = () => {
37 - return { 41 + return {text: "교수명을 입력하세요."};
38 - "attachment":{ 42 + //교수님 성함 사용자 입력 받기
39 - "type":"template", 43 + //교수님 목록 띄우기_버튼
40 - "payload":{ 44 + //강의명 목록 띄우기_버튼
41 - "template_type":"button", 45 + //->강의평가 띄우기
42 - "text": text,
43 - "buttons":[
44 - {
45 - "type":"postback",
46 - "title":"Cats",
47 - "payload":"CAT_PICS"
48 - },
49 - {
50 - "type":"postback",
51 - "title":"Dogs",
52 - "payload":"DOG_PICS"
53 - }
54 - ]
55 - }
56 - }
57 - }
58 } 46 }
59 47
48 +exports.choicebylectTemplate = () => {
49 + //강의명 사용자 입력 받기
50 + //일치하는 강의 없으면 추측 강의 목록 띄우기_버튼
51 + //max n개 강의 목록 띄우기_버튼
52 + //->그 중에 없으면 교수명 입력으로
53 + //->있으면 강의평가 띄우기
54 +}
60 55
56 +exports.help = () => {
57 + //소개, 뒤로가기
58 +}
61 59
62 -exports.imageTemplate= (type, sender_id) => { 60 +exports.rate = (payload) => {
63 - return { 61 + return {text:"강의평가입니다."};
64 - "attachment":{ 62 + //강의평가
65 - "type":"image",
66 - "payload":{
67 - "url": getImage(type, sender_id),
68 - "is_reusable":true
69 - }
70 - }
71 - }
72 } 63 }
73 64
65 +
66 +
67 +
68 +
69 +
70 +
71 +
72 +
73 +
74 +
75 +
76 +
74 let users = {}; 77 let users = {};
75 78
76 const getImage= (type, sender_id) => { 79 const getImage= (type, sender_id) => {
......