For the Creative Coding Maastricht Meetup. We'll use it in a project that fires up an Arduino connected light installation everytime someone tweets something that matches #caracola. For great profit of course!
Assuming you have a *nix system, and you have ruby
, rubygems
, bundler
and rbenv
installed. Then, clone this repository and run bundle install
. Now that the dependencies are taken care of, you can start the script by running bundle exec ruby twitter_hashtag_monitor.rb
. Enjoy the stream! YMMV.
Go to https://apps.twitter.com/ to get yer API keys.
Place them in a file called .env
which you will copy from example.env
by using the following command:
cp example.env .env
In the file twitter_hashtag_monitor.rb
you can set the filters you'd like in the topics
variable, like so:
topics = ['#coffee', '#caracola']
In the infinite loop you can plug any arbitrary code you want, e.g. to fire the Arduino.
# Infinite loop
client.filter(track: topics.join(',')) do |object|
if object.is_a?(Twitter::Tweet)
# It's a matching tweet! Do anything you want!
# e.g. print the Tweet content
puts "[#{Time.now.strftime("%F %T")}] #{object.user.name}: #{object.text}"
end
end
More documentation available in the twitter gem that powers the script.
Then run the following to execute script:
bundle exec twitter_hashtag_monitor.rb
Use this to print a limited number of twitter messages to stdout
.
# Outputs 1 twitter message
bundle exec one_twitter_message.rb
# Outputs 123 twitter message
bundle exec one_twitter_message.rb 123
Thank you Caracola for generously providing us with a place to host our meetups! We recommend anyone in the Maastricht NL area to keep a close eye on this space, because there's a ton of exciting stuff happening.