summaryrefslogtreecommitdiff
path: root/crystal/pubsub.cr
blob: e1644ca5d7dd3c905bdc0d1278e6c9629b02aad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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\"}"
#  }
#}
#