As a tie-in to my DIY Stream Deck, I wrote a Python script for OBS Studio that allows you to send messages to Twitch chat using OBS’s built-in hotkey system.

Scripting

The 21.0 update for OBS added support for Python and Lua scripting, using a new scripting console and an extension of the C API. In the words of the developers, scripting allows you to “quickly extend, add features, or automate the program without needing to build a native plugin module.”

Since my non-embedded C++ still needs some work, I decided to try writing this program as a Python script rather than a C++ plugin.

Setting Properties

Via the scripts window, the chat script has access to Qt GUI elements in OBS such as text boxes, lists, and buttons. The user can fill in these fields to customize the script without having to modify any configuration files. With the initial release of the script, these configuration options include:

  • Channel: the channel on Twitch to post messages in
  • User: the username of the user posting messages
  • Oauth: the access key required for authentication to Twitch servers
  • Messages: the custom list of messages that can be sent

These settings are stored with the rest of your OBS settings and are reloaded on startup. There are also two buttons on the bottom of the panel for testing authentication and test sending the first message in the list.

(Note: I wanted the test message button to use the user-selected message in the list, but that property doesn’t appear to be exposed by the API, at least not yet.)

Hotkey Hooking

Each message added to the message list gets its own custom hotkey that can be modified in the OBS settings window. These work like any other hotkey in the program and are saved with their message in the script settings.

The list of messages is extensible, and will add / remove hotkeys as list items are changed. Reordering the message list will also reorder how items appear in the OBS hotkey settings.

Talking to Twitch Chat

Aside from the OBS API functions, the real meat of the script is the code for communicating with Twitch chat itself. Twitch chat is just IRC, so I’m using Python 3’s built-in socket library to handle all of the networking.

After a hotkey has been pressed, the script will open a connection to the Twitch server, authenticate, and then send a message. With version 1.0, this connection is released after a short period to avoid overhead and let repeated messages through without re-authenticating. All messages are rate-limited to the global limit based on the system time, which prevents the user from accidentally receiving a temporary site-wide chat ban for spam.

Any network errors are raised as exceptions if a test button is pressed, but are suppressed if triggered from a hotkey message. This is to alert the user if there is a problem during setup, but otherwise hide issues to avoid interruptions during streaming.

Using the Script

If you’d like to use the script yourself, it’s quite easy. Download the script from GitHub or the OBS resource page and follow the instructions in the README. By far the most difficult part is getting the OBS scripting console to work. You will need to install Python and point OBS to the installation directory. If you load the script and no properties appear, the Python installation / link is not set up correctly.

The script is licensed under GPL v3; it’s free software, so you can use it, modify it, and distribute it so long as you abide by the license.

Although I think it’s obvious, I’ll stress here that this is a 3rd party plugin for OBS. I have no affiliation or endorsement as a developer from either Twitch or OBS.

Conclusion

This was a fun script to write, and it makes me want to see what else I could with OBS scripting / plugins in the future.

I may end up converting this to a C++ plugin at some point, to avoid users having to install and configure Python to use it. But that’s a project for another day.


26 Comments

twitch.tv/Steve_XM · August 14, 2018 at 1:10 pm

I searched everywhere for this functionality. Thank you for making this. I plan on trying it out in the next few days. I will be using the Roccat Power Grid (Free) app on an Android phone. I will now be able to press a single button and post links to my YT and Social medias in the Twitch chat. I’m curious what else you would use it for?

Emilio Gustavo Ormeño · November 5, 2018 at 6:54 am

Hi, Excuse me for my basic question. I cant get the obs scripts running. I’ve installed Python, then I configure the installation path in obs, but they still do not working. Could you give me an idea about what I need or what Im doint wrong?

Another question, is possible from a script, to stop and resume the obs screen capturing?

Thanks

    Dave · November 5, 2018 at 1:09 pm

    Double-check that your Python version is correct for your version of OBS. If you have 64-bit OBS you need 64-bit Python, ditto for 32-bit.

    It should be possible to start/stop recording from a script, although I haven’t messed with the OBS API in several months so I couldn’t tell you how off the top of my head. Depending on what you’re doing you might also want to look into the OBS Sockets plugin.

KedarOthort · July 24, 2019 at 1:39 am

My Python installing path is correct, Python is fully and correct installed on the computer, but the script will not show any properties. They are both 64-bit (I double checked that first). Could the new OBS update be messing with it?

    Dave · July 24, 2019 at 3:14 am

    I’m running the latest version of OBS and I have no issues. Are you sure you linked Python correctly within OBS? Do any of the other OBS Python example scripts work?

      Dave · July 25, 2019 at 3:16 am

      I just did some more testing and it looks like OBS’s Python scripting support doesn’t work properly with Python 3.7+. Try using Python 3.6.

theSlugman · March 26, 2020 at 7:43 pm

