Parsing Twitch !command chat messages

Using Twitch with Touch Portal

This guide will show you how to parse a Twitch !command and how you can retrieve the usefull components and use them in Touch Portal.

Step 01: Monitoring Chat Message

We start by adding the event to listen for all Twitch chat messages.

As we want only the messages that start with !command we use this as the criteria for the event. Of course the !command is just an example and could be changed to whatever command indicator you like to use.



Step 02: Simple command structure

In this step we assume the command has this structure !command user. In step 1 we captured the !command and now we want the user part of the message. For this we are going to use the Calculate with Value action:

First you need to create a new Value in which you will temporary store the result of our manipulation. In this case we created a Value called Value 1.

This action stores the result of the calculation set, which in this case is the message ( '${twitch-chat0}' is the code holding the last send Twitch Message) from Twitch and then subtracts the text !command from it.

Touch Portal keeps the last 5 send messages in memory. You can retrieve these messages in your actions by clicking on the '+' button and going to the Twitch section. Whenever a new message is received from Twitch these last 5 Twitch messages will all change. Message zero is the last recieved message, message one is the one received before that and so on.

From this moment the Value 1 will hold the full message with the command part removed. In our case this will be just the user component. We can now use the Value 1 in our other actions to work with. A simple example:

In this simple example we just reply in the chat with a thank you to the user that was specified in the !command chat message. So !command John would result in a message send to Twitch like Thank you John!

Please note that the use of Values requires the Pro Upgrade. Get the Pro upgrade to use Values and also benefit from all other Pro Upgrade only features such as maximum of 110 buttons per page, unlimited pages, plug-ins support, Global Events and much more. Check the link for more information.


Step 03: Complex command structure

In this step we are going to take it to the next level. Sometimes a simple command structure will just not cut it and you want more. In this part we are going to use the powerfull Replace in Value action.

The Replace in Value action is able to use regex to replace parts of the given Value. We are going to use this to store the arguments into a value and then strip everything except the n-th argument. The result is that we can use the n-th argument from the Twitch chat message. Lets work with an example like this: !shoutout John Doe.

We want to do a formal shoutout which means that we will do a shout out to the give persons surname. In this case we want to send to the Twitch chat Follow Mr. Doe!.

Our approach with the simple structure will not work anymore as we have some unknowns which we cannot remove beforehand. This is where we are going to use the regex functionality of the Replace in Value action to remove everything except the n-th word in that Value. So in our case we want the 3rd word, which is Doe.

The regex works with a first-item-is-zero-index so this means to get the 4th word it means that you have to use 3 as it is the 4th index in the range 0, 1, 2, 3, 4, 5 etc.

See the action flow in the image below to select the 3rd word. If you need a different word for your own personal situation then just change the 2 into the number you require.

Regex is a powerful system but also a very complex one. If you want to dive deeper in the world of regex we suggest to search for some good resources online to learn more about this. Teaching about regex is out of the scope of this tutorial.

Just so you do not have to type it from the image here the regex:

Regex:
^([^ ]+\ ){2}([^ ]+).*

End Result

You are now able to parse commands send to you from Twitch and use simple and complex structures and use them to enhance the experience for your users.


Back to Guide Overview