// OnTwitchNotification Handles Incoming Twitch Notifications
OnTwitchNotification Handles Incoming Twitch Notifications
// OnTwitchNotification Handles Incoming Twitch Notifications
func OnTwitchNotification(rw http.ResponseWriter, request *http.Request) {
	if http.MethodGet == request.Method {
		fmt.Println("Received GET")
		q := request.URL.Query()
		log.Println(q)
		mode := q.Get(twitch.TwitchHubModeQueryParameter)
		if twitch.TwitchModeDenied == mode {
			reason := q.Get(twitch.TwitchHubReasonQueryParameter)
			log.Println("Failed to Subscribe to Webhook: " + reason)
			rw.WriteHeader(http.StatusOK)
			return
		}
		challenge := q.Get(twitch.TwitchHubChallengeQueryParameter)
		rw.WriteHeader(http.StatusOK)
		rw.Write([]byte(challenge))
		return
	}
	if http.MethodPost == request.Method {
		fmt.Println("Received POST")
		var payload twitch.TwitchNotificationPayload
		err := httputil.DecodeJson(request.Body, &payload)
		if err != nil {
			panic(err)
		}
		for _, notification := range payload.Notifications {
			logNotification(&notification)
			if isLiveNotification(&notification) {
				discordClient.SendDiscordMessage(newTwitchLiveMessage(notification.UserId))
			}
		}
	}
}
func OnTwitchNotification ( rw http . ResponseWriter , request * http . Request ) { if http . MethodGet == request . Method { fmt . Println ( " " ) q := request . URL . Query ( ) log . Println ( q ) mode := q . Get ( twitch . TwitchHubModeQueryParameter ) if twitch . TwitchModeDenied == mode { reason := q . Get ( twitch . TwitchHubReasonQueryParameter ) log . Println ( " " + reason ) rw . WriteHeader ( http . StatusOK ) return } challenge := q . Get ( twitch . TwitchHubChallengeQueryParameter ) rw . WriteHeader ( http . StatusOK ) rw . Write ( [ ] byte ( challenge ) ) return } if http . MethodPost == request . Method { fmt . Println ( " " ) var payload twitch . TwitchNotificationPayload err := httputil . DecodeJson ( request . Body , & payload ) if err != nil { panic ( err ) } for _ , notification := range payload . Notifications { logNotification ( & notification ) if isLiveNotification ( & notification ) { discordClient . SendDiscordMessage ( newTwitchLiveMessage ( notification . UserId ) ) } } } }
OnTwitchNotification Handles Incoming Twitch Notifications
OnTwitchNotification Handles Incoming Twitch Notifications
// The GET occurs after the subscription to the stream update is made // The main purpose is to provide twitch a way to validate the endpoint //topic := q.Get(twitch.TwitchHubTopicQueryParameter) //lease := q.Get(twitch.TwitchHubLeaseQueryParameter) // The POST occurs when the actual event of going live occurs, we'll need to decode // the payload into notification objects, then send a discord webhook message // Iterate through all notifications, send discord message for live streams // Don't Send Messages for Duplicates or Title/Game Updates