• This repository has been archived on 24/Nov/2017
  • Stars
    star
    178
  • Rank 214,989 (Top 5 %)
  • Language
    Ruby
  • Created almost 16 years ago
  • Updated about 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple framework for creating Twitter bots, inspired by Sinatra

IMPORTANT

THIS PROJECT IS DEAD. IT DOES NOT WORK. THE CODE IS KEPT HERE ONLY FOR HISTORIC PURPOSES.

DO NOT USE THIS PROJECT.

Twibot

Official URL: github.com/cjohansen/twibot/tree/master Christian Johansen (www.cjohansen.no) Twitter: @cjno

Description

Twibot (pronounced like “Abbot”), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra.

Usage

Simple example

require 'twibot'

# Receive messages, and tweet them publicly
#
message do |message, params|
  post_tweet message
end

# Respond to @replies if they come from the right crowd
#
reply :from => [:cjno, :irbno] do |message, params|
  post_reply message, "I agree"
end

# Listen in and log tweets
#
tweet do |message, params|
  MyApp.log_tweet(message)
end

# Search for tweets matching a query.  The available search operators
# are explained here: <http://search.twitter.com/operators>
#
search "twibot" do |message, params|
  # do_something
end

# Search for tweets with a hashtag
#  see: <http://twitter.pbworks.com/Hashtags>
#
# Note: hashtag is just a convenience wrapper
# around search.  It will invoke the search
# before and after filters.
#
hashtag "twibot" do |message, params|
  # do_something
end

# Search for tweets with one of a number of hashtags
#  see: <http://twitter.pbworks.com/Hashtags>
#
# Note: hashtags is just an alias to hashtag
#
hashtags [:twibot, :ruby, "twitter4r"] do |message, params|
  # do_something
end

# Process any new followers.  user_id will be
# the user's Numeric id and params will always
# be an empty Hash.
#
# add_friend!(id) is a convenience wrapper around the
# twitter4r friendship method.  remove_friend!(id)
# is also available.
#
follower do |user_id, params|
  # keep out the riff-raff...
  bot.add_friend!(user_id) unless user_id == 890631
end

# add some set-up code that will be called
# before each polling cycle.  :all is the
# default, so it can safely be omitted
#
before :all do
  MyApp.log("Started polling at #{Time.now}")
end

# the after hook for the polling cycle gets
# passed the number of messages that were
# processed
#
after :all do |message_count|
  MyApp.log("Finished polling at #{Time.now}.  Got #{message_count} messages.")
end

# each action has before and after hooks available:
#  - follower
#  - message
#  - reply
#  - search
#  - tweet
#
# there can be only one before and one after callback
# registered for a given type.  the callback block
# will be called with no arguments.
#
# Note: hashtag and hashtags are just wrappers around
# search and do not have their own hooks.  Use the
# search hooks when using hashtag or hashtags.
#
before :message do
  MyApp.is_processing_a_message = true
end

after :message do
  MyApp.is_processing_a_message = false
end

after :follower do
  MyApp.log("I have another follower!")
end

Running the bot

To run the bot, simply do:

ruby bot.rb

Configuration

Twibot looks for a configuration file in ./config/bot.yml. It should contain atleast:

login: twitter_login
password: twitter_password

You can also pass configuration as command line arguments:

ruby bot.rb --login myaccount

…or configure with Ruby:

configure do |conf|
  conf.login = "my_account"
do

If you don’t specify login and/or password in any of these ways, Twibot will prompt you for those.

If you want to change how Twibot is configured, you can setup the bot instance manually and give it only the configuration options you want:

# Create bot only with default configuration
require 'twibot'
bot = Twibot::Bot.new(Twibot::Config.default)

# Application here...

If you want command line arguments you can do:

require 'twibot'
bot = Twibot::Bot.new(Twibot::Config.default << Twibot::CliConfig.new)

