Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Nightbot custom api 101: In writing a custom api for Nightbot, you have to know about the limitations. The big limitation is that nightbot custom api can only handle raw text output on a website. This means that everything needs to be processed server side and outputted as raw text. In this example, we have a PHP script read lines from "quotes.txt" and output a single line to the URL we feed nightbot custom api ("http:://HOST.com/nightbot/CHANNEL/quotes.php").
- To add this api to your nightbot, add the command;
- !addcom !quote !(customapi <URL>)
- All the command does is output the raw text contents of the website to nightbot.
- NB: Nightbot will not handle HTML, so if the text-output contains HTML code, nightbot will only output <html>
- Disclaimer: This code is used as a Nightbot custom API to output a random quote from the text-file "quotes.txt".
- The text-file MUST be formatted so that quotes are separated by lines
- Ex. textfile(quotes.txt):
- "Do NOT let the Viking near the redstone" - Everyone, 2015
- "Keep you meat away from me!" - Jynx, 2015
- "This code can handle an empty line in the text file as well!" - Wacky, when writing the pastebin!
- <?php
- // This array stores all the quotes.
- $quotes = array();
- // Opens a file for reading
- $file = fopen("quotes.txt", "r");
- // Loops through file line for line
- while(! feof($file)){
- // Put current line into a temporary variable
- $str = fgets($file);
- // Check to see if the current line is empty
- if($str != "") {
- // If the line is not empty, add it to the quotes-array.
- $quotes[] = $str;
- }
- }
- // Pick a random number(index) in the quotes-array
- $r = rand(0, count($quotes)-1);
- // Increment number of index, to make it human non-programmer understandable which quote we are seeing.
- $current = $r + 1;
- // Output raw text-data to the custom-api URL, with the quote, and a number(index + 1) so people know which quote(in
- // number) are being outputtet.
- echo 'Quote(' . $current . '):' . $quotes[$r];
- // Close the "quotes.txt" file stream. ALWAYS close the file stream. ALWAYS.
- fclose($file);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement