김대연

Add tmi socket

1 +SOCKET_PORT=
2 +TOKEN=
3 +PAPAGO_ID=
4 +PAPAGO_SECRET=
5 +BOT_USERNAME=
6 +OAUTH_TOKEN=
1 +## SOCKET SERVER
2 +
3 +> TMI client (twitch chat socket server)와 socket서버를 연결하여 front에게 채팅/번역된 채팅을 emit해주는 서버
4 +
5 +## 현재 개발상태
6 +
7 +> nodejs서버와 테스트용으로 만든 ejs를 결합.
8 +> papago API와 연동하여 챗이 emit하면 한국어인지 판단하고 해당 room에 번역된 채팅을 뿌려줌
9 +
...@@ -46,3 +46,43 @@ exports.detect = (message,io,room) => { ...@@ -46,3 +46,43 @@ exports.detect = (message,io,room) => {
46 } 46 }
47 }); 47 });
48 } 48 }
49 +
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 +
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
......
1 +const tmi = require('tmi.js');
2 +const papago = require('./openAPIs/papago_api')
3 +const ttlserver = require('../socket_server')
4 +
5 +// Define configuration options
6 +var opts = {
7 + identity: {
8 + username: process.env.BOT_USERNAME,
9 + password: process.env.OAUTH_TOKEN
10 + },
11 + channels: [
12 + 'bachelorchuckchuck'
13 + ]
14 +};
15 +
16 +// Create a client with our options
17 +const client = new tmi.client(opts);
18 +
19 +// Register our event handlers (defined below)
20 +client.on('message', onMessageHandler);
21 +client.on('connected', onConnectedHandler);
22 +
23 +// Connect to Twitch:
24 +client.connect();
25 +
26 +// Called every time a message comes in
27 +function onMessageHandler (target, context, msg, self) {
28 + if (self) { return; } // Ignore messages from the bot
29 +
30 + client.say(target, `/color `+changecolor());
31 + papago.detectchat(msg, client, target);
32 +
33 + if(msg == '척척학사'){
34 + client.say(target, `안녕하세요 척척학사의 방송입니다.`);
35 + }
36 +
37 +}
38 +
39 +exports.addChannel = (channel) =>{
40 + opts.channels.append(channel);
41 +}
42 +
43 +// Called every time the bot connects to Twitch chat
44 +function onConnectedHandler (addr, port) {
45 + console.log(`* Connected to ${addr}:${port}`);
46 +}
......
This diff could not be displayed because it is too large.
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
13 "ejs": "^3.1.5", 13 "ejs": "^3.1.5",
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 } 18 }
18 } 19 }
......
...@@ -4,11 +4,25 @@ const app = require('express')(); ...@@ -4,11 +4,25 @@ const app = require('express')();
4 const http = require('http').Server(app); 4 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 +const tmi = require('tmi.js');
8 +// Define configuration options
9 +var opts = {
10 + identity: {
11 + username: process.env.BOT_USERNAME,
12 + password: process.env.OAUTH_TOKEN
13 + },
14 + channels: [
15 + 'bachelorchuckchuck'
16 + ]
17 + };
18 +// Create a client with our options
19 +const client = new tmi.client(opts); //twitch chatbot client
7 20
8 app.set('view engine', 'ejs'); 21 app.set('view engine', 'ejs');
9 app.set('views', './testviews'); 22 app.set('views', './testviews');
10 23
11 let room = ['streamer1', 'streamer2']; 24 let room = ['streamer1', 'streamer2'];
25 +// client.opts.channels;
12 let a = 0; 26 let a = 0;
13 27
14 app.get('/', (req, res) => { 28 app.get('/', (req, res) => {
...@@ -44,13 +58,41 @@ io.on('connection', (socket) => { ...@@ -44,13 +58,41 @@ io.on('connection', (socket) => {
44 }); 58 });
45 }); 59 });
46 60
47 - socket.on('chat message', (num,name, msg) => { 61 + socket.on('chat message', (num, name, msg) => {
48 papago.detect(msg,io,room[num]); 62 papago.detect(msg,io,room[num]);
49 io.to(room[a]).emit('chat message', name, msg); 63 io.to(room[a]).emit('chat message', name, msg);
50 }); 64 });
51 }); 65 });
52 66
53 -
54 http.listen(process.env.SOCKET_PORT, () => { 67 http.listen(process.env.SOCKET_PORT, () => {
55 console.log(`Connect at ${process.env.SOCKET_PORT}`); 68 console.log(`Connect at ${process.env.SOCKET_PORT}`);
56 -});
...\ No newline at end of file ...\ No newline at end of file
69 +});
70 +
71 +/////////////////////Twitch Bot//////////////////////////
72 +
73 +
74 +// Register our event handlers (defined below)
75 +client.on('message', onMessageHandler);
76 +client.on('connected', onConnectedHandler);
77 +
78 +// Connect to Twitch:
79 +client.connect();
80 +
81 +// Called every time a message comes in
82 +function onMessageHandler (target, context, msg, self) {
83 + if (self) { return; } // Ignore messages from the bot
84 +
85 + io.to(target).emit('chatmessage',)
86 + papago.detectchat(msg, client, target);
87 +
88 +
89 + if(msg == '척척학사'){
90 + client.say(target, `안녕하세요 척척학사의 방송입니다.`);
91 + }
92 +
93 +}
94 +
95 +// Called every time the bot connects to Twitch chat
96 +function onConnectedHandler (addr, port) {
97 + console.log(`*KwitchBot Connected to ${addr}:${port}`);
98 +}
......