Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wjessop committed Sep 12, 2011
0 parents commit 1c296ef
Show file tree
Hide file tree
Showing 13 changed files with 595 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.rvmrc
pkg/
2 changes: 2 additions & 0 deletions Gemfile
@@ -0,0 +1,2 @@
source :rubygems
gemspec
135 changes: 135 additions & 0 deletions README.md
@@ -0,0 +1,135 @@
# Scamp

A framework for writing [Campfire](http://campfirenow.com/) bots. Scamp is in early development so use it at your own risk, pull requests welcome.

## Requirements

Ruby >= 1.9.2 (At least for the named captures)

## Installation

`gem install Scamp` or put `gem 'Scamp'` in your Gemfile.

## Usage and Examples

Matchers are tested in order and all that satisfy the match and conditions will be run. Careful, Scamp listens to itself, you could easily create an infinite loop. Look in the examples dir for more.

require 'Scamp'

scamp = Scamp.new(:api_key => "YOUR API KEY")

Scamp.behaviour do
#
# Simple matching based on regex or string:
#
match /^repeat (\w+), (\w+)$/ do
say "You said #{matches[0]} and #{matches[1]}"
end
#
# A special user and channel method is available in match blocks.
#
match "a user said" do
say "#{user} said something in channel #{channel}"
end
match "Hello!" do
say "Hi there"
end
#
# Limit the match to certain channels, users or both.
#
match /^Lets match (.+)$/, :conditions => {:channel => /someregex/} do
say "Only said if channel name mathces /someregex/"
end
match "some text", :conditions => {:user => /someregex/} do
say "Only said if user name mathces /someregex/"
end
match /some other text/, :conditions => {:user => /someregex/, :channel => /some other regex/} do
say "You can mix conditions"
end
#
# Named caputres become avaiable in your match block
#
match /^say (?<yousaid>.+)$/ do
say "You said #{yousaid}"
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.
#
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"
end
# Connect and join some channels
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:

:conditions => {:channel => /someregex/}
:conditions => {:channel => "some string"}
:conditions => {:channel => 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"

## TODO

* Write the tests
* Allow multiple values for conditions, eg: :conditions => {:channel => [/someregex/, "Some channel"]}
* Remove debugging output
* Add support for a logger

## Known issues

* Bot doesn't detect that it's been kicked out of a channel and recommect
* Bot tends to crash when it encounters an error.

## How to contribute

Here's the most direct way to get your work merged into the project:

1. Fork the project
2. Clone down your fork
3. Create a feature branch
4. Add your feature + tests
5. Make sure everything still passes by running the tests
6. If necessary, rebase your commits into logical chunks, without errors
7. Push the branch up
8. Send a pull request for your branch

Take a look at the TODO list or known issues for some inspiration if you need it.

## License

Copyright (C) 2011 by Will Jessop

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require 'bundler/gem_tasks'
76 changes: 76 additions & 0 deletions examples/bot.rb
@@ -0,0 +1,76 @@
#!/usr/bin/env ruby

$:.unshift File.join(File.dirname(__FILE__), '../lib')

require 'Scamp'

scamp = Scamp.new(:api_key => "YOUR API KEY")

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"
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"
end

# Limit a match to a channel condition based on a regex
match /^channel regex (.+)$/, :conditions => {:channel => /someregex/} do
say "Limit a match to a channel condition based on a regex"
end

# Limit a match to a user condition based on a regex
match /^user regex (.+)$/, :conditions => {:user => /someregex/} do
say "Limit a match to a user condition based on a regex"
end

# Limit a match to a user condition based on a string
match /^user name (.+)$/, :conditions => {:user => "Will Jessop"} do
say "Limit a match to a user condition based on a string"
end

# Limit a match to a user condition based on a string
match "user id check", :conditions => {:user => 774016} do
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"
end

# Match text with a regex, access the captures from the match object
match /^repeat (\w+), (\w+)$/ do
say "You said #{matches[0]} and #{matches[1]}"
end

# Match text with a regex, access the named captures as a method
match /^say (?<yousaid>.+)$/ do
say "You said #{yousaid}"
end

# Simple string match, interpolating the channel 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 channel, using the channel ID
say "#{user} said something in channel #{channel}", 293788

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

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

# FIXME: this does if the channel doesn't exist. Need a better error.
scamp.connect!([293788, "Monitoring"])
51 changes: 51 additions & 0 deletions lib/scamp.rb
@@ -0,0 +1,51 @@
require 'eventmachine'
require 'em-http-request'
require 'yajl'

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

class Scamp
include Connection
include Channels
include Users

attr_accessor :channels, :user_cache, :channel_cache
attr :matchers, :api_key

def initialize(options = {})
options ||= {}
raise ArgumentError, "You must pass an API key" unless options[:api_key]

@api_key = options[:api_key]
@channels = {}
@user_cache = {}
@channel_cache = {}
@matchers ||= []
end

def behaviour &block
instance_eval &block
end

def connect!(channel_list)
connect(api_key, channel_list)
end

private

def match trigger, params={}, &block
params ||= {}
matchers << Matcher.new(self, {:trigger => trigger, :action => block, :conditions => params[:conditions]})
end

def process_message(msg)
matchers.each do |matcher|
matcher.attempt(msg)
end
end
end
57 changes: 57 additions & 0 deletions lib/scamp/action.rb
@@ -0,0 +1,57 @@
#
# Actions are run in the context of a Scamp::Action.
# This allows us to make channel, nick etc. methods
# available on a per-message basis
#

# {:room_id=>401839, :created_at=>"2011/09/10 00:23:19 +0000", :body=>"something", :id=>408089344, :user_id=>774016, :type=>"TextMessage"}

class Scamp
class Action

attr :matches, :bot

def initialize(bot, action, message)
@bot = bot
@action = action
@message = message
end

def matches=(match)
@matches = match[1..-1]
match.names.each do |name|
name_s = name.to_sym
self.class.send :define_method, name_s do
match[name_s]
end
end
end

def channel
puts "Need the real channel name at #{__FILE__}:#{__LINE__}"
@message[:room_id]
end

def user
bot.username_for(@message[:user_id])
end

def user_id
@message[:user_id]
end

def message
@message[:body]
end

def run
self.instance_eval &@action
end

private

def say(msg, channel_id_or_name = channel)
bot.say(msg, channel_id_or_name)
end
end
end

0 comments on commit 1c296ef

Please sign in to comment.