Advertisement
thewackyviking

Nightbot Quotes Custom API

Jul 21st, 2015
1,981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. 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").
  2.  
  3. To add this api to your nightbot, add the command;
  4. !addcom !quote !(customapi <URL>)
  5.  
  6. All the command does is output the raw text contents of the website to nightbot.
  7. NB: Nightbot will not handle HTML, so if the text-output contains HTML code, nightbot will only output <html>
  8.  
  9. Disclaimer: This code is used as a Nightbot custom API to output a random quote from the text-file "quotes.txt".
  10. The text-file MUST be formatted so that quotes are separated by lines
  11.  
  12. Ex. textfile(quotes.txt):
  13. "Do NOT let the Viking near the redstone" - Everyone, 2015
  14. "Keep you meat away from me!" - Jynx, 2015
  15.  
  16. "This code can handle an empty line in the text file as well!" - Wacky, when writing the pastebin!
  17.  
  18. <?php
  19.         // This array stores all the quotes.
  20.         $quotes = array();
  21.    
  22.     // Opens a file for reading
  23.         $file = fopen("quotes.txt", "r");
  24.    
  25.     // Loops through file line for line
  26.         while(! feof($file)){
  27.         // Put current line into a temporary variable
  28.                 $str = fgets($file);
  29.         // Check to see if the current line is empty
  30.         if($str != "") {
  31.             // If the line is not empty, add it to the quotes-array.
  32.                     $quotes[] = $str;
  33.         }
  34.         }
  35.  
  36.     // Pick a random number(index) in the quotes-array
  37.         $r = rand(0, count($quotes)-1);
  38.     // Increment number of index, to make it human non-programmer understandable which quote we are seeing.
  39.         $current = $r + 1;
  40.  
  41.     // Output raw text-data to the custom-api URL, with the quote, and a number(index + 1) so people know which quote(in
  42.         // number) are being outputtet.
  43.         echo 'Quote(' . $current . '):' . $quotes[$r];
  44.  
  45.  
  46.     // Close the "quotes.txt" file stream. ALWAYS close the file stream. ALWAYS.
  47.         fclose($file);
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement