Showing
1 changed file
with
117 additions
and
0 deletions
postback.js
0 → 100644
1 | +var express = require('express'); | ||
2 | +var app = express(); | ||
3 | +const line = require('@line/bot-sdk'); | ||
4 | +const config = require('./config'); | ||
5 | + | ||
6 | +//papago api | ||
7 | +var request = require('request'); | ||
8 | + | ||
9 | +//번역 api_url | ||
10 | +var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; | ||
11 | + | ||
12 | +//언어감지 api_url | ||
13 | +var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | ||
14 | + | ||
15 | +//eng grammar check | ||
16 | + | ||
17 | +var EnGrammarCheck_api_url = 'https://api.textgears.com/check.php'; | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | +// create LINE SDK client | ||
22 | +const client = new line.Client(config.line_config); | ||
23 | + | ||
24 | +// create Express app | ||
25 | +// about Express itself: https://expressjs.com/ | ||
26 | + | ||
27 | +// register a webhook handler with middleware | ||
28 | +// about the middleware, please refer to doc | ||
29 | +app.post('/webhook', line.middleware(config.line_config), (req, res) => { | ||
30 | + Promise | ||
31 | + .all(req.body.events.map(handleEvent)) | ||
32 | + .then((result) => res.json(result)) | ||
33 | + .catch((err) => { | ||
34 | + console.error(err); res.status(200).end(); | ||
35 | + }); | ||
36 | +}); | ||
37 | + | ||
38 | +function handleEvent(event) { | ||
39 | + if (event.type !== 'message' || event.message.type !== 'text' ) { | ||
40 | + // ignore non-text-message event | ||
41 | + return Promise.resolve(null); | ||
42 | + } | ||
43 | + return new Promise(function(resolve, reject) { | ||
44 | + //언어 감지 option | ||
45 | + var detect_options = { | ||
46 | + url : languagedetect_api_url, | ||
47 | + form : {'query': event.message.text}, | ||
48 | + headers: config.naver_header | ||
49 | + }; | ||
50 | + //papago 언어 감지 | ||
51 | + request.post(detect_options,function(error,response,body){ | ||
52 | + console.log(response.statusCode); | ||
53 | + if(!error && response.statusCode == 200){ | ||
54 | + var detect_body = JSON.parse(response.body); | ||
55 | + var source = ''; | ||
56 | + var target = ''; | ||
57 | + var result = { type: 'text', text:''}; | ||
58 | + | ||
59 | + //언어 감지가 제대로 됐는지 확인 | ||
60 | + console.log(detect_body.langCode); | ||
61 | + | ||
62 | + | ||
63 | +app.post('/message',function(req,res){ var msg = req.body.content; | ||
64 | + console.log('전달받은 메시지: ' +msg); | ||
65 | + | ||
66 | + var sent ={}; | ||
67 | + | ||
68 | + switch(msg) | ||
69 | + { | ||
70 | + case '번역': | ||
71 | + case '1': | ||
72 | + if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ | ||
73 | + source = detect_body.langCode == 'ko' ? 'ko':'en'; | ||
74 | + target = source == 'ko' ? 'en':'ko'; | ||
75 | + //papago 번역 option | ||
76 | + var options = { | ||
77 | + url: translate_api_url, | ||
78 | + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | ||
79 | + form: {'source':source, 'target':target, 'text':event.message.text}, | ||
80 | + headers: config.naver_header | ||
81 | + }; | ||
82 | + request.post(options, function(error, response, body){ | ||
83 | + // Translate API Sucess | ||
84 | + if(!error && response.statusCode == 200){ | ||
85 | + // JSON | ||
86 | + var objBody = JSON.parse(response.body); | ||
87 | + // Message 잘 찍히는지 확인 | ||
88 | + | ||
89 | + result.text = objBody.message.result.translatedText; | ||
90 | + //console.log(result.text); | ||
91 | + //번역된 문장 보내기 | ||
92 | + client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
93 | + } | ||
94 | + }); | ||
95 | +} | ||
96 | + send={ | ||
97 | + 'message':{ | ||
98 | + 'text': result.text | ||
99 | + } | ||
100 | + } | ||
101 | + break; | ||
102 | + case '문법': | ||
103 | + case '문법체크': | ||
104 | + case '2': | ||
105 | + | ||
106 | + break; | ||
107 | + | ||
108 | + } | ||
109 | + | ||
110 | + | ||
111 | + | ||
112 | + | ||
113 | +}); | ||
114 | + | ||
115 | +} | ||
116 | +}); | ||
117 | + |
-
Please register or login to post a comment