trans.js 2.72 KB
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const PAPAGO_ID = 'r_kuebFKCqBeL3SI_dFb'
const PAPAGO_SECRET = 'Wdz2tudrzB'
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "www.chatbotshin.tk"
const sslport = 23023;
const bodyParser = require('body-parser');


// language identification & translation
function trans_lng_selector(){
    if(eventObj.message.text=='/번역 영어' || eventObj.message.text=='/번역 프랑스어' || eventObj.message.text=='/번역 일본어'){
    //trans_trigger = 0 or 1;
    trans_change(eventObj.message.text);
    trans_trigger = 1;
    console.log('[trans_dest]', trans_dest);
    }
}



function translating(replyToken, message) {

    request.post(
        {
            url: PAPAGO_URL,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'X-Naver-Client-Id': `${PAPAGO_ID}`,
                'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
            },
            body: 'source=ko&' + trans_dest + '&text=' + message,
            json:true
        },(error, response, body) => {
            if(!error && response.statusCode == 200) {
                console.log(body.message);
                var transMessage = body.message.result.translatedText;
                request.post(
                    {
                        url: TARGET_URL,
                        headers: {
                            'Authorization': `Bearer ${TOKEN}`
                        },
                        json: {
                            "replyToken":replyToken,
                            "messages":[
                                {
                                    "type":"text",
                                    "text":transMessage
                                }
                            ]
                        }
                    },(error, response, body) => {
                        console.log(body)
                    });
            }
        });

}

// language index identification
function trans_change(index){
    if(index == '/번역 영어'){
        trans_dest = 'target=en';
    }
    else if(index == '/번역 일본어'){
        trans_dest = 'target=ja';
    }
    else if(index == '/번역 프랑스어'){
        trans_dest = 'target=fr';
    }
}

module.exports.translating = translating;
module.exports.trans_lng_selector = trans_lng_selector;
module.exports.trans_change = trans_change;