Toggle navigation
Toggle navigation
This project
Loading...
Sign in
정민혁
/
TermProject_KakaoBot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
정민혁
2018-12-09 14:51:05 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b171dc7bb9ee84d84282345dec621b06fd1c09c1
b171dc7b
1 parent
4147d1c8
언어 변경 옵션 제작, 한->영,영->한 중 선택가능
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
10 deletions
app.js
routes/message.js
app.js
View file @
b171dc7
...
...
@@ -3,6 +3,7 @@ var express = require('express');
var
path
=
require
(
'path'
);
var
cookieParser
=
require
(
'cookie-parser'
);
var
logger
=
require
(
'morgan'
);
var
bodyparser
=
require
(
'body-parser'
);
var
indexRouter
=
require
(
'./routes/index'
);
var
keyboardRouter
=
require
(
'./routes/keyboard'
);
...
...
@@ -15,8 +16,9 @@ app.set('views', path.join(__dirname, 'views'));
app
.
set
(
'view engine'
,
'ejs'
);
app
.
use
(
logger
(
'dev'
));
app
.
use
(
bodyparser
.
json
());
app
.
use
(
express
.
json
());
app
.
use
(
express
.
urlencoded
({
extended
:
fals
e
}));
app
.
use
(
express
.
urlencoded
({
extended
:
tru
e
}));
app
.
use
(
cookieParser
());
app
.
use
(
express
.
static
(
path
.
join
(
__dirname
,
'public'
)));
...
...
routes/message.js
View file @
b171dc7
var
express
=
require
(
'express'
);
var
request
=
require
(
'request'
);
var
app
=
express
.
Router
();
// Naver Auth Key // to do)) get a new Auth key
var
client_id
=
'is216aNtQ6QLNkWW03bc'
;
var
client_secret
=
'Ot0BMdWXOJ'
;
// Naver API URL
var
api_url
=
'https://openapi.naver.com/v1/language/translate'
;
var
source
=
'ko'
;
var
target
=
'en'
;
//what kind of task to do
var
translate
=
false
;
var
dictionary
=
false
;
...
...
@@ -32,7 +32,7 @@ app.post('/', function(req, res) {
"keyboard"
:
{
"type"
:
"buttons"
,
"buttons"
:[
"한-영"
,
"영-한"
]
}
,
}
});
}
else
if
(
_obj
.
content
==
'번역'
){
translate
=
true
;
...
...
@@ -65,7 +65,7 @@ app.post('/', function(req, res) {
var
options
=
{
url
:
api_url
,
// (source : 번역할 대상), (target: 번역후 언어), 카톡에서 받는 메시지(text)
form
:
{
'source'
:
'ko'
,
'target'
:
'en'
,
'text'
:
req
.
body
.
content
},
form
:
{
'source'
:
source
,
'target'
:
target
,
'text'
:
req
.
body
.
content
},
headers
:
{
'X-Naver-Client-Id'
:
client_id
,
'X-Naver-Client-Secret'
:
client_secret
}
};
// Naver Post API
...
...
@@ -78,24 +78,23 @@ app.post('/', function(req, res) {
console
.
log
(
objBody
.
message
.
result
.
translatedText
);
// Kakao Message API
let
m
a
ssage
=
{
let
m
e
ssage
=
{
"message"
:
{
// Naver API Translate 결과를 Kakao Message
"text"
:
objBody
.
message
.
result
.
translatedText
}
};
// Kakao Message API 전송
res
.
set
({
'content-type'
:
'application/json'
}).
send
(
JSON
.
stringify
(
m
a
ssage
));
}).
send
(
JSON
.
stringify
(
m
e
ssage
));
}
else
{
// Naver Message Error 발생
res
.
status
(
response
.
statusCode
).
end
();
console
.
log
(
'error = '
+
response
.
statusCode
);
let
m
a
ssage
=
{
let
m
e
ssage
=
{
"message"
:
{
"text"
:
response
.
statusCode
},
...
...
@@ -104,13 +103,61 @@ app.post('/', function(req, res) {
// Kakao에 Error Message
res
.
set
({
'content-type'
:
'application/json'
}).
send
(
JSON
.
stringify
(
m
a
ssage
));
}).
send
(
JSON
.
stringify
(
m
e
ssage
));
}
});
}
else
if
(
dictionary
)
{
var
options
=
{
url
:
'https://openapi.naver.com/v1/search/encyc.json?query='
+
encodeURI
(
req
.
query
.
query
)
+
'&display=3&start=1'
,
headers
:
{
'X-Naver-Client-Id'
:
client_id
,
'X-Naver-Client-Secret'
:
client_secret
}
};
request
.
get
(
options
,
function
(
error
,
response
,
body
)
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
objBody
=
JSON
.
parse
(
response
.
body
);
let
message
=
{
"message"
:
{
"text"
:
objBody
.
title
+
" : "
+
objBody
.
description
},
"message_button"
:
{
"label"
:
"백과사전에서 직접보기"
,
"url"
:
objBody
.
link
}
};
}
else
{
res
.
status
(
response
.
statusCode
).
end
();
console
.
log
(
'error = '
+
response
.
statusCode
);
}
});
}
else
if
(
option
)
{
if
(
_obj
.
content
==
"한-영"
)
{
source
=
'ko'
;
target
=
'en'
;
}
else
if
(
_obj
.
content
==
"영-한"
)
{
source
=
"en"
;
target
=
"ko"
;
}
console
.
log
(
source
,
target
);
res
.
json
({
"message"
:
{
"text"
:
source
+
"에서"
+
target
+
"으로 번역합니다."
},
"keyboard"
:
{
"type"
:
"buttons"
,
"buttons"
:[
"설정"
,
"번역"
,
"사전"
]
}
});
}
});
...
...
Please
register
or
login
to post a comment