Ruby で Bluesky を触る
2023/06/21
Ruby で Bluesky を触るやり方
ポスト、リポスト、リプライ、ライク
bskyrb という Gem を使えば、Bluesky での基本的な操作ができる
app.rb
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'bskyrb'
end
# 認証
credentials = Bskyrb::Credentials.new('username', 'password')
session = Bskyrb::Session.new(credentials, 'https://bsky.social')
bsky = Bskyrb::RecordManager.new(session)
# ポスト
post = bsky.create_post('Hello world from bskyrb!')
post_uri = post['uri']
# リポスト
bsky.repost(post_uri)
# リプライ
bsky.create_reply(post_uri, 'Replying to post from bskyrb')
# ライク
bsky.like(post_uri)
ストリーミングでポストを取得する
skyfall という Gem を使えば、ポストをストリーミングで取得できる
app.rb
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'skyfall'
end
sky = Skyfall::Stream.new('bsky.social', :subscribe_repos)
sky.on_message do |m|
next if m.type != :commit
m.operations.each do |op|
next unless op.action == :create && op.type == :bsky_post
puts '------------------------'
puts op.raw_record['text']
end
end
sky.on_connect { puts 'Connected' }
sky.on_disconnect { puts 'Disconnected' }
sky.on_error { |e| puts "ERROR: #{e}" }
sky.connect
sleep
$ ruby app.rb
Connected
------------------------
Ei @diviviann.bsky.social olha o que recebi (e vai ter) hoje
Porque lloras, Zuckenberg? 🤣
------------------------
😘😘❤️
------------------------
im not a US citizen but i get your point
------------------------
[超新星] +1400
ten : 2400
4 : CHO ---> MOY
------------------------
ХА! Я ВСЕ БАЧУ!!
------------------------
J.Lo spends nights in a hyperbaric chamber, her life force withdrawn intentionally from her senses to create a meditative suspended animation, while celestial beings pat the nectar of immortal bliss on her face. I’m saying she looks good, y’all!!!
------------------------
Fair enough! Thank you for the tip.
bskyrb と組み合わせて使うと好きなボットが作れる