Hello, trying to make my first application using twitch webhook. Made sure that my application gives 202, then I got confused. I understand that this is not the final answer with the data, you need to send something else, here the question arises, what exactly and how to organize it? Please tell me how to implement it. Thanks.
You canât use http://localhost as a callback, you need to use an address that is internet accessible and has a web server running that will listen for the GET and POST messages sent by Twitch.
Once you send the subscription request to Twitch, they will then send a GET request to your callback (and since youâre using localhost, youâll never receive it as thatâs not reachable by Twitch) at which point you need a web server running to listen for that request and respond with the hub.challenge contained in the request.
After you respond to that challenge the webhook subscription should be created, and you will be sent POST requests to your callback which contain the notifications about the topic you requested.
and I donât know how to respond to that message. Made an echo, but it doesnât work. All actions, subscriptions, the beginning of the stream, do not work. What am I doing wrong?
You have subscribed to the follows topic for a user following someone, this will only send you events when $target_user_id follows someone else on Twitch (which is back to front for overlay/chat announcments for new followers to a channel).
It wonât send you subscriptions (there is currently no topic for that) and it wonât send you stream changes.
For Stream Change Subscribe to https://api.twitch.tv/helix/streams?user_id=<USERID> For new follows to a streamer Subscribe to https://api.twitch.tv/helix/users/follows?first=1&to_id=<USERID> For new follows a user makes Subscribe to https://api.twitch.tv/helix/users/follows?first=1&from_id=<USERID>
You probably donây need the last topic, but that is what you have subscribed to
You donât need and should never enable this on production.
You also donât need the access control header in this context.
Additionally you can use the following:
To check the status and existence of any active Webhooks.
GOTCHA As this is a webhook following the spec, data is not delivered via $_POST but via php://input as per
Code Tidy/corrections
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
file_put_contents(__DIR__ . "/payload.txt", print_r(file_get_contents('php://input'), true) . "\n", FILE_APPEND);
$incoming_payload = json_decode(file_get_contents('php://input'));
if (json_last_error() == JSON_ERROR_NONE) {
// all good do stuff
header('HTTP/1.1 200 OK');
echo 'OK';
exit;
}
// someone posted but no data to read?
header('HTTP/1.1 404 Not Found');
exit;
}
// must be a GET (should not be PUT/etc
file_put_contents(__DIR__ . "/getRequest.txt", print_r($_GET, true) . "\n", FILE_APPEND);
if (isset($_GET['hub_challenge'])) {
$challenge = $_GET['hub_challenge'];
header('HTTP/1.1 200 OK');
echo $challenge;
exit;
}
header('HTTP/1.1 404 Not Found');
exit;
Finally if ($_SERVER['REQUEST_METHOD'] === 'POST') { is fine. I usually prefer if ($_POST) but thatâs a preference thing really
Thank you very much, everything works. I had a few more questions:
So for every event, I have to send new requests? For follower 'hub.topic' = 'https://api.twitch.tv/helix/users/follows?first=1&to_id=******** http://mysite.com/twitch.webhookfollow.php
To notify the streamer? https://api.twitch.tv/helix/streams?user_id=******** http://mysite.com/twitch.webhookstream.php
Or perhaps somehow specify the query to receive all events?
The connection will operate 10 days, after which it will stop working right? So you have to put the update on cron, or is the connection not lost?
https://api.twitch.tv/helix/users/follows?first=1&to_id=******** we receive information about all new followers, and is it possible to also receive notifications about all unfollowers? Thanks for your help.
The Topic you specified will send you all new followers as they occur for that to_id as long as the webhook is active/within lease time. And you continue to maintain the listener.
Correct, you set the lease time to whatever you want, the maximum is 10 days and you need to manually recreate the link. You will need to space out your reconnections to stay within the posted rate limits
No you cannot receive notifications about unfollows. This leads to harassment of the person unfollowing and as such is excluded as a notification.
to receive notification of the beginning of the stream.
And therefore the files take answers. twitch.webhook.follow.php and twitch.webhook.stream.php with the code you specified above. Is it possible to somehow make a request for receiving and notifications about new followers and the beginning of the stream in one request?
That is, specify something like âhub.topicâ = all and receive all available notifications and how to track them in twitch.webhook.php is Allowed not to create a large number of requests and files that will accept responses.
And there was another question. Configured notifications about the stream, it comes more than once. For 3 hours stream can come more than 4 notifications, this is a bug of the system? Or did I do something wrong Or I made a mistake in the code, although I did not see such an error in the notification of followers?