How to build a Twitch bot for free with 0 setup. (Part 3)

Using input from the outside world.

In part 2, I explained a bit more of the PHP syntax and how to build a quote command. Now you’ll be able to write a command that accepts input from chat.

We’re gonna build an insult command, which allows a user to direct an insult to another user.

Communication is key

Your bot is learning how to talk.

The way chat and the bot can communicate is via the URL. I’m sure you’ve seen URLs that use a question mark and ampersands:

The thing after the question mark is called the query string. It contains key-value pairs separated by ampersands. If you think about it, it’s pretty much like arrays. In that URL, there are 2 pairs:

  • v which has a value of Tss5ztW4E3M
  • t which has a value of 1s

We can access those values in our code:

echo $_GET['v']; // this would output Tss5ztW4E3M

Some of that should look familiar (the brackets and the dollar sign) and some of it shouldn’t (_GET). So far we can deduce it must be a variable and the value it holds must be an array. But we didn’t define $_GET anywhere, so where does it come from?

We have to go deeper

Not too much though.

Just like thearray_rand() function was provided to us by PHP out of the box, this variable was too. We call this variable a super global. This array holds values from the query string.

“Don’t you have to put an integer between the brackets to retrieve a value from the array?” Yes, that’s what you do when the value doesn’t have any name. Arrays, like in the query string, can hold key-value pairs. We call them associative arrays. To the left of => you have the key and the value on the right:

$person = [
'name' => 'John',
'age' => 23,
'job' => 'Web developer',
'hobbies' => ['guitar', 'running', 'videogames']
echo $person['hobbies'][2]; // this would output videogames

So for the youtube URL from earlier, you can picture $_GET in your head like:

$_GET = [
'v' => 'Tss5ztW4E3M',
't' => '1s'
echo $_GET['t']; // this would output 1s

Back to the insult

Starring the person that wants to lose friends.

Some of you have probably figured out where things are going:

echo $_GET['to'] . ', ' . $_GET['from'] . ' says you are a doodoo head';

Obviously, something’s missing. If you leave it like that, I’m gonna be the one insulting loremIpsum all the time whenever someone says !insult user134260394 because johnrivs and loremIpsum are hard-coded in the URL. We need to make this dynamic.

Nightbot offers a few variables we can use when adding commands. The ones we need are called $(user) and $(touser). Nightbot will substitute them for the name of the person that triggered the command and the name right next to the command, respectively. So go back to your chat and !addcom !insult $(customapi$(user)&to=$(touser)).

When the user johnrivs types !insult loremIpsum, Nightbot will make the call to

Hope that wasn’t too hard. Some other day I will add more parts to this tutorial.

PART 1 | PART 2 | PART 3

Hey there! I’m John Rivs, web developer. After developing a Twitch bot for a while, I’ve decided to explain what’s the first step. My bot looks very differently today from what I show in this tutorial, but it explains almost exactly how my bot started.

Any questions? Leave a comment or hit me up on Twitter.