Socket
Book a DemoInstallSign in
Socket

twitter_tweet_bot

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twitter_tweet_bot

1.2.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

twitter_tweet_bot

Tweet Bot with Twitter's V2 API.
(by OAuth 2.0 with PCKE)

Getting Started

# Gemfile
gem 'twitter_tweet_bot'

Then run bundle install.

Usage

require 'twitter_tweet_bot'

TwitterTweetBot.post_tweet(<ACEESS_TOKEN>, 'Yeah!')
  • Configuration
  • Issue Authorization URL
  • Go to Authorization URL
  • Fetch Access Token
  • Post Tweet
Details

Step1. Configuration

require 'twitter_tweet_bot'

TwitterTweetBot.configure do |config|
  # Twitter's Bot Name (any)
  config.name = 'iambot'
  # Twitter's Client ID
  config.client_id = '*****'
  # Twitter's Client Secret
  config.client_secret = '*****'
  # Redirect URL After Authorization
  config.redirect_uri = 'https://example.com/twitter/callback'
  # Twitter's App Scopes with OAuth 2.0
  config.scopes = %w[tweet.read tweet.write users.read offline.access]
end

Step2. Issue an authorization url

authorization = TwitterTweetBot.authorize
# =>
#  #<TwitterTweetBot::Entity::Authorization
#   @code_verifier="*****",
#   @state="***",
#   @url="https://twitter.com/i/oauth2/authorize?response_type=code&redirect_uri=<YOUR_REDIRECT_URI>&client_id=<YOUR_CLIENT_ID>&scope=<SCOPES>&code_challenge=*****&code_challenge_method=S256&state=***">

Step3. Redirect (or Go) to authorization.url

And smash "Authorize app".

If authorized, redirected to your config.redirect_uri.
Check CODE in Twitter's response.

e.g. https://example.com/twitter/callback?state=***&code=*****

Step4. Fetch an access token

token = TwitterTweetBot.fetch_token('<CODE>', authorization.code_verifier)
# =>
#  #<TwitterTweetBot::Entity::Token
#   @access_token="<YOUR_ACCESS_TOKEN>",
#   @expires_in=7200,
#   @refresh_token="<YOUR_REFRESH_TOKEN>",
#   @scope="tweet.write users.read tweet.read offline.access",
#   @token_type="bearer">

Step5. Post a tweet

TwitterTweetBot.post_tweet(token.access_token, 'Yeah!')
# =>
#  #<TwitterTweetBot::Entity::Tweet
#   @edit_history_tweet_ids=["0123456789"],
#   @id="0123456789",
#   @text="Yeah!">
With some params
TwitterTweetBot.post_tweet(token.access_token, 'Yeah! Yeah!') do |params|
  params.reply = { in_reply_to_tweet_id: '*******************' }
end

Ex. Refresh an access token (required 'offline.access' in scopes)

TwitterTweetBot.refresh_token(token.refresh_token)

Caching

TwitterTweetBot can cache follow variables automatically in any store (like Rails.cache).

  • code_verifier
  • state
  • access_token
  • refresh_token

If needed, require 'twitter_tweet_bot/cache'.

require 'twitter_tweet_bot/cache'

TwitterTweetBot.post_tweet('Yeah!')
Details

Step1. Configuration

require 'twitter_tweet_bot/cache'

TwitterTweetBot.configure do |config|
  # ...

  # Any Cache Store (required `#write(key, value)` and `#read(key)` implementation).
  config.cache_provider = ActiveSupport::Cache.lookup_store(:file_store, '../tmp/cache')
end

Step2. Issue an authorization url

# `code_verifier` and `state` will be cached.
TwitterTweetBot.authorize

Step3. Fetch an access token

# `access_token` and `refresh_token` will be cached.
TwitterTweetBot.fetch_token('<CODE>')

Don't need to pass code_verifier.
(resolved from cache)

Step4. Post a tweet

TwitterTweetBot.post_tweet('Yeah!')

Don't need to pass access_token.
(resolved from cache)

Ex. Check a cache

TwitterTweetBot.client.cache.read
# =>
#  { :code_verifier=>"*****", :state=>"***", :access_token=>"*****", :refresh_token=>"*****" }

FAQs

Package last updated on 08 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.