Skip to content

Commit 19fe012

Browse files
authored
update events Bot (#49)
* update events Bot * update bot publish path * adding gem for bluesky social media * update publisher to allow no setting social media * update blueSky script * remove atproton from install gem * fixing bluesky connection * fixing empty key on bluesky * fixing blueslky * refactory mastodon * update publisher and check integrations * update and compatibilize integrations and publisher * udpate post text
1 parent 3580419 commit 19fe012

File tree

6 files changed

+170
-57
lines changed

6 files changed

+170
-57
lines changed

.github/workflows/bot.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55
workflow_dispatch:
66
schedule:
7-
- cron: "0 9 * * *"
7+
- cron: "1 0 * * 0"
88

99
permissions:
1010
contents: write
@@ -25,21 +25,21 @@ jobs:
2525

2626
- run: sudo apt-get update && sudo apt-get install -y libgd-dev
2727

28-
- run: gem install ruby-libgd x
28+
- run: gem install ruby-libgd
2929

3030
- run: ruby demo/rsn_events_bot/social_event_image.rb
3131

3232
- name: Commit image
3333
if: github.ref == 'refs/heads/main'
3434
run: |
3535
git config --global user.name "rsn-bot"
36-
git config --global user.email "bot@rubystacknews.com"
36+
git config --global user.email "ggerman@gmail.com"
3737
3838
git add demo/rsn_events_bot/rsn_events.png
39-
git commit -m "update events image" || echo "no changes"
39+
git commit -m "Update Events image" || echo "no changes"
4040
git push
4141
42-
- run: ruby demo/rsn_events_bot/post_to_mastodon.rb
42+
- run: ruby demo/rsn_events_bot/publish.rb
4343
env:
4444
MASTODON_TOKEN: ${{ secrets.MASTODON_TOKEN }}
4545
MASTODON_INSTANCE: https://ruby.social

demo/rsn_events_bot/post_to_mastodon.rb

Lines changed: 0 additions & 52 deletions
This file was deleted.

demo/rsn_events_bot/publish.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require_relative "publishers/mastodon"
2+
require_relative "publishers/bluesky"
3+
require_relative "publishers/discord"
4+
5+
message = "Ruby events this week 🧵\n🤖 Generated automatically by RubyEventsBot using ruby-libgd.
6+
Updated every 7 days.\n#Ruby #RubyEvents #RubyLanguage #RubyOnRails"
7+
image = "demo/rsn_events_bot/rsn_events.png"
8+
9+
publishers = [
10+
Publishers::Mastodon.new,
11+
Publishers::Bluesky.new,
12+
Publishers::Discord.new
13+
]
14+
15+
publishers.each do |publisher|
16+
puts publisher.post(message, image) ? "x" : "."
17+
end
18+
19+
puts "Publishing finished"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require "net/http"
2+
require "json"
3+
require "time"
4+
5+
module Publishers
6+
class Bluesky
7+
def post(text, _image)
8+
user = ENV["BSKY_USER"]
9+
pass = ENV["BSKY_PASS"]
10+
11+
return unless user && pass
12+
13+
# -------------------------
14+
# login
15+
# -------------------------
16+
17+
login_uri = URI("https://bsky.social/xrpc/com.atproto.server.createSession")
18+
19+
login_body = {
20+
identifier: user,
21+
password: pass
22+
}
23+
24+
login_res = http_post(login_uri, login_body, {
25+
"Content-Type" => "application/json"
26+
})
27+
28+
return false unless login_res.code.to_i == 200
29+
30+
session = JSON.parse(login_res.body)
31+
32+
access_token = session["accessJwt"]
33+
did = session["did"]
34+
35+
# -------------------------
36+
# create post
37+
# -------------------------
38+
39+
post_uri = URI("https://bsky.social/xrpc/com.atproto.repo.createRecord")
40+
41+
post_body = {
42+
repo: did,
43+
collection: "app.bsky.feed.post",
44+
record: {
45+
text: text,
46+
createdAt: Time.now.utc.iso8601
47+
}
48+
}
49+
50+
post_res = http_post(post_uri, post_body, {
51+
"Authorization" => "Bearer #{access_token}",
52+
"Content-Type" => "application/json"
53+
})
54+
55+
post_res.code.to_i == 200
56+
end
57+
58+
private
59+
60+
def http_post(uri, body, headers = {})
61+
req = Net::HTTP::Post.new(uri)
62+
63+
headers.each { |k, v| req[k] = v }
64+
65+
req.body = body.to_json
66+
67+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
68+
http.request(req)
69+
end
70+
71+
return res.code.to_i == 200
72+
end
73+
end
74+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "net/http"
2+
require "uri"
3+
4+
module Publishers
5+
class Discord
6+
def post(text, image_path)
7+
webhook = ENV["DISCORD_WEBHOOK"]
8+
9+
exit unless webhook
10+
11+
uri = URI(webhook)
12+
req = Net::HTTP::Post.new(uri)
13+
14+
form = [
15+
["content", text],
16+
["file", File.open(image_path)]
17+
]
18+
19+
req.set_form form, "multipart/form-data"
20+
21+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
22+
http.request(req)
23+
end
24+
25+
return res.code.to_i == 200
26+
end
27+
end
28+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "net/http"
2+
require "json"
3+
4+
module Publishers
5+
class Mastodon
6+
def post(text, image_path)
7+
instance = ENV["MASTODON_INSTANCE"]
8+
token = ENV["MASTODON_TOKEN"]
9+
10+
exit unless instance && token
11+
12+
abort "Image not found" unless File.exist?(image_path)
13+
14+
uri = URI("#{instance}/api/v1/media")
15+
16+
req = Net::HTTP::Post.new(uri)
17+
req["Authorization"] = "Bearer #{token}"
18+
19+
form_data = [
20+
["file", File.open(image_path), { filename: "rsn_events.png" }]
21+
]
22+
23+
req.set_form form_data, "multipart/form-data"
24+
25+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
26+
27+
media = JSON.parse(res.body)
28+
media_id = media["id"]
29+
30+
uri = URI("#{instance}/api/v1/statuses")
31+
32+
req = Net::HTTP::Post.new(uri)
33+
req["Authorization"] = "Bearer #{token}"
34+
35+
req.set_form_data(
36+
"status" => text,
37+
"media_ids[]" => media_id
38+
)
39+
40+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
41+
return res.code.to_i == 200
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)