Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- First, install Ruby 2.2 or newer. The Ruby installation should come with
- Rubygems (the package-manager for Ruby stuff) preinstalled. Using Rubygems,
- you can install Twittbot with `gem install twittbot`. After doing that the
- `twittbot` command should be available in your system.
- You would create a new bot using `twittbot new my-bot` from the terminal, then
- cd’ing in that directory and authenticating using `twittbot auth`.
- Then you create a new .yml file in the ./etc directory which contains all the
- IDs I want to tweet at that looks like this:
- nilsding@localhost:~/tmp/my-bot> cat etc/announcement.yml
- ---
- :ids:
- - 1234
- - 4567
- - 8901
- :already_tweeted: []
- Now comes the actual code. For that, create the file
- lib/announcement.rb which looks like this:
- Twittbot::BotPart.new :announcement do
- every 30, :minutes do
- ids = @config[:ids].shift(15) # take 25 ids at a time
- next if ids.empty? # do not do anything if there are no ids anymore
- ids.each do |id|
- begin
- # look up the user name behind the ID, keep in mind that the
- # endpoint for that might be rate limited
- username = bot.user(id, skip_status: true)&.screen_name
- if username.nil?
- @config[:ids] << id
- next
- end
- # finally, tweet the message:
- bot.tweet "@#{username} Hey, check out my cool site!”
- # and add the id to the :already_tweeted list
- @config[:already_tweeted] << id
- rescue => e
- puts "fatal error: " + e.message
- @config[:ids] << id
- end
- end
- bot.save_config
- end
- end
- This is untested Ruby code, but should do what I want.
- You can run this bot using the `twittbot start` command. If everything works
- it should start tweeting at the first 25 ids right away
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement