mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
make eventsub events work on multiple channels
This commit is contained in:
parent
13c7fd30b2
commit
e993de4eb1
|
@ -31,7 +31,7 @@ func (app *application) createLiveSubscription(target, channel string) string {
|
||||||
},
|
},
|
||||||
Transport: helix.EventSubTransport{
|
Transport: helix.EventSubTransport{
|
||||||
Method: "webhook",
|
Method: "webhook",
|
||||||
Callback: "https://bot.noury.li/eventsub",
|
Callback: fmt.Sprintf("https://bot.noury.li/eventsub/%v", target),
|
||||||
Secret: app.Config.eventSubSecret,
|
Secret: app.Config.eventSubSecret,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -78,7 +78,8 @@ func (app *application) deleteLiveSubscription(target, channel string) string {
|
||||||
// which one has the type we want for this event. Then use this ones ID.
|
// which one has the type we want for this event. Then use this ones ID.
|
||||||
var eventSubID string
|
var eventSubID string
|
||||||
for i := 0; i < len(resp.Data.EventSubSubscriptions); i++ {
|
for i := 0; i < len(resp.Data.EventSubSubscriptions); i++ {
|
||||||
if resp.Data.EventSubSubscriptions[i].Type == "stream.online" {
|
if resp.Data.EventSubSubscriptions[i].Type == "stream.online" &&
|
||||||
|
resp.Data.EventSubSubscriptions[i].Transport.Callback == fmt.Sprintf("https://bot.noury.li/eventsub/%s", target) {
|
||||||
eventSubID = resp.Data.EventSubSubscriptions[i].ID
|
eventSubID = resp.Data.EventSubSubscriptions[i].ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,7 +118,7 @@ func (app *application) createOfflineSubscription(target, channel string) string
|
||||||
},
|
},
|
||||||
Transport: helix.EventSubTransport{
|
Transport: helix.EventSubTransport{
|
||||||
Method: "webhook",
|
Method: "webhook",
|
||||||
Callback: "https://bot.noury.li/eventsub",
|
Callback: fmt.Sprintf("https://bot.noury.li/eventsub/%v", target),
|
||||||
Secret: app.Config.eventSubSecret,
|
Secret: app.Config.eventSubSecret,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -164,7 +165,8 @@ func (app *application) deleteOfflineSubscription(target, channel string) string
|
||||||
// which one has the type we want for this event. Then use this ones ID.
|
// which one has the type we want for this event. Then use this ones ID.
|
||||||
var eventSubID string
|
var eventSubID string
|
||||||
for i := 0; i < len(resp.Data.EventSubSubscriptions); i++ {
|
for i := 0; i < len(resp.Data.EventSubSubscriptions); i++ {
|
||||||
if resp.Data.EventSubSubscriptions[i].Type == "stream.offline" {
|
if resp.Data.EventSubSubscriptions[i].Type == "stream.offline" &&
|
||||||
|
resp.Data.EventSubSubscriptions[i].Transport.Callback == fmt.Sprintf("https://bot.noury.li/eventsub/%s", target) {
|
||||||
eventSubID = resp.Data.EventSubSubscriptions[i].ID
|
eventSubID = resp.Data.EventSubSubscriptions[i].ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/lyx0/nourybot/internal/data"
|
"github.com/lyx0/nourybot/internal/data"
|
||||||
|
@ -23,7 +22,7 @@ func (app *application) startRouter() {
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
router.GET("/", app.homeRoute)
|
router.GET("/", app.homeRoute)
|
||||||
router.GET("/status", app.statusPageRoute)
|
router.GET("/status", app.statusPageRoute)
|
||||||
router.POST("/eventsub", app.eventsubFollow)
|
router.POST("/eventsub/:channel", app.eventsubFollow)
|
||||||
router.GET("/commands", app.commandsRoute)
|
router.GET("/commands", app.commandsRoute)
|
||||||
router.GET("/commands/:channel", app.channelCommandsRoute)
|
router.GET("/commands/:channel", app.channelCommandsRoute)
|
||||||
router.GET("/timer", app.timersRoute)
|
router.GET("/timer", app.timersRoute)
|
||||||
|
@ -46,9 +45,10 @@ type eventSubNotification struct {
|
||||||
// eventsubMessageId stores the last message id of an event sub. Twitch resends events
|
// eventsubMessageId stores the last message id of an event sub. Twitch resends events
|
||||||
// if it is unsure that you have gotten them so we check if the last event has the same
|
// if it is unsure that you have gotten them so we check if the last event has the same
|
||||||
// message id and if it does discard the event.
|
// message id and if it does discard the event.
|
||||||
var lastEventSubSubscriptionId = ""
|
var lastEventSubSubscriptionId = []string{"xd"}
|
||||||
|
|
||||||
func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
channel := ps.ByName("channel")
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -74,11 +74,16 @@ func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _
|
||||||
w.Write([]byte(vals.Challenge))
|
w.Write([]byte(vals.Challenge))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//r.Body.Close()
|
||||||
|
|
||||||
if vals.Subscription.ID == lastEventSubSubscriptionId {
|
// Check if the current events subscription id equals the last events.
|
||||||
return
|
// If it does ignore the event since it's a repeated event.
|
||||||
} else {
|
for i := 0; i < len(lastEventSubSubscriptionId); i++ {
|
||||||
lastEventSubSubscriptionId = vals.Subscription.ID
|
if vals.Subscription.ID == lastEventSubSubscriptionId[i] {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
lastEventSubSubscriptionId[i] = vals.Subscription.ID
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vals.Subscription.Type {
|
switch vals.Subscription.Type {
|
||||||
|
@ -86,24 +91,22 @@ func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _
|
||||||
var liveEvent helix.EventSubStreamOnlineEvent
|
var liveEvent helix.EventSubStreamOnlineEvent
|
||||||
|
|
||||||
err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&liveEvent)
|
err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&liveEvent)
|
||||||
log.Printf("got stream online event webhook: %s is live\n", liveEvent.BroadcasterUserName)
|
log.Printf("got stream online event webhook: [%s]: %s is live\n", channel, liveEvent.BroadcasterUserName)
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Write([]byte("ok"))
|
w.Write([]byte("ok"))
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
|
|
||||||
game := ivr.GameByUsername(liveEvent.BroadcasterUserLogin)
|
game := ivr.GameByUsername(liveEvent.BroadcasterUserLogin)
|
||||||
title := ivr.TitleByUsername(liveEvent.BroadcasterUserLogin)
|
title := ivr.TitleByUsername(liveEvent.BroadcasterUserLogin)
|
||||||
|
|
||||||
app.SendNoBanphrase("nouryxd", fmt.Sprintf("%s went live FeelsGoodMan Game: %s; Title: %s; https://twitch.tv/%s", liveEvent.BroadcasterUserName, game, title, liveEvent.BroadcasterUserLogin))
|
app.SendNoBanphrase(channel, fmt.Sprintf("%s went live FeelsGoodMan Game: %s; Title: %s; https://twitch.tv/%s", liveEvent.BroadcasterUserName, game, title, liveEvent.BroadcasterUserLogin))
|
||||||
|
|
||||||
case helix.EventSubTypeStreamOffline:
|
case helix.EventSubTypeStreamOffline:
|
||||||
var offlineEvent helix.EventSubStreamOfflineEvent
|
var offlineEvent helix.EventSubStreamOfflineEvent
|
||||||
err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&offlineEvent)
|
err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&offlineEvent)
|
||||||
log.Printf("got stream offline event webhook: %s is now offline\n", offlineEvent.BroadcasterUserName)
|
log.Printf("got stream online event webhook: [%s]: %s is live\n", channel, offlineEvent.BroadcasterUserName)
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Write([]byte("ok"))
|
w.Write([]byte("ok"))
|
||||||
app.SendNoBanphrase("nouryxd", fmt.Sprintf("%s went offline FeelsBadMan", offlineEvent.BroadcasterUserName))
|
app.SendNoBanphrase(channel, fmt.Sprintf("%s went offline FeelsBadMan", offlineEvent.BroadcasterUserName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue