2024-02-13 19:13:44 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lyx0/nourybot/pkg/ivr"
|
|
|
|
"github.com/nicklaw5/helix/v2"
|
|
|
|
)
|
|
|
|
|
2024-02-14 22:13:07 +01:00
|
|
|
const (
|
|
|
|
eventSubResponseOK = 200
|
|
|
|
eventSubResponseAccepted = 202
|
|
|
|
eventSubResponseNoContent = 204
|
|
|
|
eventSubResponseBadRequest = 400
|
|
|
|
eventSubResponseUnauthorized = 401
|
|
|
|
eventSubResponseForbidden = 403
|
|
|
|
)
|
|
|
|
|
2024-02-28 12:34:22 +01:00
|
|
|
// createLiveSubscription creates a stream.online twitch eventsub subscription for the specified channel
|
2024-02-13 19:13:44 +01:00
|
|
|
func (app *application) createLiveSubscription(target, channel string) string {
|
|
|
|
uid := ivr.IDByUsername(channel)
|
2024-02-14 22:13:07 +01:00
|
|
|
if uid == "xd" {
|
|
|
|
return fmt.Sprintf("Could not find user with name %v", channel)
|
|
|
|
}
|
2024-02-13 19:13:44 +01:00
|
|
|
|
2024-02-14 22:13:07 +01:00
|
|
|
resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{
|
2024-02-13 19:13:44 +01:00
|
|
|
Type: helix.EventSubTypeStreamOnline,
|
|
|
|
Version: "1",
|
|
|
|
Condition: helix.EventSubCondition{
|
|
|
|
BroadcasterUserID: uid,
|
|
|
|
},
|
|
|
|
Transport: helix.EventSubTransport{
|
|
|
|
Method: "webhook",
|
2024-02-29 17:46:27 +01:00
|
|
|
Callback: fmt.Sprintf("https://bot.noury.li/eventsub/%v", target),
|
2024-02-13 19:13:44 +01:00
|
|
|
Secret: app.Config.eventSubSecret,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-02-14 22:13:07 +01:00
|
|
|
app.Log.Errorw("Error creating EventSub subscription",
|
2024-02-13 19:13:44 +01:00
|
|
|
"resp", resp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infof("%+v\n", resp)
|
|
|
|
return fmt.Sprintf("Created subscription for channel %v; uid=%v", channel, uid)
|
|
|
|
}
|
2024-02-14 22:13:07 +01:00
|
|
|
|
2024-02-28 12:34:22 +01:00
|
|
|
// deleteLiveSubscription deletes a stream.live twitch eventsub subscription for the specified channel
|
2024-02-14 22:13:07 +01:00
|
|
|
func (app *application) deleteLiveSubscription(target, channel string) string {
|
|
|
|
uid := ivr.IDByUsername(channel)
|
|
|
|
if uid == "xd" {
|
|
|
|
return fmt.Sprintf("Could not find user with name %v", channel)
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{
|
2024-02-14 22:13:07 +01:00
|
|
|
UserID: uid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error getting EventSub subscriptions",
|
|
|
|
"resp", resp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
return "Something went wrong FeelsBadMan"
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infof("%+v\n", resp)
|
|
|
|
|
|
|
|
if resp.StatusCode != eventSubResponseOK {
|
|
|
|
return "Subscription was not found FeelsBadMan"
|
|
|
|
}
|
|
|
|
if len(resp.Data.EventSubSubscriptions) == 0 {
|
|
|
|
return "No such subscription found"
|
|
|
|
}
|
2024-02-14 23:01:54 +01:00
|
|
|
|
|
|
|
// Since we can't pass more than one variable to the api call we loop
|
|
|
|
// over all the resp.Data.EventSubSubscriptions we get back and check
|
|
|
|
// which one has the type we want for this event. Then use this ones ID.
|
|
|
|
var eventSubID string
|
|
|
|
for i := 0; i < len(resp.Data.EventSubSubscriptions); i++ {
|
2024-02-29 17:46:27 +01:00
|
|
|
if resp.Data.EventSubSubscriptions[i].Type == "stream.online" &&
|
|
|
|
resp.Data.EventSubSubscriptions[i].Transport.Callback == fmt.Sprintf("https://bot.noury.li/eventsub/%s", target) {
|
2024-02-14 23:01:54 +01:00
|
|
|
eventSubID = resp.Data.EventSubSubscriptions[i].ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//eventSubID := resp.Data.EventSubSubscriptions[0].ID
|
|
|
|
//app.SendNoContext("nouryxd", eventSubID)
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
resp2, err := app.HelixClient.RemoveEventSubSubscription(eventSubID)
|
2024-02-14 23:01:54 +01:00
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error getting EventSub subscriptions",
|
|
|
|
"resp", resp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
return "Something went wrong FeelsBadMan"
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp2.StatusCode != eventSubResponseNoContent {
|
|
|
|
return "Subscription was not found FeelsBadMan"
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infof("%+v\n", resp2)
|
|
|
|
return fmt.Sprintf("Successfully deleted live notification for channel %s; id=%v", channel, uid)
|
|
|
|
}
|
|
|
|
|
2024-02-28 12:34:22 +01:00
|
|
|
// createOfflineSubscription creates a stream.offline twitch eventsub subscription for the specified channel
|
2024-02-14 23:01:54 +01:00
|
|
|
func (app *application) createOfflineSubscription(target, channel string) string {
|
|
|
|
uid := ivr.IDByUsername(channel)
|
|
|
|
if uid == "xd" {
|
|
|
|
return fmt.Sprintf("Could not find user with name %v", channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := app.HelixClient.CreateEventSubSubscription(&helix.EventSubSubscription{
|
|
|
|
Type: helix.EventSubTypeStreamOffline,
|
|
|
|
Version: "1",
|
|
|
|
Condition: helix.EventSubCondition{
|
|
|
|
BroadcasterUserID: uid,
|
|
|
|
},
|
|
|
|
Transport: helix.EventSubTransport{
|
|
|
|
Method: "webhook",
|
2024-02-29 17:46:27 +01:00
|
|
|
Callback: fmt.Sprintf("https://bot.noury.li/eventsub/%v", target),
|
2024-02-14 23:01:54 +01:00
|
|
|
Secret: app.Config.eventSubSecret,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error creating EventSub subscription",
|
|
|
|
"resp", resp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infof("%+v\n", resp)
|
|
|
|
return fmt.Sprintf("Created offline subscription for channel %v; uid=%v", channel, uid)
|
|
|
|
}
|
|
|
|
|
2024-02-28 12:34:22 +01:00
|
|
|
// deleteOfflineSubscription deletes a stream.offline twitch eventsub subscription for the specified channel
|
2024-02-14 23:01:54 +01:00
|
|
|
func (app *application) deleteOfflineSubscription(target, channel string) string {
|
|
|
|
uid := ivr.IDByUsername(channel)
|
|
|
|
if uid == "xd" {
|
|
|
|
return fmt.Sprintf("Could not find user with name %v", channel)
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
resp, err := app.HelixClient.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{
|
2024-02-14 23:01:54 +01:00
|
|
|
UserID: uid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error getting EventSub subscriptions",
|
|
|
|
"resp", resp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
return "Something went wrong FeelsBadMan"
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infof("%+v\n", resp)
|
|
|
|
|
|
|
|
if resp.StatusCode != eventSubResponseOK {
|
|
|
|
return "Subscription was not found FeelsBadMan"
|
|
|
|
}
|
|
|
|
if len(resp.Data.EventSubSubscriptions) == 0 {
|
|
|
|
return "No such subscription found"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we can't pass more than one variable to the api call we loop
|
|
|
|
// over all the resp.Data.EventSubSubscriptions we get back and check
|
|
|
|
// which one has the type we want for this event. Then use this ones ID.
|
|
|
|
var eventSubID string
|
|
|
|
for i := 0; i < len(resp.Data.EventSubSubscriptions); i++ {
|
2024-02-29 17:46:27 +01:00
|
|
|
if resp.Data.EventSubSubscriptions[i].Type == "stream.offline" &&
|
|
|
|
resp.Data.EventSubSubscriptions[i].Transport.Callback == fmt.Sprintf("https://bot.noury.li/eventsub/%s", target) {
|
2024-02-14 23:01:54 +01:00
|
|
|
eventSubID = resp.Data.EventSubSubscriptions[i].ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//eventSubID = resp.Data.EventSubSubscriptions[0].ID
|
|
|
|
//app.SendNoContext("nouryxd", eventSubID)
|
2024-02-14 22:13:07 +01:00
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
resp2, err := app.HelixClient.RemoveEventSubSubscription(eventSubID)
|
2024-02-14 22:13:07 +01:00
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error getting EventSub subscriptions",
|
|
|
|
"resp", resp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
return "Something went wrong FeelsBadMan"
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp2.StatusCode != eventSubResponseNoContent {
|
|
|
|
return "Subscription was not found FeelsBadMan"
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infof("%+v\n", resp2)
|
|
|
|
return fmt.Sprintf("Successfully deleted live notification for channel %s; id=%v", channel, uid)
|
|
|
|
}
|