Skip to content

Commit

Permalink
Rename channel to room to be more in line with Campfire and it's API
Browse files Browse the repository at this point in the history
  • Loading branch information
wjessop committed Sep 23, 2011
1 parent a59ec60 commit 9c2463f
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 145 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Matchers are tested in order and all that satisfy the match and conditions will
end
#
# A special user and channel method is available in match blocks.
# A special user and room method is available in match blocks.
#
match "a user said" do
say "#{user} said something in channel #{channel}"
say "#{user} said something in room #{room}"
end
match "Hello!" do
Expand All @@ -48,17 +48,17 @@ Matchers are tested in order and all that satisfy the match and conditions will
end
#
# Limit the match to certain channels, users or both.
# Limit the match to certain rooms, users or both.
#
match /^Lets match (.+)$/, :conditions => {:channel => "Some Channel"} do
say "Only said if channel name mathces /someregex/"
match /^Lets match (.+)$/, :conditions => {:room => "Some Room"} do
say "Only said if room name mathces /someregex/"
end
match "some text", :conditions => {:user => "Some User"} do
say "Only said if user name mathces /someregex/"
end
match /some other text/, :conditions => {:user => "Some User", :channel => 123456} do
match /some other text/, :conditions => {:user => "Some User", :room => 123456} do
say "You can mix conditions"
end
Expand All @@ -70,13 +70,13 @@ Matchers are tested in order and all that satisfy the match and conditions will
end
#
# You can say multiple times, and you can specify an alternate channel.
# Default behaviour is to 'say' in the channel that caused the match.
# You can say multiple times, and you can specify an alternate room.
# Default behaviour is to 'say' in the room that caused the match.
#
match "something" do
say "#{user} said something in channel #{channel}"
say "#{user} said something in channel #{channel}", 237872
say "#{user} said something in channel #{channel}", "System Administration"
say "#{user} said something in room #{room}"
say "#{user} said something in room #{room}", 237872
say "#{user} said something in room #{room}", "System Administration"
end
#
Expand All @@ -95,27 +95,27 @@ Matchers are tested in order and all that satisfy the match and conditions will
end
end
# Connect and join some channels
# Connect and join some rooms
scamp.connect!([293788, "Monitoring"])

In the channel/user conditions you can use the name, regex or ID of a user or channel, in say you can ise a string or ID, eg:
In the room/user conditions you can use the name, regex or ID of a user or room, in say you can ise a string or ID, eg:

:conditions => {:channel => /someregex/}
:conditions => {:channel => "some string"}
:conditions => {:channel => 123456}
:conditions => {:room => /someregex/}
:conditions => {:room => "some string"}
:conditions => {:room => 123456}

:conditions => {:user => /someregex/}
:conditions => {:user => "some string"}
:conditions => {:user => 123456}

say "#{user} said something in channel #{channel}", 237872
say "#{user} said something in channel #{channel}", "System Administration"
say "#{user} said something in room #{room}", 237872
say "#{user} said something in room #{room}", "System Administration"

By default Scamp listens to itself. This could either be fun, or dangerous, you decide. You can turn this off by passing :first\_match\_only => true in the initialisation options

scamp = Scamp.new(:api_key => "YOUR API KEY", :subdomain => "yoursubdomain", :first_match_only => true)

Scamp will listen to all messages that are sent on the channels it is listening on and doesn't need to be addressed by name. If you prefer to only trigger bot commands when you address your bot directly add the :required\_prefix initialisation option:
Scamp will listen to all messages that are sent on the rooms it is listening on and doesn't need to be addressed by name. If you prefer to only trigger bot commands when you address your bot directly add the :required\_prefix initialisation option:

scamp = Scamp.new(:api_key => "YOUR API KEY", :subdomain => "yoursubdomain", :required_prefix => 'Bot: ')

