Giter Club home page Giter Club logo

twitter_topic_bot's Introduction

TwitterTopicBot

Gem Travis

Create a Twitter bot in 5 minutes that tweets and engages with the community on topics you're interested in!

Installation

Add this line to your application's Gemfile:

gem 'twitter_topic_bot'

And then execute:

$ bundle

Or install it yourself as:

$ gem install twitter_topic_bot

Usage

Telling Your Bot What to Tweet

To let your bot know what to tweet about, you need to make an object that responds to the three methods in the code sample below. The topic below is art, but your topic can be about anything.

class ArtContentPreparer
  def topic_string
    ['#painting', '#watercolor'].sample
  end

  def prepare_tweet
    %q{If you hear a voice within you say 'you cannot paint,' then by all means paint, and that voice will be silenced. -Vincent Van Gogh}
  end

  def prepare_reply(tweet_to_reply_to, user_to_reply_to)
    "@#{user_to_reply_to}, thank you for tweeting about art!"
  end
end

content_preparer = ArtContentPreparer.new

Getting Twitter Credentials

Next, get your Twitter API credentials ready. Register an app on Twitter with read & write permissions. Once you have your API keys, prepare the following pieces of information:

credentials = {
  username: '<Your bot\'s Twitter username (without the @ sign)>',
  consumer_key: '<Your consumer key>',
  consumer_secret: '<Your consumer secret>',
  access_token: '<Your access token>',
  access_token_secret: '<Your access token secret>'
}

Making Your Bot Interact with the World

Next, instantiate a TwitterTopicBot, and make it do things!

bot = TwitterTopicBot.new(content_preparer, credentials)

bot.tweet
bot.retweet_someone
bot.follow_someone
bot.retweet_mentions
bot.reply_to_someone
bot.follow_followers

Automating Your Bot's Activities

To make your bot run automatically on a schedule, define a schedule for it like so:

bot.schedule do |schedule|
  schedule.every('30m') { bot.tweet }
  schedule.every('3h') { bot.follow_someone }
  schedule.every('1d') { bot.reply_to_someone }
  schedule.cron('15,45 * * * *') { bot.retweet_someone }
end

View the Rufus-Scheduler documentation to see examples of how to configure the schedule.

For the schedule take effect over time, you need to keep your process open. You can add the following to your Ruby file to keep the process running indefinitely:

loop { sleep 1 }

Launching Your Bot

If your Ruby code so far is in a file called my_bot.rb, you can launch your bot with the following command in the terminal:

ruby my_bot.rb

Example File

The code above is available in this example file.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/annejohnson/twitter_topic_bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.