김대연

Add Twitch OAuth2 Authenticate

1 SOCKET_PORT= 1 SOCKET_PORT=
2 -TOKEN= 2 +TWITCH_TOKEN=
3 PAPAGO_ID= 3 PAPAGO_ID=
4 PAPAGO_SECRET= 4 PAPAGO_SECRET=
5 BOT_USERNAME= 5 BOT_USERNAME=
......
...@@ -48,45 +48,23 @@ exports.detectchat = (message, client, io, target) => { ...@@ -48,45 +48,23 @@ exports.detectchat = (message, client, io, target) => {
48 }); 48 });
49 } 49 }
50 50
51 - 51 +exports.trans = (message, client,io, target) => {
52 -// exports.trans = (message, lang, io, room) => { 52 + request.post(
53 -// request.post( 53 + {
54 -// { 54 + url: PAPAGO_URL,
55 -// url: PAPAGO_URL, 55 + headers: {
56 -// headers: { 56 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
57 -// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 57 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
58 -// 'X-Naver-Client-Id': `${PAPAGO_ID}`, 58 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
59 -// 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 59 + },
60 -// }, 60 + body: `source=ko&target=en&text=` + message,
61 -// body: `source=${lang}&target=ko&text=` + message, 61 + json:true
62 -// json:true 62 + },async (error, response, body) => {
63 -// },(error, response, body) => { 63 + if(!error && response.statusCode == 200) {
64 -// if(!error && response.statusCode == 200) { 64 + var Translated = await body.message.result.translatedText;
65 -// var Translated = body.message.result.translatedText; 65 + client.say(target, "(Trans) "+Translated);
66 -// io.to(room).emit('chat message', "trans", Translated); 66 + io.to(target.replace('#','')).emit('chat message', "Trans", Translated);
67 -// } 67 + }
68 -// }); 68 + });
69 -// } 69 +}
70 -
71 -
72 -// exports.detect = (message,io,room) => {
73 -// request.post(
74 -// {
75 -// url: dPAPAGO_URL,
76 -// headers: {
77 -// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
78 -// 'X-Naver-Client-Id': `${PAPAGO_ID}`,
79 -// 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
80 -// },
81 -// body: `query=` + message,
82 -// json:true
83 -// },(error, response, body) => {
84 -// if(!error && response.statusCode == 200) {
85 -// var lang = body.langCode;
86 -// if(lang != 'ko'){
87 -// this.trans(message,lang,io,room)
88 -// }
89 -// }
90 -// });
91 -// }
92 70
......
...@@ -160,6 +160,14 @@ ...@@ -160,6 +160,14 @@
160 "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz", 160 "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz",
161 "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==" 161 "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA=="
162 }, 162 },
163 + "axios": {
164 + "version": "0.21.0",
165 + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.0.tgz",
166 + "integrity": "sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==",
167 + "requires": {
168 + "follow-redirects": "^1.10.0"
169 + }
170 + },
163 "balanced-match": { 171 "balanced-match": {
164 "version": "1.0.0", 172 "version": "1.0.0",
165 "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", 173 "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
...@@ -488,6 +496,11 @@ ...@@ -488,6 +496,11 @@
488 "unpipe": "~1.0.0" 496 "unpipe": "~1.0.0"
489 } 497 }
490 }, 498 },
499 + "follow-redirects": {
500 + "version": "1.13.0",
501 + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz",
502 + "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA=="
503 + },
491 "forever-agent": { 504 "forever-agent": {
492 "version": "0.6.1", 505 "version": "0.6.1",
493 "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", 506 "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
9 "author": "Daeyeonkim97", 9 "author": "Daeyeonkim97",
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 + "axios": "^0.21.0",
12 "dotenv": "^8.2.0", 13 "dotenv": "^8.2.0",
13 "ejs": "^3.1.5", 14 "ejs": "^3.1.5",
14 "express": "^4.17.1", 15 "express": "^4.17.1",
......
...@@ -13,12 +13,13 @@ var opts = { ...@@ -13,12 +13,13 @@ var opts = {
13 username: process.env.BOT_USERNAME, 13 username: process.env.BOT_USERNAME,
14 password: process.env.OAUTH_TOKEN 14 password: process.env.OAUTH_TOKEN
15 }, 15 },
16 - channels: ["nnonuu"] 16 + channels: ["tmwardo"]
17 }; 17 };
18 // Create a client with our options 18 // Create a client with our options
19 var client = new tmi.client(opts); //twitch chatbot client 19 var client = new tmi.client(opts); //twitch chatbot client
20 20
21 var bodyParser = require('body-parser'); 21 var bodyParser = require('body-parser');
22 +const { default: Axios } = require('axios');
22 app.use(bodyParser.urlencoded({ extended: false })); 23 app.use(bodyParser.urlencoded({ extended: false }));
23 app.use(bodyParser.json()); 24 app.use(bodyParser.json());
24 app.set('view engine', 'ejs'); 25 app.set('view engine', 'ejs');
...@@ -33,22 +34,59 @@ app.get('/', (req, res) => { ...@@ -33,22 +34,59 @@ app.get('/', (req, res) => {
33 }); 34 });
34 35
35 app.get('/list',(req,res) => { 36 app.get('/list',(req,res) => {
36 - res.send(client.channels); 37 + var result = []
38 + for (var i = 0; i<client.channels.length; i++){
39 + result.push(client.channels[i].slice(1))
40 + }
41 + res.send(result)
42 +});
43 +
44 +app.post('/jointest',async (req,res)=>{
45 + // requests.post(`https://id.twitch.tv/oauth2/token?client_id=<클라이언트 ID>&client_secret=${process.env.TOKEN}&grant_type=client_credentials`).json()
46 + JoinChannel(req.body.streamer);
47 + res.send(req.body.streamer)
48 +});
49 +
50 +////////////////////////oauth////////////////////////
51 +const axios = require('axios')
52 +app.get('/oauth',(req,res)=>{
53 + 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}`
54 +
55 + res.redirect(codeAddr)
56 +});
57 +app.get('/join', async (req,res)=>{
58 + let code = req.query.code
59 + 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`
60 + axios.post(reqAddr).then(resp1=>{
61 + axios.get('https://id.twitch.tv/oauth2/validate',
62 + {
63 + headers:{
64 + Authorization : "Bearer "+ resp1.data.access_token
65 + }
66 + }
67 + ).then(resp2=>{
68 + JoinChannel(resp2.data.login)
69 + res.send("Joinned "+resp2.data.login)
70 + })
71 + })
72 +
37 }) 73 })
38 74
39 -app.post('/add',async (req,res)=>{ 75 +app.get('/test',(req,res)=>{
40 - 76 + res.send("")
41 - /// 봇을 새로운 채널에 추가 77 +})
42 - await client.action(req.body.streamer,'Khuwitchbot이 입장'); 78 +////////////////////////oauth////////////////////////
43 - await opts.channels.push('#'+req.body.streamer); 79 +
80 +
81 +async function JoinChannel(streamer){
82 + await client.action(streamer,'KhuwitchBot 두두등장');
83 + await opts.channels.push('#'+streamer);
44 await delete client; 84 await delete client;
45 client = await new tmi.client(opts); 85 client = await new tmi.client(opts);
46 client.on('message', onMessageHandler); 86 client.on('message', onMessageHandler);
47 client.on('connected', onConnectedHandler); 87 client.on('connected', onConnectedHandler);
48 client.connect(); 88 client.connect();
49 - res.send(req.body.streamer) 89 +}
50 - ///
51 -})
52 90
53 91
54 io.on('connection', (socket) => { 92 io.on('connection', (socket) => {
...@@ -91,15 +129,15 @@ client.on('connected', onConnectedHandler); ...@@ -91,15 +129,15 @@ client.on('connected', onConnectedHandler);
91 129
92 // Connect to Twitch: 130 // Connect to Twitch:
93 client.connect(); 131 client.connect();
94 -console.log(client);
95 132
96 // Called every time a message comes in 133 // Called every time a message comes in
97 function onMessageHandler (target, context, msg, self) { 134 function onMessageHandler (target, context, msg, self) {
98 if (self) { return; } // Ignore messages from the bot 135 if (self) { return; } // Ignore messages from the bot
99 - console.log("chatdetected")
100 -
101 if (msg.startsWith('!')){ 136 if (msg.startsWith('!')){
102 - return; 137 + if(msg.startsWith('!번역')){
138 + io.to(target.replace('#','')).emit('chat message',context["display-name"],msg.slice(4))
139 + papago.trans(msg.slice(4), client, io, target)
140 + }
103 } 141 }
104 else if(context["display-name"] == "빵_떡" 142 else if(context["display-name"] == "빵_떡"
105 || context["display-name"]=="Nightbot" 143 || context["display-name"]=="Nightbot"
......