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

Using input from the outside world.

John Rivs
John Rivs
Mar 20, 2017 · 3 min read

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:

  • which has a value of
  • which has a value of

We can access those values in our code:

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 anywhere, so where does it come from?

We have to go deeper

Not too much though.

Just like the 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:

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

Back to the insult

Starring the person that wants to lose friends.

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

Obviously, something’s missing. If you leave it like that, I’m gonna be the one insulting all the time whenever someone says because and 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 and . 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 .

When the user types , Nightbot will make the call to .

Image for post
Image for post

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store