Last active
August 16, 2025 15:56
-
-
Save openmailbox/98bdc9701e2741f4cb3b0b47f3d2c429 to your computer and use it in GitHub Desktop.
A quick example of a Twitch chat bot using Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A quick example of a Twitch chat bot in Ruby. No third party libraries. Just Ruby standard lib. | |
| # | |
| # This software is made available under the MIT License. | |
| # Copyright (C) 2025 Brandon Rice | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in all | |
| # copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| # You can fill in creds here or use environment variables if you choose. | |
| TWITCH_CHAT_TOKEN = ENV['TWITCH_CHAT_TOKEN'] | |
| TWITCH_USER = ENV['TWITCH_USER'] | |
| require 'socket' | |
| require 'logger' | |
| Thread.abort_on_exception = true | |
| class Twitch | |
| attr_reader :logger, :running, :socket | |
| def initialize(logger = nil) | |
| @logger = logger || Logger.new(STDOUT) | |
| @running = false | |
| @socket = nil | |
| end | |
| def send(message) | |
| logger.info "< #{message}" | |
| socket.puts(message) | |
| end | |
| def run | |
| logger.info 'Preparing to connect...' | |
| @socket = TCPSocket.new('irc.chat.twitch.tv', 6667) | |
| @running = true | |
| socket.puts("PASS #{TWITCH_CHAT_TOKEN}") | |
| socket.puts("NICK #{TWITCH_USER}") | |
| logger.info 'Connected...' | |
| Thread.start do | |
| while (running) do | |
| ready = IO.select([socket]) | |
| ready[0].each do |s| | |
| line = s.gets | |
| match = line.match(/^:(.+)!(.+) PRIVMSG #(\w+) :(.+)$/) | |
| message = match && match[4] | |
| if message =~ /^!hello/ | |
| user = match[1] | |
| logger.info "USER COMMAND: #{user} - !hello" | |
| send "PRIVMSG #open_mailbox :Hello, #{user} from Mailbot!" | |
| end | |
| logger.info "> #{line}" | |
| end | |
| end | |
| end | |
| end | |
| def stop | |
| @running = false | |
| end | |
| end | |
| if TWITCH_CHAT_TOKEN.empty || TWITCH_USER.empty? | |
| puts "You need to fill in your own Twitch credentials!" | |
| exit(1) | |
| end | |
| bot = Twitch.new | |
| bot.run | |
| while (bot.running) do | |
| command = gets.chomp | |
| if command == 'quit' | |
| bot.stop | |
| else | |
| bot.send(command) | |
| end | |
| end | |
| puts 'Exited.' |
🌊!
Your code is amazing. Please put a license on this :>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/nevern02/98bdc9701e2741f4cb3b0b47f3d2c429#file-twitch_bot-rb-L67 Hey in line 67, don't forget the question mark on empty.
Thanks for posting this, its super helpful. So after you put the username and token it will output to their channel?