김대연

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'
const PAPAGO_ID = process.env.PAPAGO_ID
const PAPAGO_SECRET = process.env.PAPAGO_SECRET
exports.trans = (message, lang, io, room) => {
exports.transchat = (message, lang, client,io, target) => {
request.post(
{
url: PAPAGO_URL,
......@@ -17,16 +18,16 @@ exports.trans = (message, lang, io, room) => {
},
body: `source=${lang}&target=ko&text=` + message,
json:true
},(error, response, body) => {
},async (error, response, body) => {
if(!error && response.statusCode == 200) {
var Translated = body.message.result.translatedText;
io.to(room).emit('chat message', "trans", Translated);
var Translated = await body.message.result.translatedText;
client.say(target, "(번역) "+Translated);
io.to(target.replace('#','')).emit('chat message', "번역", Translated);
}
});
}
exports.detect = (message,io,room) => {
exports.detectchat = (message, client, io, target) => {
request.post(
{
url: dPAPAGO_URL,
......@@ -41,48 +42,51 @@ exports.detect = (message,io,room) => {
if(!error && response.statusCode == 200) {
var lang = body.langCode;
if(lang != 'ko'){
this.trans(message,lang,io,room)
this.transchat(message,lang,client,io,target)
}
}
});
}
exports.transchat = (message, lang, client, 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=${lang}&target=ko&text=` + message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
var Translated = body.message.result.translatedText;
client.say(target, "(번역) ", Translated);
}
});
}
exports.detectchat = (message, client,target) => {
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,client,target)
}
}
});
}
\ No newline at end of file
// 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)
// }
// }
// });
// }
......
This diff could not be displayed because it is too large.
......@@ -14,6 +14,6 @@
"express": "^4.17.1",
"request": "^2.88.2",
"socket.io": "^3.0.3",
"tmi": "^2.0.0"
"tmi.js": "^1.5.0"
}
}
......
......@@ -5,24 +5,24 @@ const http = require('http').Server(app);
const io = require('socket.io')(http);
const papago = require('./openAPIs/papago_api');
// const tmi = require('tmi.js');
// // Define configuration options
// var opts = {
// identity: {
// username: process.env.BOT_USERNAME,
// password: process.env.OAUTH_TOKEN
// },
// channels: [
// 'bachelorchuckchuck'
// ]
// };
// // Create a client with our options
// const client = new tmi.client(opts); //twitch chatbot client
const tmi = require('tmi.js');
// Define configuration options
var opts = {
identity: {
username: process.env.BOT_USERNAME,
password: process.env.OAUTH_TOKEN
},
channels: [
'nnonuu'
]
};
// Create a client with our options
const client = new tmi.client(opts); //twitch chatbot client
app.set('view engine', 'ejs');
app.set('views', './testviews');
let room = ['streamer1', 'streamer2'];
let room = ['nnonuu', 'bachelorchuckchuck'];
// client.opts.channels;
let a = 0;
......@@ -46,55 +46,57 @@ io.on('connection', (socket) => {
console.log('user disconnected');
});
socket.on('leaveRoom', (num,name) => {
socket.leave(room[num], () => {
console.log('Someone leave a ' + room[num]);
io.to(room[num]).emit('leaveRoom', num ,name);
socket.on('leaveRoom', (streamer,name) => {
socket.leave(streamer, () => {
console.log('Someone leave a ' + streamer);
io.to(streamer).emit('leaveRoom', streamer ,name);
});
});
socket.on('joinRoom', (num,name) => {
socket.join(room[num], () => {
console.log('Someone join a ' + room[num]);
io.to(room[num]).emit('joinRoom', num ,name);
socket.on('joinRoom', (streamer,name) => {
socket.join(streamer, () => {
console.log('Someone join a ' + streamer);
io.to(streamer).emit('joinRoom', streamer ,name);
console.log(streamer,io)
});
});
socket.on('chat message', (num, name, msg) => {
papago.detect(msg,io,room[num]);
io.to(room[a]).emit('chat message', name, msg);
socket.on('chat message', (streamer, name, msg) => {
io.to(streamer).emit('chat message', name, msg);
});
});
http.listen(process.env.SOCKET_PORT, () => {
console.log(`Connect at ${process.env.SOCKET_PORT}`);
});
/////////////////////Twitch Bot//////////////////////////
// // Register our event handlers (defined below)
// client.on('message', onMessageHandler);
// client.on('connected', onConnectedHandler);
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// // Connect to Twitch:
// client.connect();
// Connect to Twitch:
client.connect();
// // Called every time a message comes in
// function onMessageHandler (target, context, msg, self) {
// if (self) { return; } // Ignore messages from the bot
// Called every time a message comes in
function onMessageHandler (target, context, msg, self) {
if (self) { return; } // Ignore messages from the bot
// io.to(target).emit('chatmessage',)
// papago.detectchat(msg, client, target);
io.to(target.replace('#','')).emit('chat message',context["display-name"],msg)
papago.detectchat(msg, client, io, target);
// if(msg == '척척학사'){
// client.say(target, `안녕하세요 척척학사의 방송입니다.`);
// }
if(msg == '척척학사2'){
client.say(target, `안녕하세요 척척학사의 방송입니다.`);
}
// }
}
// // Called every time the bot connects to Twitch chat
// function onConnectedHandler (addr, port) {
// console.log(`*KwitchBot Connected to ${addr}:${port}`);
// }
// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
console.log(`*KwitchBot Connected to ${addr}:${port}`);
}
......
......@@ -50,8 +50,8 @@
</head>
<body>
<select>
<option value="streamer1">streamer1</option>
<option value="streamer2">streamer2</option>
<option value="nnonuu">nnonuu</option>
<option value="bachelorchuckchuck">bachelorchuckchuck</option>
</select>
<ul id="messages"></ul>
<form action="">
......@@ -64,21 +64,22 @@
$(() => {
const name = prompt('What your name');
const socket = io();
let room = ['streamer1', 'streamer2'];
let room = ['nnonuu', 'bachelorchuckchuck'];
var streamer = room[0]
let num = 0;
socket.emit('joinRoom', num, name);
socket.emit('joinRoom', streamer, name);
$('select').change(() => {
socket.emit('leaveRoom', num, name);
socket.emit('leaveRoom', room[num], name);
num++;
num = num % 2;
socket.emit('joinRoom', num, name);
$('#messages').append($('<li>').text(num + room[num]));
socket.emit('joinRoom', room[num], name);
});
$('form').submit(() => {
socket.emit('chat message', num, name, $('#m').val());
socket.emit('chat message', streamer, name, $('#m').val());
$('#m').val('');
return false;
});
......@@ -88,14 +89,14 @@
msg));
});
socket.on('leaveRoom', (num, name) => {
socket.on('leaveRoom', (streamer, name) => {
$('#messages').append($('<li>').text(name + ' leaved '
+ room[num] + ' :('));
+ streamer + ' :('));
});
socket.on('joinRoom', (num, name) => {
socket.on('joinRoom', (streamer, name) => {
$('#messages').append($('<li>').text(name + ' joined '
+ room[num] + ':)'));
+ streamer + ':)'));
});
});
</script>
......