diff --git a/.github/workflows/main_twitch-mz-bot.yml b/.github/workflows/main_twitch-mz-bot.yml index 1a26f8f..6515d28 100644 --- a/.github/workflows/main_twitch-mz-bot.yml +++ b/.github/workflows/main_twitch-mz-bot.yml @@ -46,6 +46,8 @@ jobs: envkey_AZURE_OPENAI_API_VERSION: ${{ secrets.AZURE_OPENAI_API_VERSION }} envkey_AZURE_OPENAI_VECTOR_STORE_ID: ${{ secrets.AZURE_OPENAI_VECTOR_STORE_ID }} envkey_OPENAI_MONGODB_URI: ${{ secrets.OPENAI_MONGODB_URI }} + envkey_TWITCH_EVENTSUB_SECRET: ${{ secrets.TWITCH_EVENTSUB_SECRET }} + envkey_TWITCH_HOSTNAME: ${{ secrets.TWITCH_HOSTNAME }} - name: Set up Node.js version uses: actions/setup-node@v4 with: diff --git a/app.js b/app.js index ba1281f..69780e4 100644 --- a/app.js +++ b/app.js @@ -7,11 +7,12 @@ const TempsDeFlorsService = require('./services/tempsDeFlors') const TwitchService = require("./services/twitch"); const ScreenshotService = require("./services/screenshot"); const moment = require('moment-timezone') +const {EventSubMiddleware} = require("@twurple/eventsub-http"); mongoose.connect(config.database).then(() => { const messenger = new Messenger() messenger.init() - .then(() => { + .then((apiClient) => { console.log('Connected') const app = express() @@ -113,9 +114,23 @@ mongoose.connect(config.database).then(() => { res.redirect(link.link); }) + const middleware = new EventSubMiddleware({ + apiClient, + hostName: config.twitch.hostname, + pathPrefix: '/twitch', + secret: config.twitch.eventSubSecret, + strictHostCheck: false + }); + middleware.apply(app) - const listener = app.listen(process.env.PORT, ()=> { + const listener = app.listen(process.env.PORT, async ()=> { console.log('Listening on port ', + listener.address().port) + + await middleware.markAsReady(); + middleware.onChannelFollow(config.twitch.roomId, config.twitch.userId, event => { + console.log(`${event.userDisplayName} just followed ${event.broadcasterDisplayName}!`); + }); + app.get('/', (req, res) => res.redirect('/stream')) }) }) diff --git a/config/index.js b/config/index.js index d94b1e1..07d2b9f 100644 --- a/config/index.js +++ b/config/index.js @@ -7,7 +7,9 @@ module.exports = { clientSecret: process.env.TWITCH_CLIENT_SECRET, userId: process.env.TWITCH_USER_ID, roomId: process.env.TWITCH_ROOM_ID, - username: process.env.TWITCH_USER_NAME + username: process.env.TWITCH_USER_NAME, + eventSubSecret: process.env.TWITCH_EVENTSUB_SECRET, + hostname: process.env.TWITCH_HOSTNAME, }, aemet: { apiKey: process.env.AEMET_API_KEY diff --git a/lib/messenger.js b/lib/messenger.js index 1295c37..8cc7931 100644 --- a/lib/messenger.js +++ b/lib/messenger.js @@ -9,6 +9,7 @@ const TokenService = require("../services/token") const Notifier = require("./notifier") const {isMod, isBroadcaster, isVip, isAdmin, isEditor} = require("./helper"); const Logger = require('../services/logger') +const { ApiClient } = require('@twurple/api'); class Messenger { constructor () {} @@ -53,10 +54,14 @@ class Messenger { } listen () { - this.bot.on('message', this.handleText.bind(this)); - this.bot.on('connected', this.handleConnect.bind(this)); + const apiClient = new ApiClient({ authProvider: this.authProvider }) - return this.bot.connect().catch(console.error) + this.bot.on('message', this.handleText.bind(this)) + this.bot.on('connected', this.handleConnect.bind(this)) + + + this.bot.connect().catch(console.error) + return apiClient } async handleText (target, context, msg, self) { diff --git a/package.json b/package.json index 37f7123..34d59b4 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,11 @@ "author": "Lluis ", "license": "MIT", "dependencies": { - "@twurple/auth": "^7.1.0", - "@twurple/auth-tmi": "^7.1.0", + "@twurple/api": "^7.2.0", + "@twurple/auth": "^7.2.0", + "@twurple/auth-tmi": "^7.2.0", + "@twurple/eventsub-http": "^7.2.0", + "@twurple/eventsub-ws": "^7.2.0", "dotenv": "^16.4.5", "ejs": "^3.1.10", "express": "^4.19.2",