Skip to content

Commit

Permalink
testing eventsub
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Oct 15, 2024
1 parent 022b953 commit 195be46
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main_twitch-mz-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 17 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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'))
})
})
Expand Down
4 changes: 3 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {}
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"author": "Lluis <[email protected]>",
"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",
Expand Down

0 comments on commit 195be46

Please sign in to comment.