package messages import ( "github.com/mitchellh/mapstructure" "go.fifitido.net/twitch/eventsub/events" ) func decode[T any](input map[string]any) (T, error) { var result T decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ Metadata: nil, Result: &result, TagName: "json", }) if err != nil { return result, err } if err := decoder.Decode(input); err != nil { return result, err } return result, nil } func decodeNotificationPayload(data map[string]any) (NotificationPayload, error) { payload, err := decode[NotificationPayload](data) if err != nil { return NotificationPayload{}, err } event := payload.Event.(map[string]any) switch payload.Subscription.Type { case "channel.update": var err error payload.Event, err = decode[events.ChannelUpdateEvent](event) return NotificationPayload{}, err case "channel.follow": var err error payload.Event, err = decode[events.ChannelFollowEvent](event) return NotificationPayload{}, err case "channel.ad_break.begin": var err error payload.Event, err = decode[events.ChannelAdBreakBeginEvent](event) return NotificationPayload{}, err case "channel.chat.clear": var err error payload.Event, err = decode[events.ChannelChatClearEvent](event) return NotificationPayload{}, err case "channel.chat.clear_user_messages": var err error payload.Event, err = decode[events.ChannelChatClearUserMessagesEvent](event) return NotificationPayload{}, err case "channel.chat.message": var err error payload.Event, err = decode[events.ChannelChatMessageEvent](event) return NotificationPayload{}, err case "channel.chat.message_delete": var err error payload.Event, err = decode[events.ChannelChatMessageDeleteEvent](event) return NotificationPayload{}, err case "channel.chat.notification": var err error payload.Event, err = decode[events.ChannelChatNotificationEvent](event) return NotificationPayload{}, err case "channel.chat_settings.update": var err error payload.Event, err = decode[events.ChannelChatSettingsUpdateEvent](event) return NotificationPayload{}, err case "channel.subscribe": var err error payload.Event, err = decode[events.ChannelSubscribeEvent](event) return NotificationPayload{}, err case "channel.subscription.end": var err error payload.Event, err = decode[events.ChannelSubscriptionEndEvent](event) return NotificationPayload{}, err case "channel.subscription.gift": var err error payload.Event, err = decode[events.ChannelSubscriptionGiftEvent](event) return NotificationPayload{}, err case "channel.subscription.message": var err error payload.Event, err = decode[events.ChannelSubscriptionMessageEvent](event) return NotificationPayload{}, err case "channel.cheer": var err error payload.Event, err = decode[events.ChannelCheerEvent](event) return NotificationPayload{}, err case "channel.raid": var err error payload.Event, err = decode[events.ChannelRaidEvent](event) return NotificationPayload{}, err case "channel.ban": var err error payload.Event, err = decode[events.ChannelBanEvent](event) return NotificationPayload{}, err case "channel.unban": var err error payload.Event, err = decode[events.ChannelUnbanEvent](event) return NotificationPayload{}, err case "channel.moderator.add": var err error payload.Event, err = decode[events.ChannelModeratorAddEvent](event) return NotificationPayload{}, err case "channel.moderator.remove": var err error payload.Event, err = decode[events.ChannelModeratorRemoveEvent](event) return NotificationPayload{}, err case "channel.guest_star_session.begin": var err error payload.Event, err = decode[events.ChannelGuestStarSessionBeginEvent](event) return NotificationPayload{}, err case "channel.guest_star_session.end": var err error payload.Event, err = decode[events.ChannelGuestStarSessionEndEvent](event) return NotificationPayload{}, err case "channel.guest_star_guest.update": var err error payload.Event, err = decode[events.ChannelGuestStarGuestUpdateEvent](event) return NotificationPayload{}, err case "channel.guest_star_settings.update": var err error payload.Event, err = decode[events.ChannelGuestStarSettingsUpdateEvent](event) return NotificationPayload{}, err case "channel.channel_points_custom_reward.add": var err error payload.Event, err = decode[events.ChannelPointsCustomRewardAddEvent](event) return NotificationPayload{}, err case "channel.channel_points_custom_reward.update": var err error payload.Event, err = decode[events.ChannelPointsCustomRewardUpdateEvent](event) return NotificationPayload{}, err case "channel.channel_points_custom_reward.remove": var err error payload.Event, err = decode[events.ChannelPointsCustomRewardRemoveEvent](event) return NotificationPayload{}, err case "channel.channel_points_custom_reward_redemption.add": var err error payload.Event, err = decode[events.ChannelPointsCustomRewardRedemptionAddEvent](event) return NotificationPayload{}, err case "channel.channel_points_custom_reward_redemption.update": var err error payload.Event, err = decode[events.ChannelPointsCustomRewardRedemptionUpdateEvent](event) return NotificationPayload{}, err case "channel.poll.begin": var err error payload.Event, err = decode[events.ChannelPollBeginEvent](event) return NotificationPayload{}, err case "channel.poll.progress": var err error payload.Event, err = decode[events.ChannelPollProgressEvent](event) return NotificationPayload{}, err case "channel.poll.end": var err error payload.Event, err = decode[events.ChannelPollEndEvent](event) return NotificationPayload{}, err case "channel.prediction.begin": var err error payload.Event, err = decode[events.ChannelPredictionBeginEvent](event) return NotificationPayload{}, err case "channel.prediction.progress": var err error payload.Event, err = decode[events.ChannelPredictionProgressEvent](event) return NotificationPayload{}, err case "channel.prediction.lock": var err error payload.Event, err = decode[events.ChannelPredictionLockEvent](event) return NotificationPayload{}, err case "channel.prediction.end": var err error payload.Event, err = decode[events.ChannelPredictionEndEvent](event) return NotificationPayload{}, err case "channel.hype_train.begin": var err error payload.Event, err = decode[events.HypeTrainBeginEvent](event) return NotificationPayload{}, err case "channel.hype_train.progress": var err error payload.Event, err = decode[events.HypeTrainProgressEvent](event) return NotificationPayload{}, err case "channel.hype_train.end": var err error payload.Event, err = decode[events.HypeTrainEndEvent](event) return NotificationPayload{}, err case "channel.charity_campaign.donate": var err error payload.Event, err = decode[events.CharityDonationEvent](event) return NotificationPayload{}, err case "channel.charity_campaign.start": var err error payload.Event, err = decode[events.CharityCampaignStartEvent](event) return NotificationPayload{}, err case "channel.charity_campaign.progress": var err error payload.Event, err = decode[events.CharityCampaignProgressEvent](event) return NotificationPayload{}, err case "channel.charity_campaign.stop": var err error payload.Event, err = decode[events.CharityCampaignStopEvent](event) return NotificationPayload{}, err case "channel.shield_mode.begin": var err error payload.Event, err = decode[events.ShieldModeEvent](event) return NotificationPayload{}, err case "channel.shield_mode.end": var err error payload.Event, err = decode[events.ShieldModeEvent](event) return NotificationPayload{}, err case "channel.shoutout.create": var err error payload.Event, err = decode[events.ShoutoutCreateEvent](event) return NotificationPayload{}, err case "channel.shoutout.receive": var err error payload.Event, err = decode[events.ShoutoutReceivedEvent](event) return NotificationPayload{}, err case "conduit.shard.disabled": var err error payload.Event, err = decode[events.ConduitShardDisabledEvent](event) return NotificationPayload{}, err case "drop.entitlement.grant": var err error payload.Event, err = decode[events.DropEntitlementGrantEvent](event) return NotificationPayload{}, err case "extension.bits_transaction.create": var err error payload.Event, err = decode[events.ExtensionBitsTransactionCreateEvent](event) return NotificationPayload{}, err case "channel.goal.begin": var err error payload.Event, err = decode[events.GoalsEvent](event) return NotificationPayload{}, err case "channel.goal.progress": var err error payload.Event, err = decode[events.GoalsEvent](event) return NotificationPayload{}, err case "channel.goal.end": var err error payload.Event, err = decode[events.GoalsEvent](event) return NotificationPayload{}, err case "stream.online": var err error payload.Event, err = decode[events.StreamOnlineEvent](event) return NotificationPayload{}, err case "stream.offline": var err error payload.Event, err = decode[events.StreamOfflineEvent](event) return NotificationPayload{}, err case "user.authorization.grant": var err error payload.Event, err = decode[events.UserAuthorizationGrantEvent](event) return NotificationPayload{}, err case "user.authorization.revoke": var err error payload.Event, err = decode[events.UserAuthorizationRevokeEvent](event) return NotificationPayload{}, err case "user.update": var err error payload.Event, err = decode[events.UserUpdateEvent](event) return NotificationPayload{}, err } return payload, nil }