require "http/web_socket" require "json" require "pretty_print" settings = Hash(String, String).new settings["home"] = Path.home.to_s settings["access_token"] = File.read( settings["home"] + "/.config/twitch/access_token" ).chomp settings["client_id"] = File.read( settings["home"] + "/.config/twitch/client_id" ).chomp settings["channel"] = File.read( settings["home"] + "/.config/twitch/channel" ).chomp settings["channel_id"] = File.read( settings["home"] + "/.config/twitch/channel_id" ).chomp def pubsub_send ( settings, pubsub : HTTP::WebSocket, msg ) pubsub.send( msg ) puts( ("SENT : " + msg).gsub(/#{settings["access_token"]}/,"NOPE").gsub(/#{settings["client_id"]}/, "NOPE") ) end def ircmsg( home : String, channel : String, msg : String) if msg !~ /(\r|\n)/ sock = Socket.unix # sock.connect Socket::UNIXAddress.new( home + "/.irssi/twitch-socket".to_s, type: Socket::Type::DGRAM ) sock.connect Socket::UNIXAddress.new( home + "/.irssi/twitch-socket".to_s ) sock.puts( "#" + channel + " " + msg ) sock.close end end def effectsmsg( msg : String ) sock = Socket.unix sock.connect Socket::UNIXAddress.new( "/etc/twitch/effects-socket" ) sock.puts( msg ) sock.close rescue ex STDERR.puts( ex ) end msg_ping = { "type" => "PING" }.to_json.to_s struct Nil def as_s? self end end spawn do loop do begin obs_pubsub = HTTP::WebSocket.new( URI.parse( "ws://127.0.0.1:4444/" ), HTTP::Headers{"Cookie" => "SESSIONID=1234"} ) obs_pubsub.on_message do | message | print( "RECEIVED: ") json = JSON.parse( message ) print( json.to_pretty_json ) print( "\n") case json["update-type"]? when "SwitchScenes" ircmsg( settings["home"], settings["channel"], "| obs: switched scene to " + ( json["scene-name"]?.as_s? || "unknown" ) ) end end obs_pubsub.run rescue ex ircmsg( settings["home"], settings["channel"], "obs.cr: " + ex.to_s.gsub(/\r|\n/, ' ') ) puts ex obs_pubsub && obs_pubsub.close end sleep 10 next end end loop do sleep 10 end #RECEIVED: { # "type": "MESSAGE", # "data": { # "topic": "channel-subscribe-events-v1.59895482", # "message": "{\"benefit_end_month\":0,\"user_name\":\"orangesherbet\",\"display_name\":\"orangesherbet\",\"channel_name\":\"bungmonkey\",\"user_id\":\"36184711\",\"channel_id\":\"59895482\",\"time\":\"2020-05-06T06:55:31.362125005Z\",\"sub_message\":{\"message\":\"seqDag bungmoButt bungmoFart\",\"emotes\":[{\"start\":0,\"end\":5,\"id\":\"496082\"},{\"start\":7,\"end\":16,\"id\":\"300780134\"},{\"start\":18,\"end\":27,\"id\":\"301501910\"}]},\"sub_plan\":\"1000\",\"sub_plan_name\":\"5\",\"months\":0,\"cumulative_months\":21,\"context\":\"resub\"}" # } #} #