From f871d5a8d3cd6ed8535999be1cb7f59e485af81f Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Mon, 19 Nov 2018 07:33:20 -0800 Subject: twitchpubsub.rb: add nonce, add missing space --- twitchpubsub.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/twitchpubsub.rb b/twitchpubsub.rb index 521f73a..c3f3994 100755 --- a/twitchpubsub.rb +++ b/twitchpubsub.rb @@ -13,7 +13,7 @@ confdir = Dir.home + '/.config/twitch/' ENV['APPDATA'] && confdir = ENV['APPDATA'] + "\\twitch\\" ENV['XDG_CONFIG_HOME'] && confdir = ENV['XDG_CONFIG_HOME'] + '/twitch/' -access_token = IO.read( confdir + 'access_token' ).chomp +access_token = IO.read( confdir + 'access_token' ).chomp || true channel = IO.read( confdir + 'channel' ).chomp client_id = IO.read( confdir + 'client_id' ).chomp unix_socket_path = confdir + 'chat_socket' @@ -45,13 +45,15 @@ end def dispatch_message( message, unix_socket_path ) unix_socket = UNIXSocket.new( unix_socket_path ) - if message['sub_plan'] then # Subscription - unix_socket.print( message['user_name'] + ' ' + message['context'] + 'cribed with ' + message['sub_plan'] + ': "' + message['sub_message']['message'] + "\"\n" ) - elsif message['item_description'] then # commerce + if message['context'] == 'subgift' then # Gifted subscription + unix_socket.print( message['recipient_user_name'] + ' has subscribed for ' + (message['months'].to_i+1).to_s + ' months at sub level #' + message['sub_plan'] + 'thanks to a gift from ' + message['user_name'] + ': "' + message['sub_message']['message'] + "\"\n" ) + elsif message['sub_plan'] then # Subscription + unix_socket.print( message['user_name'] + ' has ' + message['context'] + 'scribed for ' + (message['months'].to_i+1).to_s + ' months at sub level #' + message['sub_plan'] + ': "' + message['sub_message']['message'] + "\"\n" ) + elsif message['item_description'] then # commerce unix_socket.print( message['user_name'] + ' has purchased ' + message['item_description'] + ': "' + message['purchase_message']['message'] + "\"\n" ) - elsif message['bits_used'] then # bits + elsif message['bits_used'] then # bits unix_socket.print( message['user_name'] + ' sends ' + message['bits_used'].to_s + 'bits: "' + message['chat_message'] + "\"\n" ) - elsif message['tags'] then # whisper + elsif message['tags'] then # whisper puts message['tags']['login'] + ' whispers: "' + message['body'] + '"' end unix_socket.close @@ -70,7 +72,7 @@ IO.select( [ tcp_socket ] ) print ssl_socket.read_nonblock( 4096 ) # Twitch calls it "auth_token" here rather than the usual "access_token" because Twitch. -ssl_socket.write( WebSocket::Frame::Outgoing::Client.new( data: '{ "type": "LISTEN", "data": { "topics": ["channel-bits-events-v1.' + channel_id + '", "channel-subscribe-events-v1.' + channel_id + '", "channel-commerce-events-v1.' + channel_id + '", "whispers.' + channel_id + '" ], "auth_token": "'+access_token+'" } }', type: :text ).to_s ) +ssl_socket.write( WebSocket::Frame::Outgoing::Client.new( data: '{ "type": "LISTEN", "nonce": "fart", "data": { "topics": ["channel-bits-events-v1.' + channel_id + '", "channel-subscribe-events-v1.' + channel_id + '", "channel-commerce-events-v1.' + channel_id + '", "whispers.' + channel_id + '" ], "auth_token": "'+access_token+'" } }', type: :text ).to_s ) # Keepalive thread t2 = Thread.new { @@ -110,6 +112,9 @@ t3 = Thread.new { # {"type":"MESSAGE","data":{"topic":"channel-subscribe-events-v1.59895482","message":"{\"user_name\":\"tobiidk\",\"display_name\":\"TobiiDK\",\"channel_name\":\"bungmonkey\",\"user_id\":\"22413536\",\"channel_id\":\"59895482\",\"time\":\"2017-07-12T11:21:07.118989503Z\",\"sub_message\":{\"message\":\"\",\"emotes\":null},\"sub_plan\":\"1000\",\"sub_plan_name\":\"Throw money at Bung\",\"months\":0,\"context\":\"sub\"}"}} # EOF # +# sub_frame = <<-'EOF' +#{"type":"MESSAGE","data":{"topic":"channel-subscribe-events-v1.59895482","message":"{\"user_name\":\"sash_isynergy\",\"display_name\":\"sasH_iSynergy\",\"channel_name\":\"bungmonkey\",\"user_id\":\"101100602\",\"channel_id\":\"59895482\",\"recipient_id\":\"152875613\",\"recipient_user_name\":\"twixgun\",\"recipient_display_name\":\"twixgun\",\"time\":\"2018-06-14T14:42:01.772426724Z\",\"sub_message\":{\"message\":\"\",\"emotes\":null},\"sub_plan\":\"1000\",\"sub_plan_name\":\"5\",\"months\":0,\"context\":\"subgift\"}"}} +# # bits_frame = <<-'EOF' # {"type":"MESSAGE","data":{"topic":"channel-bits-events-v1.59895482","message":"{\"data\":{\"user_name\":\"tobiidk\",\"channel_name\":\"bungmonkey\",\"user_id\":\"22413536\",\"channel_id\":\"59895482\",\"time\":\"2017-07-12T11:17:37.432Z\",\"chat_message\":\"cheer100 cheer100 From a bro! cheer100 cheer100\",\"bits_used\":400,\"total_bits_used\":400,\"context\":\"cheer\",\"badge_entitlement\":{\"new_version\":100,\"previous_version\":0}},\"version\":\"1.0\",\"message_type\":\"bits_event\",\"message_id\":\"6e7e92bb-8b97-5c00-872c-256ed59a920c\"}"}} # EOF -- cgit v1.2.3