summaryrefslogtreecommitdiff
path: root/crystal/pubsub.cr
diff options
context:
space:
mode:
authorJoe Rayhawk <jrayhawk+git@omgwallhack.org>2022-03-14 08:14:08 -0700
committerJoe Rayhawk <jrayhawk+git@omgwallhack.org>2022-03-14 08:14:08 -0700
commitcfcabdf515e1fa60c5b2bfe41c636439b6494ab3 (patch)
treee9d4649235cf4972021466e1e267c9b820df6508 /crystal/pubsub.cr
parentf0195cf97aab6b1d28ac76c39c7c4ce63da78ae9 (diff)
downloadtwitchtools-cfcabdf515e1fa60c5b2bfe41c636439b6494ab3.tar.gz
twitchtools-cfcabdf515e1fa60c5b2bfe41c636439b6494ab3.zip
crystal/pubsub.cr: make more robust
Diffstat (limited to 'crystal/pubsub.cr')
-rw-r--r--crystal/pubsub.cr82
1 files changed, 69 insertions, 13 deletions
diff --git a/crystal/pubsub.cr b/crystal/pubsub.cr
index e1644ca..fd344f2 100644
--- a/crystal/pubsub.cr
+++ b/crystal/pubsub.cr
@@ -10,6 +10,25 @@ settings["client_id"] = File.read( settings["home"] + "/.config/twitch/client
settings["channel"] = File.read( settings["home"] + "/.config/twitch/channel" ).chomp
settings["channel_id"] = File.read( settings["home"] + "/.config/twitch/channel_id" ).chomp
+struct Nil
+ def to_s?
+ self
+ end
+end
+
+struct Nil
+ def as_s?
+ self
+ end
+end
+
+struct Nil
+ def as_i?
+ self
+ end
+end
+
+
macro get_winsz()
# Crystal doesn't support the TIOCGWINSZ ioctl
`stty size`.split[1].to_i
@@ -33,12 +52,31 @@ def pubsub_listen ( settings : Hash, topic : String, channel_id : String = setti
}.to_json.to_s )
end
+#begin
+
+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 + " " + "pubsub: " + 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
-#begin
-
pubsub = HTTP::WebSocket.new( URI.parse( "wss://pubsub-edge.twitch.tv" ), HTTP::Headers{"Cookie" => "SESSIONID=1234"} )
@@ -54,14 +92,22 @@ pubsub.on_message do | message |
msg = JSON.parse( json["data"]["message"].not_nil!.to_s )
case json["data"]["topic"].not_nil!
when "channel-bits-events-v1." + settings["channel_id"]
- sock = Socket.unix
- sock.connect Socket::UNIXAddress.new( settings["home"] + "/.irssi/twitch-socket".to_s )
- sock.puts( "#" + settings["channel"] + " | " + msg["user_name"].to_s + " sends " + msg["bits_used"].to_s + "bits: " + msg["chat_message"].to_s + "\n" )
- sock.close
+ bits_username = ( msg["data"]["user_name"]?.as_s? || "unknown" )
+ bits_used = ( msg["data"]["bits_used"]?.as_i?.to_s || "a quantity" )
+ bits_message = ( msg["data"]["chat_message"]?.as_s? || "" )
+ ircmsg( settings["home"], settings["channel"], "| " + bits_username.to_s + " sends " + bits_used.to_s + " bits: " + bits_message.to_s )
when "channel-subscribe-events-v1." + settings["channel_id"]
- case msg["context"].not_nil!
- when "subgift"
- when "resub"
+ sub_sender = ( msg["user_name"]?.as_s? || "anonymous" )
+ sub_recver = ( msg["recipient_user_name"]?.as_s? || sub_sender )
+ sub_plan = ( msg["sub_plan"]?.as_s? || "unknown" )
+ sub_months = ( msg["cumulative_months"]?.as_i? || 0 )
+ sub_msg = ( msg["sub_message"]["message"]?.as_s? || "" )
+ effectsmsg( "overlay gltext 10 " + sub_recver + " subscribed!" )
+ pp msg["is_gift"]
+ if ( msg["is_gift"]? )
+ ircmsg( settings["home"], settings["channel"], sub_recver.to_s + " has subscribed for " + sub_months.to_s + " months at sub level #" + sub_plan.to_s + ": " + sub_msg.to_s )
+ else
+ ircmsg( settings["home"], settings["channel"], sub_recver.to_s + " has subscribed for " + sub_months.to_s + " months at sub level #" + sub_plan.to_s + "thanks to a gift from " + sub_sender.to_s + ": " + sub_msg.to_s )
end
# when "channel-commerce-events-v1." + settings["channel_id"] # "deprecated" which means "completely unsupported" because Twitch
when "channel-bits-badge-unlocks." + settings["channel_id"]
@@ -91,12 +137,12 @@ spawn do
end
end
-pubsub.run
begin
-
-rescue
- puts "exception"
+ pubsub.run
+rescue ex
+ ircmsg( settings["home"], settings["channel"], "pubsub.cr: " + ex.to_s.gsub(/\r|\n/, ' ') )
+ puts ex
end
#RECEIVED: {
@@ -107,3 +153,13 @@ end
# }
#}
#
+
+#RECEIVED: {
+# "type": "MESSAGE",
+# "data": {
+# "topic": "channel-bits-events-v1.59895482",
+# "message": "{
+#\"data\":{\"user_name\":\"vchen1996\",\"channel_name\":\"bungmonkey\",\"user_id\":\"509868211\",\"channel_id\":\"59895482\",\"time\":\"2021-11-11T08:46:03.117271456Z\",\"chat_message\":\"Cheer100 how does one tickle the streamer or the teammate, please advise.\",\"bits_used\":100,\"total_bits_used\":200,\"context\":\"cheer\",\"badge_entitlement\":null,\"badge_tier_entitlement\":{\"Badge\":{\"new_version\":0,\"previous_version\":0},\"Emoticons\":null}},\"version\":\"1.0\",\"message_type\":\"bits_event\",\"message_id\":\"ad0da33a-54f4-5353-b806-6fcdcdc6eb98\"}"
+# }
+#}
+