Expand All @@ -124,7 +124,7 @@ Scamp will now require commands to begin with 'Bot: ' (or whatever you have spec
## TODO

* Write more tests
* Allow multiple values for conditions, eg: :conditions => {:channel => ["This channel", "Some channel"]}
* Allow multiple values for conditions, eg: :conditions => {:room => ["This room", "Some room"]}
* Add paste support

## Known issues
Expand Down
42 changes: 21 additions & 21 deletions examples/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
scamp = Scamp.new(:api_key => "YOUR API KEY", :subdomain => "37s")

scamp.behaviour do
# Match some regex limited to a channel condition based on a channel id
match /^channel id (.+)$/, :conditions => {:channel => 401839} do
# Reply in the current channel
say "Match some regex limited to a channel condition based on a channel id"
# Match some regex limited to a room condition based on a room id
match /^room id (.+)$/, :conditions => {:room => 401839} do
# Reply in the current room
say "Match some regex limited to a room condition based on a room id"
end

# Limit a match to a channel condition based on a string
match "channel name check", :conditions => {:channel => "Monitoring"} do
say "Limit a match to a channel condition based on a string"
# Limit a match to a room condition based on a string
match "room name check", :conditions => {:room => "Monitoring"} do
say "Limit a match to a room condition based on a string"
end

# Limit a match to a user condition based on a string
Expand All @@ -28,10 +28,10 @@
say "Limit a match to a user condition based on an ID"
end

# Limit a match to a channel & user condition combined
match /^something (.+)$/, :conditions => {:channel => "Monitoring", :user => "Will Jessop"} do
# Reply in the current channel
say "Limit a match to a channel & user condition combined"
# Limit a match to a room & user condition combined
match /^something (.+)$/, :conditions => {:room => "Monitoring", :user => "Will Jessop"} do
# Reply in the current room
say "Limit a match to a room & user condition combined"
end

# Match text with a regex, access the captures from the match object
Expand All @@ -44,16 +44,16 @@
say "You said #{yousaid}"
end

# Simple string match, interpolating the channel and user in response.
# Simple string match, interpolating the room and user in response.
match "something" do |data|
# Send the response to a different channel
say "#{user} said something in channel #{channel}", "Robot Army"
# Send the response to a different room
say "#{user} said something in room #{room}", "Robot Army"

# Send the response to a different channel, using the channel ID
say "#{user} said something in channel #{channel}", 293788
# Send the response to a different room, using the room ID
say "#{user} said something in room #{room}", 293788

# Send the response to the originating channel
say "#{user} said something in channel #{channel}"
# Send the response to the originating room
say "#{user} said something in room #{room}"
end

# Play some sounds
Expand All @@ -62,11 +62,11 @@
play "drama"
end

match "multi-condition match", :conditions => {:channel => [401839, "Monitoring"], :user => ["Will Jessop", "Noah Lorang"]} do
# Reply in the current channel
match "multi-condition match", :conditions => {:room => [401839, "Monitoring"], :user => ["Will Jessop", "Noah Lorang"]} do
# Reply in the current room
say "multi-condition match"
end
end

# FIXME: this does if the channel doesn't exist. Need a better error.
# FIXME: this does if the room doesn't exist. Need a better error.
scamp.connect!([293788, "Monitoring"])
18 changes: 9 additions & 9 deletions lib/scamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

require "scamp/version"
require 'scamp/connection'
require 'scamp/channels'
require 'scamp/rooms'
require 'scamp/users'
require 'scamp/matcher'
require 'scamp/action'
require 'scamp/messages'

class Scamp
include Connection
include Channels
include Rooms
include Users
include Messages

attr_accessor :channels, :user_cache, :channel_cache, :matchers, :api_key, :subdomain,
:logger, :verbose, :first_match_only, :required_prefix, :channels_to_join
attr_accessor :rooms, :user_cache, :room_cache, :matchers, :api_key, :subdomain,
:logger, :verbose, :first_match_only, :required_prefix, :rooms_to_join

def initialize(options = {})
options ||= {}
Expand All @@ -34,20 +34,20 @@ def initialize(options = {})
end
end

@channels_to_join = []
@channels = {}
@rooms_to_join = []
@rooms = {}
@user_cache = {}
@channel_cache = {}
@room_cache = {}
@matchers ||= []
end

def behaviour &block
instance_eval &block
end

def connect!(channel_list)
def connect!(room_list)
logger.info "Starting up"
connect(api_key, channel_list)
connect(api_key, room_list)
end

def command_list
Expand Down
16 changes: 8 additions & 8 deletions lib/scamp/action.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Actions are run in the context of a Scamp::Action.
# This allows us to make channel, user etc. methods
# This allows us to make room, user etc. methods
# available on a per-message basis
#

Expand All @@ -27,12 +27,12 @@ def matches=(match)
end if match.respond_to?(:names) # 1.8 doesn't support named captures
end

def channel_id
def room_id
@message[:room_id]
end

def channel
bot.channel_name_for @message[:room_id]
def room
bot.room_name_for @message[:room_id]
end

def user
Expand All @@ -57,12 +57,12 @@ def command_list
bot.command_list
end

def say(msg, channel_id_or_name = channel_id)
bot.say(msg, channel_id_or_name)
def say(msg, room_id_or_name = room_id)
bot.say(msg, room_id_or_name)
end

def play(sound, channel_id_or_name = channel_id)
bot.play(sound, channel_id_or_name)
def play(sound, room_id_or_name = room_id)
bot.play(sound, room_id_or_name)
end
end
end
12 changes: 6 additions & 6 deletions lib/scamp/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ class Scamp
module Connection
private

def connect(api_key, channel_list)
def connect(api_key, room_list)
EventMachine.run do

# Check for channels to join, and join them
# Check for rooms to join, and join them
EventMachine::add_periodic_timer(5) do
while id = @channels_to_join.pop
while id = @rooms_to_join.pop
join_and_stream(id)
end
end

populate_channel_list do
logger.debug "Adding #{channel_list.join ', '} to list of channels to join"
@channels_to_join = channel_list.map{|c| channel_id(c) }
populate_room_list do
logger.debug "Adding #{room_list.join ', '} to list of rooms to join"
@rooms_to_join = room_list.map{|c| room_id(c) }
end

end
Expand Down
10 changes: 5 additions & 5 deletions lib/scamp/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ def run(msg, match = nil)
def conditions_satisfied_by(msg)
bot.logger.debug "Checking message against #{conditions.inspect}"

# item will be :user or :channel
# item will be :nick or :room
# cond is the int or string value.
conditions.each do |item, cond|
bot.logger.debug "Checking #{item} against #{cond}"
bot.logger.debug "msg is #{msg.inspect}"
if cond.is_a? Integer
# bot.logger.debug "item is #{msg[{:channel => :room_id, :user => :user_id}[item]]}"
return false unless msg[{:channel => :room_id, :user => :user_id}[item]] == cond
# bot.logger.debug "item is #{msg[{:room => :room_id, :user => :user_id}[item]]}"
return false unless msg[{:room => :room_id, :user => :user_id}[item]] == cond
elsif cond.is_a? String
case item
when :channel
return false unless bot.channel_name_for(msg[:room_id]) == cond
when :room
return false unless bot.room_name_for(msg[:room_id]) == cond
when :user
return false unless bot.username_for(msg[:user_id]) == cond
end
Expand Down
14 changes: 7 additions & 7 deletions lib/scamp/messages.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
class Scamp
module Messages

def say(message, channel_id_or_name)
send_message(channel_id_or_name, message, "Textmessage")
def say(message, room_id_or_name)
send_message(room_id_or_name, message, "Textmessage")
end

def play(sound, channel_id_or_name)
send_message(channel_id_or_name, sound, "SoundMessage")
def play(sound, room_id_or_name)
send_message(room_id_or_name, sound, "SoundMessage")
end

private

# curl -vvv -H 'Content-Type: application/json' -d '{"message":{"body":"Yeeeeeaaaaaahh", "type":"Textmessage"}}' -u API_KEY:X https://37s.campfirenow.com/room/293788/speak.json
def send_message(channel_id_or_name, payload, type)
def send_message(room_id_or_name, payload, type)
# post 'speak', :body => {:message => {:body => message, :type => type}}.to_json
url = "https://#{subdomain}.campfirenow.com/room/#{channel_id(channel_id_or_name)}/speak.json"
url = "https://#{subdomain}.campfirenow.com/room/#{room_id(room_id_or_name)}/speak.json"
http = EventMachine::HttpRequest.new(url).post :head => {'Content-Type' => 'application/json', 'authorization' => [api_key, 'X']}, :body => Yajl::Encoder.encode({:message => {:body => payload, :type => type}})
http.errback { logger.error "Error speaking: '#{message}' to #{channel_id(channel_id_or_name)}" }
http.errback { logger.error "Error speaking: '#{message}' to #{room_id(room_id_or_name)}" }
end

end
Expand Down
Loading

0 comments on commit 9c2463f

Please sign in to comment.