From 76bee93338066d2a4842c5116f42ee8679a91657 Mon Sep 17 00:00:00 2001 From: John Costa Date: Mon, 30 Jun 2025 20:44:56 +0100 Subject: [PATCH] fix: challenge --- src/lib/twitch.ts | 19 +++++++++++++++++++ src/routes/api/webhook/+server.ts | 12 ++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/lib/twitch.ts b/src/lib/twitch.ts index 71d7717..28c5ab7 100644 --- a/src/lib/twitch.ts +++ b/src/lib/twitch.ts @@ -21,6 +21,23 @@ type TwitchSubscribeBody = { }; }; +export const twitchChallengeEvent = z.object({ + challenge: z.string(), + subscription: z.object({ + id: z.string(), + status: z.enum(['webhook_callback_verification_pending']), + type: z.enum(['channel.subscribe']), + version: z.string(), + condition: z.object({ broadcaster_user_id: z.string() }), + transport: z.object({ + method: z.enum(['webhook']), + callback: z.string(), + }), + created_at: z.string(), + cost: z.number(), + }) +}); + // Not the complete event, but the information we need. export const twitchFollowEvent = z.object({ subscription: z.object({ @@ -31,6 +48,8 @@ export const twitchFollowEvent = z.object({ }), }); +export const twitchEvent = z.union([twitchFollowEvent, twitchChallengeEvent]); + /* *curl -X POST 'https://api.twitch.tv/helix/eventsub/subscriptions' \ -H 'Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx' \ diff --git a/src/routes/api/webhook/+server.ts b/src/routes/api/webhook/+server.ts index 12451b9..160b91f 100644 --- a/src/routes/api/webhook/+server.ts +++ b/src/routes/api/webhook/+server.ts @@ -1,7 +1,7 @@ import { createHmac, timingSafeEqual } from "node:crypto"; import type { RequestHandler } from "@sveltejs/kit"; import { HASH_SECRET } from "$env/static/private"; -import { twitchFollowEvent } from "$lib/twitch"; +import { twitchChallengeEvent, twitchFollowEvent } from "$lib/twitch"; import { client } from "$lib"; const getHmacMessage = (request: Request, body: string): string => { return ( @@ -42,9 +42,13 @@ export const fallback: RequestHandler = async ({ request }) => { console.log(jsonBody); const event = twitchFollowEvent.safeParse(jsonBody); if (!event.success) { - console.log("Not a follow event"); - console.log(event.error); - return new Response(); + const stuff = twitchChallengeEvent.safeParse(jsonBody); + if (!stuff.success) { + console.log("unknown event!!"); + return new Response(); + } + + return new Response(stuff.data.challenge); } const { user_name } = event.data.event;