-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
namespace the connector and Message classes
There's a new transitive dependency on the console gem which conflicts with our Console connector at the root of the module/class namespace. So I wrapped our connectors in a Linkbot module. Confirmed that Console and Slack work which are the ones we mostly care about these days. Wrapping Message and MessageType in the Linkbot module sorted out the connectors being able to use Message within the namespace. Also, probably avoids some future naming conflict with a message gem that sneaks its way into dependencies.
- Loading branch information
Robb Kidd
committed
May 1, 2022
1 parent
ca8d356
commit c3a66f1
Showing
8 changed files
with
413 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,107 @@ | ||
class Campfire < Linkbot::Connector | ||
Linkbot::Connector.register('campfire', self) | ||
module Linkbot | ||
class Campfire < Linkbot::Connector | ||
Linkbot::Connector.register('campfire', self) | ||
|
||
def initialize(options) | ||
super(options) | ||
end | ||
def initialize(options) | ||
super(options) | ||
end | ||
|
||
def start | ||
request_options = { | ||
:head => { | ||
'authorization' => [@options['username'], @options['password']], | ||
'Content-Type' => 'application/json' | ||
def start | ||
request_options = { | ||
:head => { | ||
'authorization' => [@options['username'], @options['password']], | ||
'Content-Type' => 'application/json' | ||
} | ||
} | ||
} | ||
|
||
user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/me.json").get request_options | ||
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble logging in." } | ||
user_http.callback { | ||
@user = JSON.parse(user_http.response)["user"] | ||
request_options[:head]['authorization'] = [@user["api_auth_token"],"x"] | ||
request_options[:body] = "_" | ||
|
||
join_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options["room"]}/join.xml").post request_options | ||
join_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." } | ||
join_http.callback { | ||
listen | ||
|
||
user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/me.json").get request_options | ||
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble logging in." } | ||
user_http.callback { | ||
@user = JSON.parse(user_http.response)["user"] | ||
request_options[:head]['authorization'] = [@user["api_auth_token"],"x"] | ||
request_options[:body] = "_" | ||
|
||
join_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options["room"]}/join.xml").post request_options | ||
join_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." } | ||
join_http.callback { | ||
listen | ||
} | ||
} | ||
} | ||
end | ||
end | ||
|
||
def listen | ||
options = { | ||
:path => "/room/#{@options["room"]}/live.json", | ||
:host => "streaming.campfirenow.com", | ||
:auth => "#{@user["api_auth_token"]}:x", | ||
:timeout => 6 | ||
} | ||
def listen | ||
options = { | ||
:path => "/room/#{@options["room"]}/live.json", | ||
:host => "streaming.campfirenow.com", | ||
:auth => "#{@user["api_auth_token"]}:x", | ||
:timeout => 6 | ||
} | ||
|
||
stream = Twitter::JSONStream.connect(options) | ||
stream = Twitter::JSONStream.connect(options) | ||
|
||
stream.each_item do |item| | ||
process_message(item) | ||
end | ||
stream.each_item do |item| | ||
process_message(item) | ||
end | ||
|
||
stream.on_error do |message| | ||
Linkbot.log.error "Campfire connector: #{message.inspect}" | ||
end | ||
stream.on_error do |message| | ||
Linkbot.log.error "Campfire connector: #{message.inspect}" | ||
end | ||
|
||
stream.on_max_reconnects do |timeout, retries| | ||
Linkbot.log.fatal "Campire connector: tried #{retries} times to connect." | ||
exit | ||
stream.on_max_reconnects do |timeout, retries| | ||
Linkbot.log.fatal "Campire connector: tried #{retries} times to connect." | ||
exit | ||
end | ||
end | ||
end | ||
|
||
|
||
def process_message(item) | ||
message = JSON.parse(item) | ||
def process_message(item) | ||
message = JSON.parse(item) | ||
|
||
if message['type'] == 'TextMessage' && message['user_id'] != @user["id"] | ||
# Check if the user who is sending this message exists in the DB yet - if not, load the users details before | ||
# processing the message | ||
if Linkbot.user_exists?(message['user_id']) | ||
# Build the message | ||
message = Message.new( message['body'], message['user_id'], Linkbot.user_ids[message['user_id']], self, :message, {} ) | ||
invoke_callbacks(message) | ||
else | ||
# Fetch the user data from campfire, then process the callbacks | ||
request_options = { | ||
:head => { | ||
'authorization' => [@user['api_auth_token'], "x"], | ||
'Content-Type' => 'application/json' | ||
} | ||
} | ||
|
||
user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/#{message['user_id']}.json").get request_options | ||
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." } | ||
user_http.callback { | ||
user = JSON.parse(user_http.response)["user"] | ||
Linkbot.add_user(user["name"],user["id"]) | ||
if message['type'] == 'TextMessage' && message['user_id'] != @user["id"] | ||
# Check if the user who is sending this message exists in the DB yet - if not, load the users details before | ||
# processing the message | ||
if Linkbot.user_exists?(message['user_id']) | ||
# Build the message | ||
message = Message.new( message['body'], message['user_id'], Linkbot.user_ids[message['user_id']], self, :message, {} ) | ||
invoke_callbacks(message) | ||
} | ||
else | ||
# Fetch the user data from campfire, then process the callbacks | ||
request_options = { | ||
:head => { | ||
'authorization' => [@user['api_auth_token'], "x"], | ||
'Content-Type' => 'application/json' | ||
} | ||
} | ||
|
||
user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/#{message['user_id']}.json").get request_options | ||
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." } | ||
user_http.callback { | ||
user = JSON.parse(user_http.response)["user"] | ||
Linkbot.add_user(user["name"],user["id"]) | ||
message = Message.new( message['body'], message['user_id'], Linkbot.user_ids[message['user_id']], self, :message, {} ) | ||
invoke_callbacks(message) | ||
} | ||
end | ||
end | ||
end | ||
end | ||
|
||
def send_messages(messages,options = {}) | ||
flattened_messages = [] | ||
messages.each {|m| flattened_messages = flattened_messages + m.split("\n")} | ||
def send_messages(messages,options = {}) | ||
flattened_messages = [] | ||
messages.each {|m| flattened_messages = flattened_messages + m.split("\n")} | ||
|
||
flattened_messages.each_with_index do |m,i| | ||
next if m.strip.empty? | ||
flattened_messages.each_with_index do |m,i| | ||
next if m.strip.empty? | ||
|
||
request_options = { | ||
:head => { | ||
'authorization' => [@user["api_auth_token"],"x"], | ||
'Content-Type' => 'application/json' | ||
}, | ||
:body => {'message' => {'body' => m, 'type' => "TextMessage"}}.to_json | ||
} | ||
request_options = { | ||
:head => { | ||
'authorization' => [@user["api_auth_token"],"x"], | ||
'Content-Type' => 'application/json' | ||
}, | ||
:body => {'message' => {'body' => m, 'type' => "TextMessage"}}.to_json | ||
} | ||
|
||
request = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options['room']}/speak.json").post request_options | ||
request = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options['room']}/speak.json").post request_options | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
class Console < Linkbot::Connector | ||
Linkbot::Connector.register('console', self) | ||
module Linkbot | ||
class Console < Linkbot::Connector | ||
Linkbot::Connector.register('console', self) | ||
|
||
def initialize(options) | ||
super(options) | ||
end | ||
def initialize(options) | ||
super(options) | ||
end | ||
|
||
def start | ||
prompt | ||
end | ||
def start | ||
prompt | ||
end | ||
|
||
def prompt | ||
print "[Console] >> " | ||
handle_message(gets) | ||
end | ||
def prompt | ||
print "[Console] >> " | ||
handle_message(gets) | ||
end | ||
|
||
def handle_message(nick='Terminal User', room='Console', msg) | ||
options = {room: room} | ||
def handle_message(nick='Terminal User', room='Console', msg) | ||
options = {room: room} | ||
|
||
if !Linkbot.user_exists?(nick) | ||
Linkbot.add_user(nick, nick) | ||
end | ||
if !Linkbot.user_exists?(nick) | ||
Linkbot.add_user(nick, nick) | ||
end | ||
|
||
message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options) | ||
message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options) | ||
|
||
EventMachine::defer(proc { | ||
invoke_callbacks(message, options) | ||
}) | ||
EventMachine::defer(proc { prompt}) | ||
end | ||
EventMachine::defer(proc { | ||
invoke_callbacks(message, options) | ||
}) | ||
EventMachine::defer(proc { prompt}) | ||
end | ||
|
||
def send_messages(msgs, options={}) | ||
msgs.each do |msg| | ||
puts msg | ||
def send_messages(msgs, options={}) | ||
msgs.each do |msg| | ||
puts msg | ||
end | ||
EventMachine::defer(proc { prompt}) | ||
end | ||
EventMachine::defer(proc { prompt}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.