Last active
November 13, 2017 01:31
Ruby script to tweet with image files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'twitter' | |
require 'oauth' | |
# Consumer key, Secretの設定 | |
CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXX" | |
CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
# Access Token Key, Secretの設定 | |
ACCESS_TOKEN_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
ACCESS_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
begin | |
# Twitter.configure 設定 | |
Twitter.configure do |config| | |
config.consumer_key = CONSUMER_KEY | |
config.consumer_secret = CONSUMER_SECRET | |
config.oauth_token = ACCESS_TOKEN_KEY | |
config.oauth_token_secret = ACCESS_SECRET | |
end | |
# Twitter クラスインスタンス化 | |
client = Twitter::Client.new | |
# ツイート文設定 | |
str_out = "<ここにツイートする文章を設定>" | |
# ツイート | |
#client.update(str_out) # <= 画像添付が無い場合 | |
open("/path/to/image_file") do |img| | |
client.update_with_media(str_out, img) | |
end # <= 画像添付が有る場合rescue => e | |
STDERR.puts "[EXCEPTION] " + e.to_s | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment