강수인

채팅 서비스 라인으로 변경(처음부터 다시 코딩..)

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var message = require('./routes/message');
var keyboard = require('./routes/keyboard');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/keyboard',keyboard);
app.use('/message',message);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('project:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
2018/11/26
mother project : KakaoBot
추가기능 : 파파고 api를 이용한 카카오톡에서 전송한 사진 속 글자 인식 및 번역
\ No newline at end of file
This diff is collapsed. Click to expand it.
{
"name": "project",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.3",
"debug": "~2.6.9",
"ejs": "~2.5.7",
"express": "^4.16.3",
"morgan": "^1.9.0",
"request": "^2.88.0",
"serve-favicon": "^2.4.5"
},
"main": "app.js",
"devDependencies": {},
"author": "",
"license": "ISC",
"description": ""
}
// Naver WEB Request
var request = require('request');
// Web Package
var express = require('express');
var app = express();
// Kakao Parser
var bodyParser = require('body-parser');
// Naver API URL
var api_url = 'https://openapi.naver.com/v1/papago/n2mt';
// Naver Auth Key
//새로 발급받은 naver papago api id, pw 입력
var client_id = 'xZMx34y7uru1v8lywZ2d';
var client_secret = 'p6L7M7WsH9';
// Parse Application JSON
app.use(bodyParser.json());
// Parse Application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true}));
// Kakao Keyboard API
app.get('/keyboard', function(req, res) {
const menu = {
"type": 'buttons',
"buttons": ["시작"]
};
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(menu));
});
// Kakao Message API
app.post('/message', function(req, res) {
const _obj = {
user_key: req.body.user_key,
type: req.body.type,
content: req.body.content
};
console.log(_obj.content)
// Naver Papago Translate
var options = {
url: api_url,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form: {'source':'ko', 'target':'en', 'text':req.body.content},
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
// Naver Post API
request.post(options, function(error, response, body){
// Translate API Sucess
if(!error && response.statusCode == 200){
// JSON
var objBody = JSON.parse(response.body);
// Message 잘 찍히는지 확인
console.log(objBody.message.result.translatedText);
// Kakao Message API
let massage = {
"message": {
// Naver API Translate 결과를 Kakao Message
"text": objBody.message.result.translatedText
},
};
// Kakao Message API 전송
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(massage));
}else{
// Naver Message Error 발생
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
let massage = {
"message": {
"text": response.statusCode
},
};
// Kakao에 Error Message
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(massage));
}
});
});
// whatsupdevelop.com 실행
app.listen(3000, function(){
});