summaryrefslogtreecommitdiff
path: root/crystal
diff options
context:
space:
mode:
Diffstat (limited to 'crystal')
-rw-r--r--crystal/pubsub.cr109
1 files changed, 109 insertions, 0 deletions
diff --git a/crystal/pubsub.cr b/crystal/pubsub.cr
new file mode 100644
index 0000000..e1644ca
--- /dev/null
+++ b/crystal/pubsub.cr
@@ -0,0 +1,109 @@
+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
+
+macro get_winsz()
+ # Crystal doesn't support the TIOCGWINSZ ioctl
+ `stty size`.split[1].to_i
+end
+
+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 pubsub_listen ( settings : Hash, topic : String, channel_id : String = settings["channel_id"])
+ return( {
+ "type" => "LISTEN",
+ "nonce" => "listen-" + topic,
+ "data" => {
+ "auth_token" => settings["access_token"],
+ "topics" => [
+ topic + "." + channel_id
+ ]
+ }
+ }.to_json.to_s )
+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"} )
+
+pubsub.on_message do | message |
+ print( "RECEIVED: ")
+ json = JSON.parse( message )
+ print( json.to_pretty_json )
+ print( "\n")
+ case json["type"].not_nil!
+ when "RESPONSE"
+ when "PONG"
+ when "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
+ when "channel-subscribe-events-v1." + settings["channel_id"]
+ case msg["context"].not_nil!
+ when "subgift"
+ when "resub"
+ end
+# when "channel-commerce-events-v1." + settings["channel_id"] # "deprecated" which means "completely unsupported" because Twitch
+ when "channel-bits-badge-unlocks." + settings["channel_id"]
+ when "channel-points-channel-v1" + settings["channel_id"] # new scope; fix later
+ when "chat_moderator_actions." + settings["channel_id"]
+ when "whispers." + settings["channel_id"]
+ end
+ end
+end
+
+[
+ "channel-bits-events-v1",
+ "channel-subscribe-events-v1",
+ "channel-commerce-events-v1", # "deprecated", which means "completely unsupported" because Twitch
+ "channel-bits-badge-unlocks",
+ "channel-points-channel-v1", # requires new scope, fix later
+ "chat_moderator_actions",
+ "whispers",
+].each do |topic|
+ pubsub_send( settings, pubsub, pubsub_listen( settings, topic ) )
+end
+
+spawn do
+ loop do
+ pubsub_send( settings, pubsub, msg_ping )
+ sleep 240
+ end
+end
+
+pubsub.run
+
+begin
+
+rescue
+ puts "exception"
+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\"}"
+# }
+#}
+#