Hey,
I’m getting success messages, but my “/marker xxxx” aren’t being captured in twitch. Any ideas? I tried with “test” and that posts on the channel just fine.
https://imgur.com/a/edIbni4

    Dave · March 26, 2020 at 9:28 pm

    Unfortunately I don’t believe markers work through IRC. I just tested and I don’t see markers created by other editors shared via IRC, and attempting to set a marker only returns a notice about the usage:

    NOTICE #partsnotincluded :Usage: "/marker" - Adds a stream marker (with an optional comment, max 140 characters) at the current timestamp. You can use markers in the Highlighter for easier editing.

    I think they only work through the chat window or via the Twitch API.

EpicStreamMan · April 14, 2020 at 11:00 pm

You’ve stated above that Twitch Stream Markers only work via Twitch API and not IRC/Chat, I’m looking for a OBS Script so I can create a Twitch Stream Marker with a Hot-Key so I can use on my DIY Streamdeck or with other macros. Is this possible and do you have any code for that? (I’m new to scripting)

    Dave · April 14, 2020 at 11:05 pm

    It’s definitely possible but I have no code for you. You would have to refer to the Twitch API documentation to figure out how to do it.

lethalprophecy · August 6, 2020 at 2:59 am

I was just using this script and it was functioning perfectly for me with my !clip command

all of a sudden within one week the script no longer functions with OBS
An attempt was made to access a socket in a way forbidden by its access permissions
Using x64 Python 3.6.4 and OBS 25.0.8

    Dave · August 6, 2020 at 1:17 pm

    Sorry to hear that. I just tested and it seems to work fine for me with OBS 25.08 and Python 3.6.8, so it may be an issue with your setup.

    Is this a persistent problem, or does it only happen once in a while? Do you have any firewall or other Internet blocking software installed? Did you make any changes to your computer within that last week?

      lethalprophecy · August 8, 2020 at 12:45 am

      I got it to work by updating to 3.6.8 I remember reading that it required an older version I guess I selected the wrong 3.6
      Thank you it is working!

        Dave · August 9, 2020 at 12:27 am

        How strange. It was developed with 3.6.4 so there should be no issues there. Maybe it was a configuration problem that got sorted by installing the newer version?

        Regardless, I’m glad you got it working!

Nelia · October 5, 2020 at 10:15 am

Hello,

I believe I’m about 90% of the way there with this solution.
Tested with both Python 3.6.8 & 3.6.4
Example of executable: Windows x86-64 executable installer
https://www.python.org/downloads/release/python-368/
OBS is 64 bit, Python says its 64 bit

Added the path in OBS to the python folder
Added the script in obs and no properties are available

I can edit the script for the necessary information, what is holding me not having properties shown.

I appreciate any thoughts or assistance, have a great day! 🙂

    Dave · October 5, 2020 at 10:30 am

    Hi there. The script properties not appearing is a sign that Python is not loaded correctly. My only advice would be to double-check everything, including the software versions’ bit type and the Python install location setting in OBS. Sorry I can’t be more helpful!

Nelia · October 5, 2020 at 11:37 am

The issue was the default installation path of Python.
Uninstalled + created a custom directory – in my case under Program Files (x86)

The script now works. Thank you for your help, have a great day!

    Dave · October 5, 2020 at 11:48 am

    Fantastic, I’m glad you were able to figure it out and thank you for sharing the solution. Enjoy the script!

Jose Campos · October 22, 2020 at 3:27 pm

Hi Dave, I’ve configured the script correctly and it’s working properly but the nickname when I send the message the nickname showed it’s my channel name and not the user I’ve set up in the script configuration. Could you help me please? Thanks!!!

    Dave · October 22, 2020 at 5:35 pm

    Hi Jose. Your nickname in the script config should match the user who requested the OAuth key. If you want to use a different user you should request an OAuth key using that account.

Jack · January 19, 2021 at 11:00 pm

Hi Dave, i wanna ask something…..
Is the script works on Streamlabs OBS ??? or i need to change something

    Dave · January 19, 2021 at 11:50 pm

    As far as I’m aware, StreamLabs OBS does not support user scripts and Qt-based add-ons. It’s for that and many other reasons that I recommend everyone use OBS Studio.

PaulBM · February 3, 2022 at 1:49 pm

I’ve recently set this up to using the OBS hotkeys via OBS Command line triggered from my joystick buttons panel. I found line 208 that registers the hotkeys needs the text position offset added to the key register call to make sure the hotkey internal names are unique.

self.hotkey_id = obs.obs_hotkey_register_frontend(“chat_hotkey_” + str(self.position), key_description, self.callback)

Thanks for this great script and the other fun projects on your site.

    Dave · February 3, 2022 at 4:34 pm

    Hi Paul! Thanks for the feedback. What issues were you seeing before making that change?

PaulBM · February 4, 2022 at 11:12 am

Before adding the following to that line…

+ str(self.position)

I could not send this command via OBSCommand.

/command=TriggerHotkeyByName,hotkeyName=chat_hotkey_0
or
/command=TriggerHotkeyByName,hotkeyName=chat_hotkey_1

etc because all the hot keys had the same internal name of chat_hotkey.

    Dave · February 6, 2022 at 1:43 am

    I see. So the script works fine standalone, but the unique name is necessary for external programs to interact with the created hotkeys?

    Thanks for bringing that to my attention. I’ll make that change ASAP and push a new release.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Would you like to know more?