blog.sorah.jp

Ruby Advent Calendar 2010 jp-en Day 6: Tinatra

Did you make any twitter bots?

Now Twitter doesn't support BASIC authorization, so bots making is more difficult.

OAuth authorization code is waste!

Tinatra resolve that problem. :)

Tinatra?

Sinatra: Classy web-development dressed in a DSL (quoted from github description)

Tinatra: Dirty twitter-bot development dressed in a DSL

Tinatra on github: https://github.com/sorah/tinatra

Install

$ gem install tinatra

or

$ git clone https://github.com/sorah/tinatra.git

Create a bot

Step 1. Register as OAuth consumer

http://dev.twitter.com/apps/new

Fill forms and submit.

NOTE: The following forms must be selected the following answers.

Application Type: Client

Default Access Type: Read & Write

Step 2. Note "Consumer key" and "Consumer secret"

See a application page; application page will be showed after registration.

Step 3. Write a code

  • Bot will reply a time to any mentions.
  • Bot post a time every boot.

Code is here

require 'tinatra'

mention do |d|
  api.update("@#{d[:user][:screen_name]} Hi, the time is #{Time.now}",
            :in_reply_to_status_id => d[:id])
end

always do
  api.update("#{Time.now}")
end

It's easy to read, easy to write. Any authorization codes don't exist.

Code is clean! Woooooo!

Step 4. Get Account For Bot

I'm using incognito window for making new account.

Step 5. Run

% ruby bot.rb
You must set a path to db file using --db= or db method.

Database is used to saving token, last mention, etc...

% ruby bot.rb --db=./db
Run bot.rb --init first.
% ruby bot.rb --db=./db --init
------------ AUTHORIZING ------------
Input your consumer key: CONSUMER_KEY
Input your consumer secret: CONSUMER_SECRET

Access This URL and press 'Allow' in account for tinatra => http://api.twitter.com//oauth/authorize...
Input key shown by twitter: 0000000

Authorizing is done.

Open URL and allow on bot account, then Input PIN code.

Final, run again.

% ruby bot.rb --db=./db 

0c4fed6571445bd0ac8f970625124506.png (552×307)

I post a mention for the bot and run again.

The bot returned the time to me.

c1345aa3bbff3ed4e9bc77521794108d.png (445×247)

Next?

  • Add the command to crontab
  • Announce the bot
  • etc...

Code description

require 'tinatra'

mention do |d|
  api.update("@#{d[:user][:screen_name]} Hi, the time is #{Time.now}",
            :in_reply_to_status_id => d[:id])
end

always do
  api.update("#{Time.now}")
end

mention block will be called with new mention.

always block will be called every boot.

api is an instance of OAuthRubytter.

More blocks exist for bot development.

require 'tinatra'

timeline do |tweet|
  p tweet #=> New tweet in timeline
end

mention do |m|
  p m #=> New tweet in mention
end

direct_message do |dm|
  p dm #=> New Direct message
end

followed do |user|
  p user #=> New user who is followed
end

removed do |user|
  p user #=> User removed
end

always do
  # Always run
  p api.class #=> OAuthRubytter
  api.update("hi")
end

It's useful.

Ruby Advent Calendar 2010: jp-en

This entry is for Ruby Advent Calendar jp-en: 2010.

Previous post is from igaiga555, And next post will be from tagomoris

I forgot to attend Ruby Advent Calendar jp: 2010.. ;-(

Backfilled at , Published at