Showing
1 changed file
with
83 additions
and
6 deletions
... | @@ -4,9 +4,21 @@ const TelegramBot = require('node-telegram-bot-api'); | ... | @@ -4,9 +4,21 @@ const TelegramBot = require('node-telegram-bot-api'); |
4 | const token = '825631426:AAE9tgw89kOZyLTre8DSDaObFQeVx7q41gw'; | 4 | const token = '825631426:AAE9tgw89kOZyLTre8DSDaObFQeVx7q41gw'; |
5 | 5 | ||
6 | // Create a bot that uses 'polling' to fetch new updates | 6 | // Create a bot that uses 'polling' to fetch new updates |
7 | -const bot = new TelegramBot(token, {polling: true}); | 7 | +const bot = new TelegramBot(token, { polling: true }); |
8 | 8 | ||
9 | -// Matches "/echo [whatever]" | 9 | +var request = require('request'); |
10 | + | ||
11 | +// Translation api url | ||
12 | +const translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; | ||
13 | + | ||
14 | +// Language detection api url | ||
15 | +const languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs' | ||
16 | + | ||
17 | +// Naver papago client id & secret | ||
18 | +const papago_client_id = 'lA0rGxQllAfrlOkGGNnK'; | ||
19 | +const papago_client_secret = 'u3fykDlNb0'; | ||
20 | + | ||
21 | +// /echo [whatever] | ||
10 | bot.onText(/\/echo (.+)/, (msg, match) => { | 22 | bot.onText(/\/echo (.+)/, (msg, match) => { |
11 | // 'msg' is the received Message from Telegram | 23 | // 'msg' is the received Message from Telegram |
12 | // 'match' is the result of executing the regexp above on the text content | 24 | // 'match' is the result of executing the regexp above on the text content |
... | @@ -19,11 +31,76 @@ bot.onText(/\/echo (.+)/, (msg, match) => { | ... | @@ -19,11 +31,76 @@ bot.onText(/\/echo (.+)/, (msg, match) => { |
19 | bot.sendMessage(chatId, resp); | 31 | bot.sendMessage(chatId, resp); |
20 | }); | 32 | }); |
21 | 33 | ||
22 | -// Listen for any kind of message. There are different kinds of | 34 | +// Listen for any kind of message. Translate if it's not a command. |
23 | -// messages. | ||
24 | bot.on('message', (msg) => { | 35 | bot.on('message', (msg) => { |
25 | const chatId = msg.chat.id; | 36 | const chatId = msg.chat.id; |
37 | + const received_msg = msg.text; | ||
38 | + | ||
39 | + // Ignore if input is a command | ||
40 | + if (received_msg[0] == '/') { | ||
41 | + return; | ||
42 | + } | ||
43 | + | ||
44 | + // Language detection options | ||
45 | + var lang_detect_options = { | ||
46 | + url: languagedetect_api_url, | ||
47 | + form: { 'query': received_msg }, | ||
48 | + headers: { | ||
49 | + 'X-Naver-Client-Id': papago_client_id, | ||
50 | + 'X-Naver-Client-Secret': papago_client_secret | ||
51 | + } | ||
52 | + }; | ||
53 | + | ||
54 | + // Papago language detection | ||
55 | + request.post(lang_detect_options, function (error, response, body) { | ||
56 | + console.log(response.statusCode); | ||
57 | + if (!error && response.statusCode == 200) { | ||
58 | + var detect_body = JSON.parse(response.body); | ||
59 | + var source = ''; | ||
60 | + var target = ''; | ||
61 | + var result = { type: 'text', text: '' }; | ||
62 | + | ||
63 | + // Check if detection was successful | ||
64 | + console.log(detect_body.langCode); | ||
65 | + | ||
66 | + // Translate using papago, only Korean and English are supported atm | ||
67 | + if (detect_body.langCode == 'ko' || detect_body.langCode == 'en') { | ||
68 | + source = detect_body.langCode == 'ko' ? 'ko' : 'en'; | ||
69 | + target = source == 'ko' ? 'en' : 'ko'; | ||
70 | + | ||
71 | + // Papago translation options | ||
72 | + var translate_options = { | ||
73 | + url: translate_api_url, | ||
74 | + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | ||
75 | + form: { | ||
76 | + 'source': source, // Translate from source | ||
77 | + 'target': target, // to target | ||
78 | + 'text': received_msg // Message to translate | ||
79 | + }, | ||
80 | + headers: { | ||
81 | + 'X-Naver-Client-Id': papago_client_id, | ||
82 | + 'X-Naver-Client-Secret': papago_client_secret | ||
83 | + } | ||
84 | + }; | ||
85 | + | ||
86 | + // Send translatation request | ||
87 | + request.post(translate_options, function (error, response, body) { | ||
88 | + // On success | ||
89 | + if (!error && response.statusCode == 200) { | ||
90 | + var objBody = JSON.parse(response.body); | ||
91 | + result.text = objBody.message.result.translatedText; | ||
26 | 92 | ||
27 | - // send a message to the chat acknowledging receipt of their message | 93 | + // Send translated message |
28 | - bot.sendMessage(chatId, 'Received your message'); | 94 | + console.log(result.text); |
95 | + bot.sendMessage(chatId, result.text); | ||
96 | + } | ||
97 | + }); | ||
98 | + } | ||
99 | + // Language not detected | ||
100 | + else { | ||
101 | + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | ||
102 | + bot.sendMessage(chatId, result.text); | ||
103 | + } | ||
104 | + } | ||
105 | + }); | ||
29 | }); | 106 | }); | ... | ... |
-
Please register or login to post a comment