Getting user and touser parameters into a customapi


#1

Hello there folks!

I’ve been starting to mess around with customapi’s to expand my command repertoire,
in this particular case I am trying to expand the amount of randomly selected answers I can parse from a command.

Take a “hug” command for instance, with several responses:

$(eval const responses = ['$(user) has decided to give $(query) a big hug!', '$(user) just slipped their arms around $(query).', '$(query) just got tackled and cuddled by $(user)!', 'I believe that $(user) likes $(query) very much. Hugs!', 'Free hugs for $(query)!', 'Dawwww, $(user) and $(query) are adorable.']; if(!$(query)){Please tell us who you’d like to share the love with! (!hug username)}else{responses[Math.floor(Math.random() * responses.length)];})

Now this allows me to have multiple responses to the command being used, and is a lot of fun for the viewers of the channels I moderate. Sadly using this method, you rapidly run into the 400 caracter limit for nightbot commands.

So little old me decided 'Why not use a $(customapi) and host a text file and a php file online, and just have the php code call up a random line from the text file?!

It’d look something like this:

<?php
    $f_contents = file("hug.txt"); 
    $line = $f_contents[rand(0, count($f_contents) - 1)];
    print_r($line)
?>

Of course, I ran into the obvious issue here that $(user) and $(query) are simply returned as such, in text form. Instead of being used as parameters. And that makes perfect sense to me.

But I can not for the love of God find how to accomplish this!

I know it can be done, as demonstrated by the “!followage” command:

$(touser) Has Been Following $(channel) on Twitch since $(customapi http://api.newtimenow.com/follow-length/?channel=$(channel)&user=$(touser))

where both $(channel) and $(touser) are parameters that are used in the customapi http://api.newtimenow.com/follow-length/

But what I can not grasp is how to transcribe these variables into code that could use them.
Does this mean that $(channel) and $(touser) are put into two variables (channel and user) ?
Would I be able to simply use those two variables without declaring them?
Do I need to have a line in my .php to call these variables?
Esentially, how can I incorporate these things into my custom api.

If this has already been answered somewhere, I’m sorry for wasting your time, but I could simply not find it.
If it hasn’t, I’d be more than delighted to know some more about this.

Thank you for your time reading this.


#2

Aaaaaaand I answered my own question by looking some more into php.
Feel free to close this post, I’ll leave it around in case anyone else has the question.

Basically there is the command “$_GET[‘var’]” that lets you get those variables you add after the link.

I ended up with this to edit the response I pull out of my text file:

<?php
    $user = $_GET['user'];
    $query = $_GET['query'];

    $search  = array('$(user)', '$(query)');
    $replace = array($user, $query);

if(!empty($query)){

    $f_contents = file("hugs.txt"); 
    $line = $f_contents[rand(0, count($f_contents) - 1)];
    $output = str_replace($search,$replace,$line);

}

else $output = "Please tell us who you'd like to share the love with! (!hug username)";

    print_r($output)

?>		

With the command looking something like this :
$(customapi http://yourwebsitehere.com/hug.php/?user=$(user)&query=$(query))


#3

You should consider using the querystring variable to encode the query so that it is properly supplied to your API even when it contains special characters like &.

Example:

$(customapi http://yourwebsitehere.com/hug.php/?user=$(querystring $(user))&query=$(querystring))

#4

I didn’t even know nightbot could do that! Amazing!

Thanks a lot for chiming in, this’ll help a lot for some other commands I have in mind!