Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김대연
/
khuwitch
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
김대연
2020-12-07 05:05:07 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
65e9ef0631beb2adb9f1f82e34f9362ecc6df208
65e9ef06
1 parent
6d0743d2
Add Twitch OAuth2 Authenticate
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
85 additions
and
55 deletions
server/.env example → server/env example
server/openAPIs/papago_api.js
server/package-lock.json
server/package.json
server/socket_server.js
server/
.
env example
→
server/env example
View file @
65e9ef0
SOCKET_PORT=
TOKEN=
T
WITCH_T
OKEN=
PAPAGO_ID=
PAPAGO_SECRET=
BOT_USERNAME=
...
...
server/openAPIs/papago_api.js
View file @
65e9ef0
...
...
@@ -48,45 +48,23 @@ exports.detectchat = (message, client, io, target) => {
});
}
// exports.trans = (message, lang, io, room) => {
// 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=${lang}&target=ko&text=` + message,
// json:true
// },(error, response, body) => {
// if(!error && response.statusCode == 200) {
// var Translated = body.message.result.translatedText;
// io.to(room).emit('chat message', "trans", Translated);
// }
// });
// }
// exports.detect = (message,io,room) => {
// request.post(
// {
// url: dPAPAGO_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: `query=` + message,
// json:true
// },(error, response, body) => {
// if(!error && response.statusCode == 200) {
// var lang = body.langCode;
// if(lang != 'ko'){
// this.trans(message,lang,io,room)
// }
// }
// });
// }
exports
.
trans
=
(
message
,
client
,
io
,
target
)
=>
{
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&target=en&text=`
+
message
,
json
:
true
},
async
(
error
,
response
,
body
)
=>
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
var
Translated
=
await
body
.
message
.
result
.
translatedText
;
client
.
say
(
target
,
"(Trans) "
+
Translated
);
io
.
to
(
target
.
replace
(
'#'
,
''
)).
emit
(
'chat message'
,
"Trans"
,
Translated
);
}
});
}
...
...
server/package-lock.json
View file @
65e9ef0
...
...
@@ -160,6 +160,14 @@
"resolved"
:
"https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz"
,
"integrity"
:
"sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA=="
},
"axios"
:
{
"version"
:
"0.21.0"
,
"resolved"
:
"https://registry.npmjs.org/axios/-/axios-0.21.0.tgz"
,
"integrity"
:
"sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw=="
,
"requires"
:
{
"follow-redirects"
:
"^1.10.0"
}
},
"balanced-match"
:
{
"version"
:
"1.0.0"
,
"resolved"
:
"https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz"
,
...
...
@@ -488,6 +496,11 @@
"unpipe"
:
"~1.0.0"
}
},
"follow-redirects"
:
{
"version"
:
"1.13.0"
,
"resolved"
:
"https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz"
,
"integrity"
:
"sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA=="
},
"forever-agent"
:
{
"version"
:
"0.6.1"
,
"resolved"
:
"https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz"
,
...
...
server/package.json
View file @
65e9ef0
...
...
@@ -9,6 +9,7 @@
"author"
:
"Daeyeonkim97"
,
"license"
:
"ISC"
,
"dependencies"
:
{
"axios"
:
"^0.21.0"
,
"dotenv"
:
"^8.2.0"
,
"ejs"
:
"^3.1.5"
,
"express"
:
"^4.17.1"
,
...
...
server/socket_server.js
View file @
65e9ef0
...
...
@@ -13,12 +13,13 @@ var opts = {
username
:
process
.
env
.
BOT_USERNAME
,
password
:
process
.
env
.
OAUTH_TOKEN
},
channels
:
[
"
nnonuu
"
]
channels
:
[
"
tmwardo
"
]
};
// Create a client with our options
var
client
=
new
tmi
.
client
(
opts
);
//twitch chatbot client
var
bodyParser
=
require
(
'body-parser'
);
const
{
default
:
Axios
}
=
require
(
'axios'
);
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
false
}));
app
.
use
(
bodyParser
.
json
());
app
.
set
(
'view engine'
,
'ejs'
);
...
...
@@ -33,22 +34,59 @@ app.get('/', (req, res) => {
});
app
.
get
(
'/list'
,(
req
,
res
)
=>
{
res
.
send
(
client
.
channels
);
var
result
=
[]
for
(
var
i
=
0
;
i
<
client
.
channels
.
length
;
i
++
){
result
.
push
(
client
.
channels
[
i
].
slice
(
1
))
}
res
.
send
(
result
)
});
app
.
post
(
'/jointest'
,
async
(
req
,
res
)
=>
{
// requests.post(`https://id.twitch.tv/oauth2/token?client_id=<클라이언트 ID>&client_secret=${process.env.TOKEN}&grant_type=client_credentials`).json()
JoinChannel
(
req
.
body
.
streamer
);
res
.
send
(
req
.
body
.
streamer
)
});
////////////////////////oauth////////////////////////
const
axios
=
require
(
'axios'
)
app
.
get
(
'/oauth'
,(
req
,
res
)
=>
{
let
codeAddr
=
`https://id.twitch.tv/oauth2/authorize?response_type=code&approval_prompt=auto&redirect_uri=
${
process
.
env
.
HOST_URI
+
':'
+
process
.
env
.
SOCKET_PORT
}
/join&client_id=
${
process
.
env
.
TWITCH_CLIENT
}
`
res
.
redirect
(
codeAddr
)
});
app
.
get
(
'/join'
,
async
(
req
,
res
)
=>
{
let
code
=
req
.
query
.
code
let
reqAddr
=
`https://id.twitch.tv/oauth2/token?client_id=
${
process
.
env
.
TWITCH_CLIENT
}
&client_secret=
${
process
.
env
.
TWITCH_SECRET
}
&code=
${
code
}
&grant_type=authorization_code&redirect_uri=
${
process
.
env
.
HOST_URI
+
':'
+
process
.
env
.
SOCKET_PORT
}
/test`
axios
.
post
(
reqAddr
).
then
(
resp1
=>
{
axios
.
get
(
'https://id.twitch.tv/oauth2/validate'
,
{
headers
:{
Authorization
:
"Bearer "
+
resp1
.
data
.
access_token
}
}
).
then
(
resp2
=>
{
JoinChannel
(
resp2
.
data
.
login
)
res
.
send
(
"Joinned "
+
resp2
.
data
.
login
)
})
})
})
app
.
get
(
'/test'
,(
req
,
res
)
=>
{
res
.
send
(
""
)
})
////////////////////////oauth////////////////////////
app
.
post
(
'/add'
,
async
(
req
,
res
)
=>
{
/// 봇을 새로운 채널에 추가
await
client
.
action
(
req
.
body
.
streamer
,
'Khuwitchbot이 입
장'
);
await
opts
.
channels
.
push
(
'#'
+
req
.
body
.
streamer
);
async
function
JoinChannel
(
streamer
){
await
client
.
action
(
streamer
,
'KhuwitchBot 두두등
장'
);
await
opts
.
channels
.
push
(
'#'
+
streamer
);
await
delete
client
;
client
=
await
new
tmi
.
client
(
opts
);
client
.
on
(
'message'
,
onMessageHandler
);
client
.
on
(
'connected'
,
onConnectedHandler
);
client
.
connect
();
res
.
send
(
req
.
body
.
streamer
)
///
})
}
io
.
on
(
'connection'
,
(
socket
)
=>
{
...
...
@@ -91,15 +129,15 @@ client.on('connected', onConnectedHandler);
// Connect to Twitch:
client
.
connect
();
console
.
log
(
client
);
// Called every time a message comes in
function
onMessageHandler
(
target
,
context
,
msg
,
self
)
{
if
(
self
)
{
return
;
}
// Ignore messages from the bot
console
.
log
(
"chatdetected"
)
if
(
msg
.
startsWith
(
'!'
)){
return
;
if
(
msg
.
startsWith
(
'!번역'
)){
io
.
to
(
target
.
replace
(
'#'
,
''
)).
emit
(
'chat message'
,
context
[
"display-name"
],
msg
.
slice
(
4
))
papago
.
trans
(
msg
.
slice
(
4
),
client
,
io
,
target
)
}
}
else
if
(
context
[
"display-name"
]
==
"빵_떡"
||
context
[
"display-name"
]
==
"Nightbot"
...
...
Please
register
or
login
to post a comment