To disable the buffering of the Twibot log file, set the ‘log_flush` config option to `true`:

configure do |conf|
  conf.log_file = File.join(DAEMON_ROOT, 'log', 'twitterd.log')
  conf.log_level = "info"
  conf.log_flush = true
end

“Routes”

Like Sinatra, and other web app frameworks, Twibot supports “routes”: patterns to match incoming tweets and messages:

require 'twibot'

tweet "time :country :city" do |message,params|
  time = MyTimeService.lookup(params[:country], params[:city])
  client.message :post, "Time is #{time} in #{params[:city]}, #{params[:country]}"
end

You can have several “tweet” blocks (or “message” or “reply”). The first one to match an incoming tweet/message will handle it.

As of the upcoming 0.1.5/0.2.0, Twibot also supports regular expressions as routes:

require 'twibot'

tweet /^time ([^\s]*) ([^\s]*)/ do |message, params|
  # params is an array of matches when using regexp routes
  time = MyTimeService.lookup(params[0], params[1])
  client.message :post, "Time is #{time} in #{params[:city]}, #{params[:country]}"
end

Working with the Twitter API

The DSL gives you access to your Twitter client instance through “client” (or “twitter”):

message do
  twitter.status :post, "Hello world" # Also: client.status :post, "Hello world"
end

Requirements

Twitter4r. You’ll need atleast 0.3.1, which is currently only available from GitHub. Versions of Twitter4r prior to 0.3.1 does not allow for the since_id parameter to be appended to URLs to the REST API. Twibot needs these to only fetch fresh messages and tweets.

Installation

gem install twibot

Is it Ruby 1.9?

As of Twibot 0.1.3, yes it is! All tests pass, please give feedback from real world usage if you have trouble.

Polling

Twitter pulled the plug on it’s xmpp service last year. This means that Twibot backed bots needs to poll the Twitter service to keep up. Twitter has a request limit on 70 reqs/hour, so you should configure your bot not to make more than that, else it will fail. You can ask for your bot account to be put on the whitelist which allows you to make 20.000 reqs/hour, and shouldn’t be a problem so long as your intentions are good (I think).

Twibot polls like this:

  • Poll messages if any message handlers exist

  • Poll tweets if any tweet or reply handlers exist

  • Sleep for interval seconds

  • Go over again

As long as Twibot finds any messages and/or tweets, the interval stays the same (min_interval configuration switch). If nothing was found however, the interval to sleep is increased by interval_step configuration option. This happens until it reaches max_interval, where it will stay until Twibot finds anything.

Contributors

License

(The MIT License)

Copyright © 2009 Christian Johansen

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.

More Repositories

1

juicer

A command line tool for JavaScript and CSS developers
Ruby
632
star
2

portfolio

Clojure
222
star
3

dumdom

Efficiently render and re-render immutable data
Clojure
157
star
4

.emacs.d

My Emacs config
Emacs Lisp
152
star
5

react-sweeper

Minesweeper in ES6 using React/immutable-js
JavaScript
106
star
6

replicant

A native ClojureScript virtual DOM renderer - render hiccup directly
Clojure
78
star
7

js-atom

Clojure(Script) atoms, in JavaScript
JavaScript
60
star
8

use_case

A small abstraction for encapsulating non-trivial business logic in Ruby applications
Ruby
48
star
9

courier

A high-level http client for Clojure and ClojureScript
Clojure
38
star
10

powerpack

A batteries-included static web site toolkit for Clojure
Clojure
35
star
11

validatious

Client side form validation with unobtrusive JavaScript
JavaScript
27
star
12

sinon-qunit

A small Sinon.JS adapter for QUnit that provides automatic sandboxing of mocks and stubs
JavaScript
26
star
13

om-sweeper

An (almost complete) implementation of Minesweeper in ClojureScript/Om (React.js)
Clojure
24
star
14

jstdutil

Small wrapper around Google's JsTestDriver to add colors, autotest and easier command invocation
Ruby
23
star
15

m1p

Map interpolation and DIY i18n/theming toolkit
Clojure
21
star
16

sinon-web

Single-page website for Sinon.JS
JavaScript
21
star
17

hiccup-find

Utilities to help you test hiccup markup generating functions
Clojure
19
star
18

phosphor-clj

Phosphor Icons as hiccup for Clojure and ClojureScript
Clojure
19
star
19

gadget-inspector

ClojureScript data browser - use from a Chrome extension or over a server in any browser
Clojure
18
star
20

powerblog

A step-by-step tutorial for building a static site with Stasis Powerpack
Clojure
16
star
21

auth0-ring

A ring middleware for OpenID Connect authentication with Auth0 in your Clojure app
Clojure
13
star
22

cjohansen-no

Source code and content for cjohansen.no doubling as a demo of Clojure/Stasis powered static sites.
JavaScript
13
star
23

imagine

Image engine for web apps: Crop, resize and filter images on the fly or to disk
Clojure
11
star
24

pharmacist

Declarative data fetching for Clojure(Script)
Clojure
9
star
25

when-rb

Ruby port of when.js
Ruby
9
star
26

form-app

Demonstrating how to build frontends around stateless, data-driven components
Clojure
8
star
27

sinon-nodeunit

A small Sinon.JS adapter for nodeunit that provides automatic sandboxing of mocks and stubs
JavaScript
8
star
28

asciidoclj

Clojure API for Asciidoc
Clojure
8
star
29

node-assert-extras

Additional assertions for Node.js
JavaScript
7
star
30

jscontext

Syntactical sugar for JsUnitTest tests - should and contexts
JavaScript
6
star
31

watch-tree

Watch directories recursively for changes using inotify for Linux
JavaScript
6
star
32

austin-repl-example

A sample Austin ClojureScript REPL setup
Clojure
6
star
33

spasm

"Just enough structure" for React-based single page web apps
JavaScript
6
star
34

Juicer2

Some initial thoughts and sketches for Juicer 2, partial rewrite of Juicer
Ruby
6
star
35

portable-text-clj

Render sanity.io Portable Text to HTML with Clojure
Clojure
6
star
36

em_pessimistic

popen with stderr and DeferrableChildProcess with errback for EventMachine
Ruby
6
star
37

fontawesome-clj

FontAwesome icons as hiccup for Clojure(Script)
Clojure
6
star
38

ubc_monitor

Monitor resource usage in OpenVZ backed VPS'.
Ruby
5
star
39

svn_import

Rake task for importing a brand new Rails project to subversion - ignoring certain files
5
star
40

live-search

A simple live search jQuery plugin, developed to showcase TDD with JavaScript
JavaScript
5
star
41

cullquery

Convenience functions for Cull.JS that use jQuery
JavaScript
4
star
42

funlines

A tiny function pipelining utility for Clojure
Clojure
4
star
43

mysql_foreign_keys

MySQL foreign keys for Rails migraitons
4
star
44

shufflify

A Spotify multi-playlist weighted shuffler
Go
3
star
45

internote

A fork of the abandoned Firefox plugin that allows sticky post-it notes on web pages
3
star
46

dumdom-devcards

Devcards for dumdom
Clojure
3
star
47

ezgravatar

A template that enables Gravatars for eZ Publish
3
star
48

clj-nats

Clojure wrapper for the official NATS.io Java SDK
Clojure
3
star
49

mmouse

Mouse movement utilities for JavaScript
JavaScript
3
star
50

clj-event-source

Clojure server-sent events Event Source client
Clojure
3
star
51

parrot

Stub HTTP responses for Clojure tests
Clojure
3
star
52

sasha

Stateless component libary for ClojureScript and dumdom
Clojure
2
star
53

libdolt

The core Dolt APIs, without any web framework dependencies
Ruby
2
star
54

chainable

A simple implementation of the chain of responsibility pattern in Ruby
2
star
55

ranger

Custom range input control for JavaScript
JavaScript
2
star
56

acts_as_prototype

A Rails plugin that adds a prototype and a property list to ActiveRecord objects (prototypes can be chained)
Ruby
2
star
57

dolt

Stand-alone Git repository browser
Ruby
2
star
58

cjohansen.github.io

Github pages
JavaScript
1
star
59

makeup

Markup and syntax highlighting
Ruby
1
star
60

cf-apply-template

A small wrapper for cloudfont create-stack/update-stack
Shell
1
star
61

revealjs-mode

An Emacs minor-mode for working with revealjs HTML files
Emacs Lisp
1
star
62

boosterconf2014

Node.js and React workshop at boosterconf.no 2014
1
star
63

om-tutorial-austin

The Om basic tutorial with the Austin browser repl
Clojure
1
star
64

cljs-compiler-woes

A reproduction of a ClojureScript compiler issue
Clojure
1
star
65

cider-figwheel-main

A demonstration of flaky CIDER/figwheel.main behavior
Clojure
1
star
66

facenode

Code from a live-coding talk. You had to be there.
JavaScript
1
star
67

docker-lein-slimer

A Docker image with Clojure, Leiningen and Slimer.JS
1
star
68

eval-lines.el

Evaluate Ruby code in Emacs. Lines ending in #=> will have the value of evaluating the line inserted after it
Emacs Lisp
1
star
69

validate

Form validation logic. Code is written for educational purposes, do not expect this to be maintained as a library.
JavaScript
1
star
70

origize.el

Emacs Lisp
1
star
71

cljsjs-moment-node

Demonstrating an issue with cljsjs/moment, node and optimizations: simple
Clojure
1
star
72

tddjs-com

The tddjs.com site
Shell
1
star
73

cljsns

Reproducing a surprising ClojureScript build snag
HTML
1
star
74

adventofcode2022

My Advent of code 2022 entries
Clojure
1
star
75

loose-client

A lean and relaxed chatting client
HTML
1
star
76

execration-no

execration.no
HTML
1
star
77

varnish-vmods-docker

A Varnish Dockerfile that includes compiled vmods
Dockerfile
1
star
78

tempcalc

A temperature calculator
Clojure
1
star
79

virtuoso-ui

Clojure
1
star
80

connect-four

The classic game of connect four
JavaScript
1
star
81

yahtzee-cljs

A functional programming implementation of Yahtzee, in ClojureScript
Clojure
1
star
82

em_rugged

Asynchronous Rugged (libgit2 bindings for Ruby) for EventMachine.
Ruby
1
star
83

compojure-api-tools-deps

Example of metosin/compojure-api not working with tools.deps
Clojure
1
star
84

replicant-sweeper

Minesweeper in Replicant
Clojure
1
star
85

recall-position.el

Remember position and window scroll in an Emacs buffer and restore it with a key-binding
Emacs Lisp
1
star
86

minesweeper-ui

JavaScript
1
star
87

uinit

An initialization system for your UI modules. Declare dependencies between features, scalars, data and elements and have you modules loaded as soon as possible.
JavaScript
1
star
88

ndc-2021

The code from my ClojureScript talk at NDC 2021: https://www.youtube.com/watch?v=yFVk3D76wQw
Clojure
1
star
89

lookup

Find content of interest in hiccup data
Clojure
1
star