Toggle navigation
Toggle navigation
This project
Loading...
Sign in
kykint
/
TELEGRAMBOT
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
kykint
2019-05-29 20:51:42 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
679d7f52dfe3ac7af61c182d0d7c6350e30358df
679d7f52
1 parent
ab69e4a5
Prepare for rewrite
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
199 deletions
app.js
bin/www
package-lock.json
package.json
app.js
View file @
679d7f5
var
express
=
require
(
'express'
);
var
app
=
express
();
const
line
=
require
(
'@line/bot-sdk'
);
const
TelegramBot
=
require
(
'node-telegram-bot-api'
);
// replace the value below with the Telegram token you receive from @BotFather
const
token
=
'825631426:AAE9tgw89kOZyLTre8DSDaObFQeVx7q41gw'
;
//
papago api
var
request
=
require
(
'request'
);
//
Create a bot that uses 'polling' to fetch new updates
const
bot
=
new
TelegramBot
(
token
,
{
polling
:
true
}
);
//번역 api_url
var
translate_api_url
=
'https://openapi.naver.com/v1/papago/n2mt'
;
// Matches "/echo [whatever]"
bot
.
onText
(
/
\/
echo
(
.+
)
/
,
(
msg
,
match
)
=>
{
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
//언어감지 api_url
var
languagedetect_api_url
=
'https://openapi.naver.com/v1/papago/detectLangs'
const
chatId
=
msg
.
chat
.
id
;
const
resp
=
match
[
1
];
// the captured "whatever"
// Naver Auth Key
//새로 발급받은 naver papago api id, pw 입력
var
client_id
=
'xZMx34y7uru1v8lywZ2d'
;
var
client_secret
=
'p6L7M7WsH9'
;
const
config
=
{
channelAccessToken
:
'mnny0MJSezgBXzR9C3Ddcc1Csdb7Y9jkvy2nqV5saOmvR2YOJ1/kj/2M0CNsLA+57B2qDpdUQ7WbCTtIKx/LAJ6Kwfop4tX3up7EM8H9EZK1td6GMbhhCb6wvUFVdb1PcTO4joCv8mspd3ubo8a+gAdB04t89/1O/w1cDnyilFU='
,
channelSecret
:
'bde77633a16fc5bfbd532d5990c6170e'
,
};
// create LINE SDK client
const
client
=
new
line
.
Client
(
config
);
// create Express app
// about Express itself: https://expressjs.com/
// register a webhook handler with middleware
// about the middleware, please refer to doc
app
.
post
(
'/webhook'
,
line
.
middleware
(
config
),
(
req
,
res
)
=>
{
Promise
.
all
(
req
.
body
.
events
.
map
(
handleEvent
))
.
then
((
result
)
=>
res
.
json
(
result
))
.
catch
((
err
)
=>
{
console
.
error
(
err
);
res
.
status
(
200
).
end
();
});
// send back the matched "whatever" to the chat
bot
.
sendMessage
(
chatId
,
resp
);
});
// event handler
function
handleEvent
(
event
)
{
if
(
event
.
type
!==
'message'
||
event
.
message
.
type
!==
'text'
)
{
// ignore non-text-message event
return
Promise
.
resolve
(
null
);
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
//언어 감지 option
var
detect_options
=
{
url
:
languagedetect_api_url
,
form
:
{
'query'
:
event
.
message
.
text
},
headers
:
{
'X-Naver-Client-Id'
:
client_id
,
'X-Naver-Client-Secret'
:
client_secret
}
};
//papago 언어 감지
request
.
post
(
detect_options
,
function
(
error
,
response
,
body
){
console
.
log
(
response
.
statusCode
);
if
(
!
error
&&
response
.
statusCode
==
200
){
var
detect_body
=
JSON
.
parse
(
response
.
body
);
var
source
=
''
;
var
target
=
''
;
var
result
=
{
type
:
'text'
,
text
:
''
};
//언어 감지가 제대로 됐는지 확인
console
.
log
(
detect_body
.
langCode
);
// Listen for any kind of message. There are different kinds of
// messages.
bot
.
on
(
'message'
,
(
msg
)
=>
{
const
chatId
=
msg
.
chat
.
id
;
//번역은 한국어->영어 / 영어->한국어만 지원
if
(
detect_body
.
langCode
==
'ko'
||
detect_body
.
langCode
==
'en'
){
source
=
detect_body
.
langCode
==
'ko'
?
'ko'
:
'en'
;
target
=
source
==
'ko'
?
'en'
:
'ko'
;
//papago 번역 option
var
options
=
{
url
:
translate_api_url
,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form
:
{
'source'
:
source
,
'target'
:
target
,
'text'
:
event
.
message
.
text
},
headers
:
{
'X-Naver-Client-Id'
:
client_id
,
'X-Naver-Client-Secret'
:
client_secret
}
};
// Naver Post API
request
.
post
(
options
,
function
(
error
,
response
,
body
){
// Translate API Sucess
if
(
!
error
&&
response
.
statusCode
==
200
){
// JSON
var
objBody
=
JSON
.
parse
(
response
.
body
);
// Message 잘 찍히는지 확인
result
.
text
=
objBody
.
message
.
result
.
translatedText
;
console
.
log
(
result
.
text
);
//번역된 문장 보내기
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
);
}
});
}
// 메시지의 언어가 영어 또는 한국어가 아닐 경우
else
{
result
.
text
=
'언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'
;
client
.
replyMessage
(
event
.
replyToken
,
result
).
then
(
resolve
).
catch
(
reject
);
}
}
});
});
}
module
.
exports
=
app
;
\ No newline at end of file
// send a message to the chat acknowledging receipt of their message
bot
.
sendMessage
(
chatId
,
'Received your message'
);
});
...
...
bin/www
deleted
100644 → 0
View file @
ab69e4a
#!/usr/bin/env node
/**
* Module dependencies.
*/
var
app
=
require
(
'../app'
);
var
debug
=
require
(
'debug'
)(
'project:server'
);
var
http
=
require
(
'http'
);
/**
* Get port from environment and store in Express.
*/
var
port
=
normalizePort
(
process
.
env
.
PORT
||
'3000'
);
app
.
set
(
'port'
,
port
);
/**
* Create HTTP server.
*/
var
server
=
http
.
createServer
(
app
);
/**
* Listen on provided port, on all network interfaces.
*/
server
.
listen
(
port
,
function
()
{
console
.
log
(
'Linebot listening on port '
+
port
+
'!'
);
});
server
.
on
(
'error'
,
onError
);
server
.
on
(
'listening'
,
onListening
);
/**
* Normalize a port into a number, string, or false.
*/
function
normalizePort
(
val
)
{
var
port
=
parseInt
(
val
,
10
);
if
(
isNaN
(
port
))
{
// named pipe
return
val
;
}
if
(
port
>=
0
)
{
// port number
return
port
;
}
return
false
;
}
/**
* Event listener for HTTP server "error" event.
*/
function
onError
(
error
)
{
if
(
error
.
syscall
!==
'listen'
)
{
throw
error
;
}
var
bind
=
typeof
port
===
'string'
?
'Pipe '
+
port
:
'Port '
+
port
;
// handle specific listen errors with friendly messages
switch
(
error
.
code
)
{
case
'EACCES'
:
console
.
error
(
bind
+
' requires elevated privileges'
);
process
.
exit
(
1
);
break
;
case
'EADDRINUSE'
:
console
.
error
(
bind
+
' is already in use'
);
process
.
exit
(
1
);
break
;
default
:
throw
error
;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function
onListening
()
{
var
addr
=
server
.
address
();
var
bind
=
typeof
addr
===
'string'
?
'pipe '
+
addr
:
'port '
+
addr
.
port
;
debug
(
'Listening on '
+
bind
);
}
package-lock.json
View file @
679d7f5
This diff is collapsed. Click to expand it.
package.json
View file @
679d7f5
{
"name"
:
"
line
bot"
,
"name"
:
"
telegram
bot"
,
"version"
:
"1.0.0"
,
"description"
:
""
,
"main"
:
"app.js"
,
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
,
"start"
:
"node ./
bin/www
"
"start"
:
"node ./
app.js
"
},
"author"
:
"강수인"
,
"license"
:
"MIT"
,
"dependencies"
:
{
"@line/bot-sdk"
:
"^6.7.1"
,
"express"
:
"^4.17.1"
,
"node-telegram-bot-api"
:
"^0.30.0"
,
"request"
:
"^2.88.0"
}
}
...
...
Please
register
or
login
to post a comment