김대연

Add tmi socket

SOCKET_PORT=
TOKEN=
PAPAGO_ID=
PAPAGO_SECRET=
BOT_USERNAME=
OAUTH_TOKEN=
## SOCKET SERVER
> TMI client (twitch chat socket server)와 socket서버를 연결하여 front에게 채팅/번역된 채팅을 emit해주는 서버
## 현재 개발상태
> nodejs서버와 테스트용으로 만든 ejs를 결합.
> papago API와 연동하여 챗이 emit하면 한국어인지 판단하고 해당 room에 번역된 채팅을 뿌려줌
......@@ -46,3 +46,43 @@ exports.detect = (message,io,room) => {
}
});
}
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
......
const tmi = require('tmi.js');
const papago = require('./openAPIs/papago_api')
const ttlserver = require('../socket_server')
// 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);
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// 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
client.say(target, `/color `+changecolor());
papago.detectchat(msg, client, target);
if(msg == '척척학사'){
client.say(target, `안녕하세요 척척학사의 방송입니다.`);
}
}
exports.addChannel = (channel) =>{
opts.channels.append(channel);
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}
......
This diff could not be displayed because it is too large.
......@@ -13,6 +13,7 @@
"ejs": "^3.1.5",
"express": "^4.17.1",
"request": "^2.88.2",
"socket.io": "^3.0.3"
"socket.io": "^3.0.3",
"tmi": "^2.0.0"
}
}
......
......@@ -4,11 +4,25 @@ const app = require('express')();
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
app.set('view engine', 'ejs');
app.set('views', './testviews');
let room = ['streamer1', 'streamer2'];
// client.opts.channels;
let a = 0;
app.get('/', (req, res) => {
......@@ -44,13 +58,41 @@ io.on('connection', (socket) => {
});
});
socket.on('chat message', (num,name, msg) => {
socket.on('chat message', (num, name, msg) => {
papago.detect(msg,io,room[num]);
io.to(room[a]).emit('chat message', name, msg);
});
});
http.listen(process.env.SOCKET_PORT, () => {
console.log(`Connect at ${process.env.SOCKET_PORT}`);
});
\ No newline at end of file
});
/////////////////////Twitch Bot//////////////////////////
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// 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
io.to(target).emit('chatmessage',)
papago.detectchat(msg, client, target);
if(msg == '척척학사'){
client.say(target, `안녕하세요 척척학사의 방송입니다.`);
}
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
console.log(`*KwitchBot Connected to ${addr}:${port}`);
}
......