김예미

Enable webhook, Add Init code

node_modules
.idea/
config/
\ No newline at end of file
......
'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!");
});
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
// 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);
}
});
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
......@@ -62,6 +44,7 @@ app.post('/webhook', (req, res) => {
// 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
......@@ -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) => {
}
});
......
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'){
response = template.greetingTemplate();
callSendAPI(sender_psid, response);
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({
"uri": "https://graph.facebook.com/v2.6/me/messages",
"qs": { "access_token": config.get('facebook.page.access_token') },
"url": "https://graph.facebook.com/v2.6/me/messages",
"qs": { "access_token": process.env.PAGE_ACCESS_TOKEN },
"method": "POST",
"json": request_body
"json": {
recipient: {id: sender_psid},
message: response
}
}, (err, res, body) => {
if (!err) {
if(cb){
cb();
}
} else {
if (err) {
console.error("Unable to send message:" + err);
}
});
......
This diff is collapsed. Click to expand it.
{
"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"
}
}
......
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) => {
......