Showing
4 changed files
with
24 additions
and
199 deletions
| 1 | -var express = require('express'); | 1 | +const TelegramBot = require('node-telegram-bot-api'); |
| 2 | -var app = express(); | ||
| 3 | -const line = require('@line/bot-sdk'); | ||
| 4 | 2 | ||
| 3 | +// replace the value below with the Telegram token you receive from @BotFather | ||
| 4 | +const token = '825631426:AAE9tgw89kOZyLTre8DSDaObFQeVx7q41gw'; | ||
| 5 | 5 | ||
| 6 | -//papago api | 6 | +// Create a bot that uses 'polling' to fetch new updates |
| 7 | -var request = require('request'); | 7 | +const bot = new TelegramBot(token, {polling: true}); |
| 8 | 8 | ||
| 9 | -//번역 api_url | 9 | +// Matches "/echo [whatever]" |
| 10 | -var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; | 10 | +bot.onText(/\/echo (.+)/, (msg, match) => { |
| 11 | + // 'msg' is the received Message from Telegram | ||
| 12 | + // 'match' is the result of executing the regexp above on the text content | ||
| 13 | + // of the message | ||
| 11 | 14 | ||
| 12 | -//언어감지 api_url | 15 | + const chatId = msg.chat.id; |
| 13 | -var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs' | 16 | + const resp = match[1]; // the captured "whatever" |
| 14 | 17 | ||
| 15 | -// Naver Auth Key | 18 | + // send back the matched "whatever" to the chat |
| 16 | -//새로 발급받은 naver papago api id, pw 입력 | 19 | + bot.sendMessage(chatId, resp); |
| 17 | -var client_id = 'xZMx34y7uru1v8lywZ2d'; | ||
| 18 | -var client_secret = 'p6L7M7WsH9'; | ||
| 19 | - | ||
| 20 | -const config = { | ||
| 21 | - channelAccessToken: 'mnny0MJSezgBXzR9C3Ddcc1Csdb7Y9jkvy2nqV5saOmvR2YOJ1/kj/2M0CNsLA+57B2qDpdUQ7WbCTtIKx/LAJ6Kwfop4tX3up7EM8H9EZK1td6GMbhhCb6wvUFVdb1PcTO4joCv8mspd3ubo8a+gAdB04t89/1O/w1cDnyilFU=', | ||
| 22 | - channelSecret: 'bde77633a16fc5bfbd532d5990c6170e', | ||
| 23 | -}; | ||
| 24 | - | ||
| 25 | - | ||
| 26 | -// create LINE SDK client | ||
| 27 | -const client = new line.Client(config); | ||
| 28 | - | ||
| 29 | -// create Express app | ||
| 30 | -// about Express itself: https://expressjs.com/ | ||
| 31 | - | ||
| 32 | -// register a webhook handler with middleware | ||
| 33 | -// about the middleware, please refer to doc | ||
| 34 | -app.post('/webhook', line.middleware(config), (req, res) => { | ||
| 35 | - Promise | ||
| 36 | - .all(req.body.events.map(handleEvent)) | ||
| 37 | - .then((result) => res.json(result)) | ||
| 38 | - .catch((err) => { | ||
| 39 | - console.error(err); | ||
| 40 | - res.status(200).end(); | ||
| 41 | - }); | ||
| 42 | }); | 20 | }); |
| 43 | 21 | ||
| 44 | -// event handler | 22 | +// Listen for any kind of message. There are different kinds of |
| 45 | -function handleEvent(event) { | 23 | +// messages. |
| 46 | - if (event.type !== 'message' || event.message.type !== 'text') { | 24 | +bot.on('message', (msg) => { |
| 47 | - // ignore non-text-message event | 25 | + const chatId = msg.chat.id; |
| 48 | - return Promise.resolve(null); | ||
| 49 | - } | ||
| 50 | - return new Promise(function(resolve, reject) { | ||
| 51 | - //언어 감지 option | ||
| 52 | - var detect_options = { | ||
| 53 | - url : languagedetect_api_url, | ||
| 54 | - form : {'query': event.message.text}, | ||
| 55 | - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | ||
| 56 | - }; | ||
| 57 | - | ||
| 58 | - //papago 언어 감지 | ||
| 59 | - request.post(detect_options,function(error,response,body){ | ||
| 60 | - console.log(response.statusCode); | ||
| 61 | - if(!error && response.statusCode == 200){ | ||
| 62 | - var detect_body = JSON.parse(response.body); | ||
| 63 | - var source = ''; | ||
| 64 | - var target = ''; | ||
| 65 | - var result = { type: 'text', text:''}; | ||
| 66 | - | ||
| 67 | - //언어 감지가 제대로 됐는지 확인 | ||
| 68 | - console.log(detect_body.langCode); | ||
| 69 | - | ||
| 70 | 26 | ||
| 71 | - //번역은 한국어->영어 / 영어->한국어만 지원 | ||
| 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: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | ||
| 81 | - }; | ||
| 82 | - | ||
| 83 | - // Naver Post API | ||
| 84 | - request.post(options, function(error, response, body){ | ||
| 85 | - // Translate API Sucess | ||
| 86 | - if(!error && response.statusCode == 200){ | ||
| 87 | - // JSON | ||
| 88 | - var objBody = JSON.parse(response.body); | ||
| 89 | - // Message 잘 찍히는지 확인 | ||
| 90 | - | ||
| 91 | - result.text = objBody.message.result.translatedText; | ||
| 92 | - console.log(result.text); | ||
| 93 | - //번역된 문장 보내기 | ||
| 94 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
| 95 | - } | ||
| 96 | - }); | ||
| 97 | - } | ||
| 98 | - // 메시지의 언어가 영어 또는 한국어가 아닐 경우 | ||
| 99 | - else{ | ||
| 100 | - result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | ||
| 101 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - }); | ||
| 107 | - | ||
| 108 | - }); | ||
| 109 | - } | ||
| 110 | - | ||
| 111 | -module.exports = app; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 27 | + // send a message to the chat acknowledging receipt of their message | ||
| 28 | + bot.sendMessage(chatId, 'Received your message'); | ||
| 29 | +}); | ... | ... |
bin/www
deleted
100644 → 0
| 1 | -#!/usr/bin/env node | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * Module dependencies. | ||
| 5 | - */ | ||
| 6 | - | ||
| 7 | -var app = require('../app'); | ||
| 8 | -var debug = require('debug')('project:server'); | ||
| 9 | -var http = require('http'); | ||
| 10 | - | ||
| 11 | -/** | ||
| 12 | - * Get port from environment and store in Express. | ||
| 13 | - */ | ||
| 14 | - | ||
| 15 | -var port = normalizePort(process.env.PORT || '3000'); | ||
| 16 | -app.set('port', port); | ||
| 17 | - | ||
| 18 | -/** | ||
| 19 | - * Create HTTP server. | ||
| 20 | - */ | ||
| 21 | - | ||
| 22 | -var server = http.createServer(app); | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Listen on provided port, on all network interfaces. | ||
| 26 | - */ | ||
| 27 | - | ||
| 28 | -server.listen(port, function () { | ||
| 29 | - console.log('Linebot listening on port ' + port + '!'); | ||
| 30 | -}); | ||
| 31 | -server.on('error', onError); | ||
| 32 | -server.on('listening', onListening); | ||
| 33 | - | ||
| 34 | -/** | ||
| 35 | - * Normalize a port into a number, string, or false. | ||
| 36 | - */ | ||
| 37 | - | ||
| 38 | -function normalizePort(val) { | ||
| 39 | - var port = parseInt(val, 10); | ||
| 40 | - | ||
| 41 | - if (isNaN(port)) { | ||
| 42 | - // named pipe | ||
| 43 | - return val; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - if (port >= 0) { | ||
| 47 | - // port number | ||
| 48 | - return port; | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | - return false; | ||
| 52 | -} | ||
| 53 | - | ||
| 54 | -/** | ||
| 55 | - * Event listener for HTTP server "error" event. | ||
| 56 | - */ | ||
| 57 | - | ||
| 58 | -function onError(error) { | ||
| 59 | - if (error.syscall !== 'listen') { | ||
| 60 | - throw error; | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - var bind = typeof port === 'string' | ||
| 64 | - ? 'Pipe ' + port | ||
| 65 | - : 'Port ' + port; | ||
| 66 | - | ||
| 67 | - // handle specific listen errors with friendly messages | ||
| 68 | - switch (error.code) { | ||
| 69 | - case 'EACCES': | ||
| 70 | - console.error(bind + ' requires elevated privileges'); | ||
| 71 | - process.exit(1); | ||
| 72 | - break; | ||
| 73 | - case 'EADDRINUSE': | ||
| 74 | - console.error(bind + ' is already in use'); | ||
| 75 | - process.exit(1); | ||
| 76 | - break; | ||
| 77 | - default: | ||
| 78 | - throw error; | ||
| 79 | - } | ||
| 80 | -} | ||
| 81 | - | ||
| 82 | -/** | ||
| 83 | - * Event listener for HTTP server "listening" event. | ||
| 84 | - */ | ||
| 85 | - | ||
| 86 | -function onListening() { | ||
| 87 | - var addr = server.address(); | ||
| 88 | - var bind = typeof addr === 'string' | ||
| 89 | - ? 'pipe ' + addr | ||
| 90 | - : 'port ' + addr.port; | ||
| 91 | - debug('Listening on ' + bind); | ||
| 92 | -} |
This diff is collapsed. Click to expand it.
| 1 | { | 1 | { |
| 2 | - "name": "linebot", | 2 | + "name": "telegrambot", |
| 3 | "version": "1.0.0", | 3 | "version": "1.0.0", |
| 4 | "description": "", | 4 | "description": "", |
| 5 | "main": "app.js", | 5 | "main": "app.js", |
| 6 | "scripts": { | 6 | "scripts": { |
| 7 | "test": "echo \"Error: no test specified\" && exit 1", | 7 | "test": "echo \"Error: no test specified\" && exit 1", |
| 8 | - "start": "node ./bin/www" | 8 | + "start": "node ./app.js" |
| 9 | }, | 9 | }, |
| 10 | "author": "강수인", | 10 | "author": "강수인", |
| 11 | "license": "MIT", | 11 | "license": "MIT", |
| 12 | "dependencies": { | 12 | "dependencies": { |
| 13 | - "@line/bot-sdk": "^6.7.1", | 13 | + "node-telegram-bot-api": "^0.30.0", |
| 14 | - "express": "^4.17.1", | ||
| 15 | "request": "^2.88.0" | 14 | "request": "^2.88.0" |
| 16 | } | 15 | } |
| 17 | } | 16 | } | ... | ... |
-
Please register or login to post a comment