I am writing a PHP script to make Nightbot (a Twitch chatbot) send multiple messages to the chat when a user triggers a command.
This is the response for the Nightbot command. The $(urlfetch) lets the bot know to make a request to the URL inside. The script allows up to 6 individual messages, separated by backticks.
$(urlfetch http://rokbot.xyz/smm.php?msg=`FIRST_MESSAGE`SECOND_MESSAGE`THIRD_MESSAGE`FOURTH_MESSAGE`FIFTH_MESSAGE`SIXTH_MESSAGE&d=DELAY)
Whenever a user triggers this command, Nightbot makes a request to the following script.
<?php
// Get headers
$heads = getallheaders();
if(isset($heads["Nightbot-Response-Url"])){
if(isset($_GET["msg"])){
// Get whatever messages Nightbot needs to send to Twitch chat via URL param "msg", using a backtick as the delimiter
$msg = array_slice(explode("`", urldecode($_GET["msg"])), 0, 6);
// Get the message delay (how long to make the bot wait between sending messages) via URL param "d"
$d = intval($_GET["d"]);
// Make sure the delay is between 5 to 60 seconds
$delay = (isset($d) && $d>=5 && $d<=60 ? $d : 5);
// Initialize cURL, the URL is the "Nightbot Response Url" contained in the headers
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $heads["Nightbot-Response-Url"]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// Loop through $msg array, running a cURL session to the Nightbot Response Url with each message, making Nightbot send each message to Twitch chat.
for($i = 0; $i < count($msg); $i++){
curl_setopt($ch, CURLOPT_POSTFIELDS, "message=".$msg[$i]);
curl_exec($ch);
sleep($delay);
}
// Job's done
curl_close($ch);
}else{
// What Nightbot displays to Twitch chat if param "msg" is undefined
echo "Enter a series of messages separated by backticks (`)";
}
}else{
// What the page displays if the request is run without the proper header... basically if someone tries to run this in their browser instead of through Nightbot
echo "<h1 align='center'>哎呀! Only Nightbot is allowed beyond this point...</h1>";
}
?>
It mostly works; Nightbot is able to send all of the messages to chat by the use of cURL in the script. However, Nightbot is limited to sending 1 message to chat every 5 seconds, so as a result I have to use sleep() while the script loops through $msg. Therefore the script may take longer than 10 seconds to run. Herein lies the issue: Nightbot will wait 10 seconds for a response, and if it doesn’t get anything, it will output an error message to chat "[Error Connecting to Remote Server]"
So what I am looking for is a way to make the script send something back along the lines of “It’s all good. You don’t have to wait any longer for a response.” while my script keeps running cURLs.
Related section of the Nightbot API documentation on making the bot send messages to chat: https://api-docs.nightbot.tv/#send-channel-message
Related section of the Nightbot docs on how $(urlfetch) works in commands: https://docs.nightbot.tv/commands/variables/urlfetch