강수인

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

1 -var express = require('express');
2 -var path = require('path');
3 -var favicon = require('serve-favicon');
4 -var logger = require('morgan');
5 -var cookieParser = require('cookie-parser');
6 -var bodyParser = require('body-parser');
7 -
8 -var message = require('./routes/message');
9 -var keyboard = require('./routes/keyboard');
10 -var app = express();
11 -
12 -// view engine setup
13 -app.set('views', path.join(__dirname, 'views'));
14 -app.set('view engine', 'ejs');
15 -
16 -// uncomment after placing your favicon in /public
17 -//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
18 -app.use(logger('dev'));
19 -app.use(bodyParser.json());
20 -app.use(bodyParser.urlencoded({ extended: false }));
21 -app.use(cookieParser());
22 -app.use(express.static(path.join(__dirname, 'public')));
23 -
24 -app.use('/keyboard',keyboard);
25 -app.use('/message',message);
26 -
27 -// catch 404 and forward to error handler
28 -app.use(function(req, res, next) {
29 - var err = new Error('Not Found');
30 - err.status = 404;
31 - next(err);
32 -});
33 -
34 -// error handler
35 -app.use(function(err, req, res, next) {
36 - // set locals, only providing error in development
37 - res.locals.message = err.message;
38 - res.locals.error = req.app.get('env') === 'development' ? err : {};
39 -
40 - // render the error page
41 - res.status(err.status || 500);
42 - res.render('error');
43 -});
44 -
45 -module.exports = app;
46 -
1 -#!/usr/bin/env node
2 -
3 -/**
4 - * Module dependencies.
5 - */
6 -
7 -var app = require('../app');
8 -var debug = require('debug')('project:server');
9 -var http = require('http');
10 -
11 -/**
12 - * Get port from environment and store in Express.
13 - */
14 -
15 -var port = normalizePort(process.env.PORT || '3000');
16 -app.set('port', port);
17 -
18 -/**
19 - * Create HTTP server.
20 - */
21 -
22 -var server = http.createServer(app);
23 -
24 -/**
25 - * Listen on provided port, on all network interfaces.
26 - */
27 -
28 -server.listen(port);
29 -server.on('error', onError);
30 -server.on('listening', onListening);
31 -
32 -/**
33 - * Normalize a port into a number, string, or false.
34 - */
35 -
36 -function normalizePort(val) {
37 - var port = parseInt(val, 10);
38 -
39 - if (isNaN(port)) {
40 - // named pipe
41 - return val;
42 - }
43 -
44 - if (port >= 0) {
45 - // port number
46 - return port;
47 - }
48 -
49 - return false;
50 -}
51 -
52 -/**
53 - * Event listener for HTTP server "error" event.
54 - */
55 -
56 -function onError(error) {
57 - if (error.syscall !== 'listen') {
58 - throw error;
59 - }
60 -
61 - var bind = typeof port === 'string'
62 - ? 'Pipe ' + port
63 - : 'Port ' + port;
64 -
65 - // handle specific listen errors with friendly messages
66 - switch (error.code) {
67 - case 'EACCES':
68 - console.error(bind + ' requires elevated privileges');
69 - process.exit(1);
70 - break;
71 - case 'EADDRINUSE':
72 - console.error(bind + ' is already in use');
73 - process.exit(1);
74 - break;
75 - default:
76 - throw error;
77 - }
78 -}
79 -
80 -/**
81 - * Event listener for HTTP server "listening" event.
82 - */
83 -
84 -function onListening() {
85 - var addr = server.address();
86 - var bind = typeof addr === 'string'
87 - ? 'pipe ' + addr
88 - : 'port ' + addr.port;
89 - debug('Listening on ' + bind);
90 -}
1 -2018/11/26
2 -mother project : KakaoBot
3 -추가기능 : 파파고 api를 이용한 카카오톡에서 전송한 사진 속 글자 인식 및 번역
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 -{
2 - "name": "project",
3 - "version": "0.0.0",
4 - "private": true,
5 - "scripts": {
6 - "start": "node ./bin/www"
7 - },
8 - "dependencies": {
9 - "body-parser": "^1.18.3",
10 - "cookie-parser": "^1.4.3",
11 - "debug": "~2.6.9",
12 - "ejs": "~2.5.7",
13 - "express": "^4.16.3",
14 - "morgan": "^1.9.0",
15 - "request": "^2.88.0",
16 - "serve-favicon": "^2.4.5"
17 - },
18 - "main": "app.js",
19 - "devDependencies": {},
20 - "author": "",
21 - "license": "ISC",
22 - "description": ""
23 -}
1 -// Naver WEB Request
2 -var request = require('request');
3 -
4 -// Web Package
5 -var express = require('express');
6 -var app = express();
7 -
8 -// Kakao Parser
9 -var bodyParser = require('body-parser');
10 -
11 -// Naver API URL
12 -var api_url = 'https://openapi.naver.com/v1/papago/n2mt';
13 -
14 -// Naver Auth Key
15 -//새로 발급받은 naver papago api id, pw 입력
16 -var client_id = 'xZMx34y7uru1v8lywZ2d';
17 -var client_secret = 'p6L7M7WsH9';
18 -
19 -// Parse Application JSON
20 -app.use(bodyParser.json());
21 -
22 -// Parse Application/x-www-form-urlencoded
23 -app.use(bodyParser.urlencoded({ extended: true}));
24 -
25 -
26 -
27 -
28 -// Kakao Keyboard API
29 -app.get('/keyboard', function(req, res) {
30 - const menu = {
31 - "type": 'buttons',
32 - "buttons": ["시작"]
33 - };
34 - res.set({
35 - 'content-type': 'application/json'
36 - }).send(JSON.stringify(menu));
37 -});
38 -
39 -// Kakao Message API
40 -app.post('/message', function(req, res) {
41 - const _obj = {
42 - user_key: req.body.user_key,
43 - type: req.body.type,
44 - content: req.body.content
45 - };
46 -
47 - console.log(_obj.content)
48 -
49 - // Naver Papago Translate
50 - var options = {
51 - url: api_url,
52 - // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
53 - form: {'source':'ko', 'target':'en', 'text':req.body.content},
54 - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
55 - };
56 -
57 - // Naver Post API
58 - request.post(options, function(error, response, body){
59 - // Translate API Sucess
60 - if(!error && response.statusCode == 200){
61 - // JSON
62 - var objBody = JSON.parse(response.body);
63 - // Message 잘 찍히는지 확인
64 - console.log(objBody.message.result.translatedText);
65 -
66 - // Kakao Message API
67 - let massage = {
68 - "message": {
69 - // Naver API Translate 결과를 Kakao Message
70 - "text": objBody.message.result.translatedText
71 - },
72 - };
73 -
74 - // Kakao Message API 전송
75 - res.set({
76 - 'content-type': 'application/json'
77 - }).send(JSON.stringify(massage));
78 - }else{
79 - // Naver Message Error 발생
80 - res.status(response.statusCode).end();
81 - console.log('error = ' + response.statusCode);
82 -
83 - let massage = {
84 - "message": {
85 - "text": response.statusCode
86 - },
87 - };
88 -
89 - // Kakao에 Error Message
90 - res.set({
91 - 'content-type': 'application/json'
92 - }).send(JSON.stringify(massage));
93 - }
94 - });
95 -});
96 -
97 -// whatsupdevelop.com 실행
98 -app.listen(3000, function(){
99 -});