Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. First, install Ruby 2.2 or newer. The Ruby installation should come with
  2. Rubygems (the package-manager for Ruby stuff) preinstalled. Using Rubygems,
  3. you can install Twittbot with `gem install twittbot`. After doing that the
  4. `twittbot` command should be available in your system.
  5.  
  6. You would create a new bot using `twittbot new my-bot` from the terminal, then
  7. cd’ing in that directory and authenticating using `twittbot auth`.
  8. Then you create a new .yml file in the ./etc directory which contains all the
  9. IDs I want to tweet at that looks like this:
  10.  
  11. nilsding@localhost:~/tmp/my-bot> cat etc/announcement.yml
  12. ---
  13. :ids:
  14. - 1234
  15. - 4567
  16. - 8901
  17. :already_tweeted: []
  18.  
  19. Now comes the actual code. For that, create the file
  20. lib/announcement.rb which looks like this:
  21.  
  22. Twittbot::BotPart.new :announcement do
  23. every 30, :minutes do
  24. ids = @config[:ids].shift(15) # take 25 ids at a time
  25. next if ids.empty? # do not do anything if there are no ids anymore
  26.  
  27. ids.each do |id|
  28. begin
  29. # look up the user name behind the ID, keep in mind that the
  30. # endpoint for that might be rate limited
  31. username = bot.user(id, skip_status: true)&.screen_name
  32. if username.nil?
  33. @config[:ids] << id
  34. next
  35. end
  36.  
  37. # finally, tweet the message:
  38. bot.tweet "@#{username} Hey, check out my cool site!”
  39. # and add the id to the :already_tweeted list
  40. @config[:already_tweeted] << id
  41. rescue => e
  42. puts "fatal error: " + e.message
  43. @config[:ids] << id
  44. end
  45. end
  46.  
  47. bot.save_config
  48. end
  49. end
  50.  
  51. This is untested Ruby code, but should do what I want.
  52.  
  53. You can run this bot using the `twittbot start` command. If everything works
  54. it should start tweeting at the first 25 ids right away
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement