Hi I am trying to get the returned object from the webhook to get verified and listen to the events.
I am getting pending status and I see on ngrok the response with the challenge but I never get it back into my server.
Not sure what I am not seeing here!
// to handle response verification.
app.use(express.json({
verify: verifyTwitchSignature
}));
app.post('/createWebhook/:broadcasterId', (req, res) => {
const createWebHookParams = {
host: "api.twitch.tv",
path: "helix/eventsub/subscriptions",
method: 'POST',
headers: {
"Content-Type": "application/json",
"Client-ID": clientId,
"Authorization": "Bearer " + authToken
}
}
const createWebHookBody = {
"type": "channel.follow",
"version": "1",
"condition": {
"broadcaster_user_id": req.params.broadcasterId
},
"transport": {
"method": "webhook",
"callback": "https:ngrokURL/notification",
"secret": webhookSecret //
}
}
let responseData = ""
const webhookReq = https.request(createWebHookParams, (result) => {
result.setEncoding('utf8')
result.on('data', (d) => {
responseData = responseData + d
})
.on('end', (result) => {
const responseBody = JSON.parse(responseData)
console.log(responseBody)
res.send(responseBody)
})
})
webhookReq.on('error', (e) => {
console.log("Error")
})
webhookReq.write(JSON.stringify(createWebHookBody))
webhookReq.end()
});
app.post('/notification', (req, res) => {
console.log("I raaaaaannnnnnn" ,req)
const messageType = req.header("Twitch-Eventsub-Message-Type");
if (messageType === 'webhook_callback_verification') {
console.log("Verifying Webhook..");
return res.status(200).send(req.body.challenge);
}
if (messageType === "revocation") {
const {
subscription
} = req.body;
console.log(`${subscription.type} subscription ${subscription.id} has been revoked`, subscription)
return res.status(200).end();
};
console.log("I am the body requestttt", req.body)
// const {
// type
// } = req.body.subscription;
const {
event
} = req.body;
if (messageType === "notification") {
try {
console.log(event);
} catch (error) {
console.log(`An error accured sending the online notificatin for ${event.broadcaster_user_name}: `, error)
}
}
res.status(200).end();
})
app.listen(PORT, () => {
console.log(`listening at http://localhost:${PORT}`)
})