So I’ll bump this thread now as I got further but I’m stuck again.
I now have a node express server that listens to port 2083.
With that I can use https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions to get the latest redemptions for a specific reward which works fine.
As I got that working, I am now trying to use channel.channel_points_custom_reward_redemption.add instead.
For that I created an event like so:
app.get('/register-webhook', function(req, res) {
if(req.session && req.session.passport && req.session.passport.user) {
var clientId = TWITCH_CLIENT_ID;
var clientSecret = TWITCH_SECRET;
// Request an app access token
var tokenOptions = {
url: 'https://id.twitch.tv/oauth2/token',
method: 'POST',
form: {
client_id: clientId,
client_secret: clientSecret,
grant_type: 'client_credentials'
}
};
request(tokenOptions, function (error, response, body) {
if (response && response.statusCode == 200) {
var accessToken = JSON.parse(body).access_token;
// Register the webhook endpoint with Twitch
var webhookOptions = {
url: 'https://api.twitch.tv/helix/eventsub/subscriptions',
method: 'POST',
headers: {
'Client-ID': clientId,
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
json: {
type: 'channel.channel_points_custom_reward_redemption.add',
version: '1',
condition: {
broadcaster_user_id: 'myid',
reward_id: 'rewardid'
},
transport: {
method: 'webhook',
callback: 'https://www.censored.fun/webhooks/custom_reward_redemption/add',
secret: 'mysecret'
}
}
};
request(webhookOptions, function (error, response, body) {
if (response && response.statusCode == 200) {
res.send('Webhook registered successfully');
} else {
res.status(response.statusCode).send(body);
}
});
} else {
res.status(response.statusCode).send(body);
}
});
} else {
res.status(401).send('Unauthorized');
}
});
Which then returns this (i censored my stuff):
{
"data": [
{
"id": "censored",
"status": "webhook_callback_verification_pending",
"type": "channel.channel_points_custom_reward_redemption.add",
"version": "1",
"condition": {
"broadcaster_user_id": "censored",
"reward_id": "censored"
},
"created_at": "2024-04-28T09:47:03.444336379Z",
"transport": {
"method": "webhook",
"callback": "https://www.censored.fun/webhooks/custom_reward_redemption/add"
},
"cost": 0
}
],
"total": 9,
"max_total_cost": 10000,
"total_cost": 0
}
As I can see it tells me “webhook_callback_verification_pending”. Now I am wondering, is this a process that needs to be manually reviewed and if everything is fine I get unlocked? How long does that take and how do I see if it has been verified? I couldn’t find anything specific online about it so I asked ChatGPT and it responded with:
- Handle Verification Challenge: When you register the webhook, Twitch sends a verification challenge to your endpoint to confirm that it can receive and handle events. You’ve already implemented this part in your
/webhooks/custom_reward_redemption/add
endpoint. It verifies the challenge and responds accordingly.
This is my endpoint, do I need to do anything else?:
// Endpoint to handle initial redemption events
app.post('/webhooks/custom_reward_redemption/add', function(req, res) {
// Verify the request is coming from Twitch by checking the 'Twitch-Eventsub-Message-Type' header
if (req.headers['twitch-eventsub-message-type'] === 'webhook_callback_verification') {
// Respond to the verification challenge
console.log("Verification Challenge:", req.body.challenge);
res.status(200).send(req.body.challenge);
} else {
// Handle the initial redemption event here
// Extract necessary data from the request body
var eventData = req.body.event;
var userId = eventData.user_id;
var userName = eventData.user_name;
// Log the redemption event
console.log("Redemption Event ID:", redemptionId);
console.log("User ID:", userId);
console.log("User Name:", userName);
// Perform actions based on the redemption event
// For example, you can update your database, trigger notifications, etc.
// Respond with a 200 OK status code
res.sendStatus(200);
}
});
Also I noticed something different. When I stop my nodejs server and restart it, the webhook also stopps, then I need to do /auth/twitch again and can register a new webhook with /register-webhook.
However when I not restart it and try /register-webhook again it tells me “subscription already exists”.
Does that mean, that everytime I restart my server, I need to reregister the webhook which then requires me to wait everytime until the webhook is verified?
And my last question is, currently the express node server is running on port 2083 (cloudflare). However the callback requires https / 443:
(callback: ‘https://www.censored.fun/webhooks/custom_reward_redemption/add’,)
Would my webhook even work and receive those events as it runs on 2083 currently?
Not sure if I understood everything correct, just trying my best to see how it works.