신일섭

번역기 모듈 분리

1 +const request = require('request');
2 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
3 +const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
4 +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
5 +const PAPAGO_ID = 'r_kuebFKCqBeL3SI_dFb'
6 +const PAPAGO_SECRET = 'Wdz2tudrzB'
7 +const fs = require('fs');
8 +const path = require('path');
9 +const HTTPS = require('https');
10 +const domain = "www.chatbotshin.tk"
11 +const sslport = 23023;
12 +const bodyParser = require('body-parser');
13 +
14 +
15 +// language identification & translation
16 +function trans_lng_selector(){
17 + if(eventObj.message.text=='/번역 영어' || eventObj.message.text=='/번역 프랑스어' || eventObj.message.text=='/번역 일본어'){
18 + //trans_trigger = 0 or 1;
19 + trans_change(eventObj.message.text);
20 + trans_trigger = 1;
21 + console.log('[trans_dest]', trans_dest);
22 + }
23 +}
24 +
25 +
26 +
27 +function translating(replyToken, message) {
28 +
29 + request.post(
30 + {
31 + url: PAPAGO_URL,
32 + headers: {
33 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
34 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
35 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
36 + },
37 + body: 'source=ko&' + trans_dest + '&text=' + message,
38 + json:true
39 + },(error, response, body) => {
40 + if(!error && response.statusCode == 200) {
41 + console.log(body.message);
42 + var transMessage = body.message.result.translatedText;
43 + request.post(
44 + {
45 + url: TARGET_URL,
46 + headers: {
47 + 'Authorization': `Bearer ${TOKEN}`
48 + },
49 + json: {
50 + "replyToken":replyToken,
51 + "messages":[
52 + {
53 + "type":"text",
54 + "text":transMessage
55 + }
56 + ]
57 + }
58 + },(error, response, body) => {
59 + console.log(body)
60 + });
61 + }
62 + });
63 +
64 +}
65 +
66 +// language index identification
67 +function trans_change(index){
68 + if(index == '/번역 영어'){
69 + trans_dest = 'target=en';
70 + }
71 + else if(index == '/번역 일본어'){
72 + trans_dest = 'target=ja';
73 + }
74 + else if(index == '/번역 프랑스어'){
75 + trans_dest = 'target=fr';
76 + }
77 +}
78 +
79 +module.exports.translating = translating;
80 +module.exports.trans_lng_selector = trans_lng_selector;
81 +module.exports.trans_change = trans_change;
...\ No newline at end of file ...\ No newline at end of file