김대연

Merge branch 'kdysy1130-socket' into 'master'

add Twitch IRC server and combime with Khuwitch SocketServer (example server)

add Twitch IRC server and combime with Khuwitch SocketServer (example server)

See merge request !9
...@@ -6,7 +6,8 @@ const dPAPAGO_URL = 'https://openapi.naver.com/v1/papago/detectLangs' ...@@ -6,7 +6,8 @@ const dPAPAGO_URL = 'https://openapi.naver.com/v1/papago/detectLangs'
6 const PAPAGO_ID = process.env.PAPAGO_ID 6 const PAPAGO_ID = process.env.PAPAGO_ID
7 const PAPAGO_SECRET = process.env.PAPAGO_SECRET 7 const PAPAGO_SECRET = process.env.PAPAGO_SECRET
8 8
9 -exports.trans = (message, lang, io, room) => { 9 +
10 +exports.transchat = (message, lang, client,io, target) => {
10 request.post( 11 request.post(
11 { 12 {
12 url: PAPAGO_URL, 13 url: PAPAGO_URL,
...@@ -17,16 +18,16 @@ exports.trans = (message, lang, io, room) => { ...@@ -17,16 +18,16 @@ exports.trans = (message, lang, io, room) => {
17 }, 18 },
18 body: `source=${lang}&target=ko&text=` + message, 19 body: `source=${lang}&target=ko&text=` + message,
19 json:true 20 json:true
20 - },(error, response, body) => { 21 + },async (error, response, body) => {
21 if(!error && response.statusCode == 200) { 22 if(!error && response.statusCode == 200) {
22 - var Translated = body.message.result.translatedText; 23 + var Translated = await body.message.result.translatedText;
23 - io.to(room).emit('chat message', "trans", Translated); 24 + client.say(target, "(번역) "+Translated);
25 + io.to(target.replace('#','')).emit('chat message', "번역", Translated);
24 } 26 }
25 }); 27 });
26 } 28 }
27 29
28 - 30 +exports.detectchat = (message, client, io, target) => {
29 -exports.detect = (message,io,room) => {
30 request.post( 31 request.post(
31 { 32 {
32 url: dPAPAGO_URL, 33 url: dPAPAGO_URL,
...@@ -41,48 +42,51 @@ exports.detect = (message,io,room) => { ...@@ -41,48 +42,51 @@ exports.detect = (message,io,room) => {
41 if(!error && response.statusCode == 200) { 42 if(!error && response.statusCode == 200) {
42 var lang = body.langCode; 43 var lang = body.langCode;
43 if(lang != 'ko'){ 44 if(lang != 'ko'){
44 - this.trans(message,lang,io,room) 45 + this.transchat(message,lang,client,io,target)
45 } 46 }
46 } 47 }
47 }); 48 });
48 } 49 }
49 50
50 -exports.transchat = (message, lang, client, target) => {
51 - request.post(
52 - {
53 - url: PAPAGO_URL,
54 - headers: {
55 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
56 - 'X-Naver-Client-Id': `${PAPAGO_ID}`,
57 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
58 - },
59 - body: `source=${lang}&target=ko&text=` + message,
60 - json:true
61 - },(error, response, body) => {
62 - if(!error && response.statusCode == 200) {
63 - var Translated = body.message.result.translatedText;
64 - client.say(target, "(번역) ", Translated);
65 - }
66 - });
67 -}
68 51
69 -exports.detectchat = (message, client,target) => {
70 - request.post(
71 - {
72 - url: dPAPAGO_URL,
73 - headers: {
74 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
75 - 'X-Naver-Client-Id': `${PAPAGO_ID}`,
76 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
77 - },
78 - body: `query=` + message,
79 - json:true
80 - },(error, response, body) => {
81 - if(!error && response.statusCode == 200) {
82 - var lang = body.langCode;
83 - if(lang != 'ko'){
84 - this.trans(message,lang,client,target)
85 - }
86 - }
87 - });
88 -}
...\ No newline at end of file ...\ No newline at end of file
52 +// exports.trans = (message, lang, io, room) => {
53 +// request.post(
54 +// {
55 +// url: PAPAGO_URL,
56 +// headers: {
57 +// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
58 +// 'X-Naver-Client-Id': `${PAPAGO_ID}`,
59 +// 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
60 +// },
61 +// body: `source=${lang}&target=ko&text=` + message,
62 +// json:true
63 +// },(error, response, body) => {
64 +// if(!error && response.statusCode == 200) {
65 +// var Translated = body.message.result.translatedText;
66 +// io.to(room).emit('chat message', "trans", Translated);
67 +// }
68 +// });
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 +
......
This diff could not be displayed because it is too large.
...@@ -14,6 +14,6 @@ ...@@ -14,6 +14,6 @@
14 "express": "^4.17.1", 14 "express": "^4.17.1",
15 "request": "^2.88.2", 15 "request": "^2.88.2",
16 "socket.io": "^3.0.3", 16 "socket.io": "^3.0.3",
17 - "tmi": "^2.0.0" 17 + "tmi.js": "^1.5.0"
18 } 18 }
19 } 19 }
......
...@@ -5,24 +5,24 @@ const http = require('http').Server(app); ...@@ -5,24 +5,24 @@ const http = require('http').Server(app);
5 const io = require('socket.io')(http); 5 const io = require('socket.io')(http);
6 const papago = require('./openAPIs/papago_api'); 6 const papago = require('./openAPIs/papago_api');
7 7
8 -// const tmi = require('tmi.js'); 8 +const tmi = require('tmi.js');
9 -// // Define configuration options 9 +// Define configuration options
10 -// var opts = { 10 +var opts = {
11 -// identity: { 11 + identity: {
12 -// username: process.env.BOT_USERNAME, 12 + username: process.env.BOT_USERNAME,
13 -// password: process.env.OAUTH_TOKEN 13 + password: process.env.OAUTH_TOKEN
14 -// }, 14 + },
15 -// channels: [ 15 + channels: [
16 -// 'bachelorchuckchuck' 16 + 'nnonuu'
17 -// ] 17 + ]
18 -// }; 18 + };
19 -// // Create a client with our options 19 +// Create a client with our options
20 -// const client = new tmi.client(opts); //twitch chatbot client 20 +const client = new tmi.client(opts); //twitch chatbot client
21 21
22 app.set('view engine', 'ejs'); 22 app.set('view engine', 'ejs');
23 app.set('views', './testviews'); 23 app.set('views', './testviews');
24 24
25 -let room = ['streamer1', 'streamer2']; 25 +let room = ['nnonuu', 'bachelorchuckchuck'];
26 // client.opts.channels; 26 // client.opts.channels;
27 let a = 0; 27 let a = 0;
28 28
...@@ -46,55 +46,57 @@ io.on('connection', (socket) => { ...@@ -46,55 +46,57 @@ io.on('connection', (socket) => {
46 console.log('user disconnected'); 46 console.log('user disconnected');
47 }); 47 });
48 48
49 - socket.on('leaveRoom', (num,name) => { 49 + socket.on('leaveRoom', (streamer,name) => {
50 - socket.leave(room[num], () => { 50 + socket.leave(streamer, () => {
51 - console.log('Someone leave a ' + room[num]); 51 + console.log('Someone leave a ' + streamer);
52 - io.to(room[num]).emit('leaveRoom', num ,name); 52 + io.to(streamer).emit('leaveRoom', streamer ,name);
53 }); 53 });
54 }); 54 });
55 55
56 - socket.on('joinRoom', (num,name) => { 56 + socket.on('joinRoom', (streamer,name) => {
57 - socket.join(room[num], () => { 57 + socket.join(streamer, () => {
58 - console.log('Someone join a ' + room[num]); 58 + console.log('Someone join a ' + streamer);
59 - io.to(room[num]).emit('joinRoom', num ,name); 59 + io.to(streamer).emit('joinRoom', streamer ,name);
60 + console.log(streamer,io)
60 }); 61 });
61 }); 62 });
62 63
63 - socket.on('chat message', (num, name, msg) => { 64 + socket.on('chat message', (streamer, name, msg) => {
64 - papago.detect(msg,io,room[num]); 65 + io.to(streamer).emit('chat message', name, msg);
65 - io.to(room[a]).emit('chat message', name, msg);
66 }); 66 });
67 }); 67 });
68 68
69 +
69 http.listen(process.env.SOCKET_PORT, () => { 70 http.listen(process.env.SOCKET_PORT, () => {
70 console.log(`Connect at ${process.env.SOCKET_PORT}`); 71 console.log(`Connect at ${process.env.SOCKET_PORT}`);
71 }); 72 });
72 73
74 +
73 /////////////////////Twitch Bot////////////////////////// 75 /////////////////////Twitch Bot//////////////////////////
74 76
75 77
76 -// // Register our event handlers (defined below) 78 +// Register our event handlers (defined below)
77 -// client.on('message', onMessageHandler); 79 +client.on('message', onMessageHandler);
78 -// client.on('connected', onConnectedHandler); 80 +client.on('connected', onConnectedHandler);
79 81
80 -// // Connect to Twitch: 82 +// Connect to Twitch:
81 -// client.connect(); 83 +client.connect();
82 84
83 -// // Called every time a message comes in 85 +// Called every time a message comes in
84 -// function onMessageHandler (target, context, msg, self) { 86 +function onMessageHandler (target, context, msg, self) {
85 -// if (self) { return; } // Ignore messages from the bot 87 + if (self) { return; } // Ignore messages from the bot
86 88
87 -// io.to(target).emit('chatmessage',) 89 + io.to(target.replace('#','')).emit('chat message',context["display-name"],msg)
88 -// papago.detectchat(msg, client, target); 90 + papago.detectchat(msg, client, io, target);
89 91
90 92
91 -// if(msg == '척척학사'){ 93 + if(msg == '척척학사2'){
92 -// client.say(target, `안녕하세요 척척학사의 방송입니다.`); 94 + client.say(target, `안녕하세요 척척학사의 방송입니다.`);
93 -// } 95 + }
94 96
95 -// } 97 +}
96 98
97 -// // Called every time the bot connects to Twitch chat 99 +// Called every time the bot connects to Twitch chat
98 -// function onConnectedHandler (addr, port) { 100 +function onConnectedHandler (addr, port) {
99 -// console.log(`*KwitchBot Connected to ${addr}:${port}`); 101 + console.log(`*KwitchBot Connected to ${addr}:${port}`);
100 -// } 102 +}
......
...@@ -50,8 +50,8 @@ ...@@ -50,8 +50,8 @@
50 </head> 50 </head>
51 <body> 51 <body>
52 <select> 52 <select>
53 - <option value="streamer1">streamer1</option> 53 + <option value="nnonuu">nnonuu</option>
54 - <option value="streamer2">streamer2</option> 54 + <option value="bachelorchuckchuck">bachelorchuckchuck</option>
55 </select> 55 </select>
56 <ul id="messages"></ul> 56 <ul id="messages"></ul>
57 <form action=""> 57 <form action="">
...@@ -64,21 +64,22 @@ ...@@ -64,21 +64,22 @@
64 $(() => { 64 $(() => {
65 const name = prompt('What your name'); 65 const name = prompt('What your name');
66 const socket = io(); 66 const socket = io();
67 - let room = ['streamer1', 'streamer2']; 67 + let room = ['nnonuu', 'bachelorchuckchuck'];
68 + var streamer = room[0]
68 let num = 0; 69 let num = 0;
69 - 70 + socket.emit('joinRoom', streamer, name);
70 - socket.emit('joinRoom', num, name);
71 71
72 $('select').change(() => { 72 $('select').change(() => {
73 - socket.emit('leaveRoom', num, name); 73 + socket.emit('leaveRoom', room[num], name);
74 num++; 74 num++;
75 num = num % 2; 75 num = num % 2;
76 - socket.emit('joinRoom', num, name); 76 + $('#messages').append($('<li>').text(num + room[num]));
77 + socket.emit('joinRoom', room[num], name);
77 }); 78 });
78 79
79 80
80 $('form').submit(() => { 81 $('form').submit(() => {
81 - socket.emit('chat message', num, name, $('#m').val()); 82 + socket.emit('chat message', streamer, name, $('#m').val());
82 $('#m').val(''); 83 $('#m').val('');
83 return false; 84 return false;
84 }); 85 });
...@@ -88,14 +89,14 @@ ...@@ -88,14 +89,14 @@
88 msg)); 89 msg));
89 }); 90 });
90 91
91 - socket.on('leaveRoom', (num, name) => { 92 + socket.on('leaveRoom', (streamer, name) => {
92 $('#messages').append($('<li>').text(name + ' leaved ' 93 $('#messages').append($('<li>').text(name + ' leaved '
93 - + room[num] + ' :(')); 94 + + streamer + ' :('));
94 }); 95 });
95 96
96 - socket.on('joinRoom', (num, name) => { 97 + socket.on('joinRoom', (streamer, name) => {
97 $('#messages').append($('<li>').text(name + ' joined ' 98 $('#messages').append($('<li>').text(name + ' joined '
98 - + room[num] + ':)')); 99 + + streamer + ':)'));
99 }); 100 });
100 }); 101 });
101 </script> 102 </script>
......