huginn_bluesky_publish_agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02e26851d9cec371c5027b45661aa2bfed1e8b84d301cb34d923e9f3f98dad72
4
+ data.tar.gz: fc13342968c0c8c8cbb8ae9e5173e11cf20af6db63f264c14aadbe5fea6a0756
5
+ SHA512:
6
+ metadata.gz: 1714003be0cfb53efbd9e5fab616457ff550b811e12928b73ecf96f45d1a2c12457f570b9091f8134788f54df8ea59e1c94672d431327a19c2c58e7072e77210
7
+ data.tar.gz: 78d421e4fb52d3f79ae96b7b68dd4690fb005c68eaebe0c2766f9f41044e78cdb0bfb545cb73fb46899aec9e348b5235393b7fd5ca596cc11a387f89ad46242a
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,176 @@
1
+ module Agents
2
+ class BlueskyPublishAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description do
9
+ <<-MD
10
+ The Bluesky Publish Agent publishes posts from the events it receives.
11
+
12
+ To be able to use this Agent you need to authenticate with Twitter with [twurl](https://github.com/twitter/twurl).
13
+
14
+ You must also specify a `message` parameter, you can use [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) to format the message.
15
+ Additional parameters can be passed via `parameters`.
16
+
17
+ `debug` is used for verbose mode.
18
+
19
+ `handle` is mandatory for authentication.
20
+
21
+ `app_password` is mandatory for authentication.
22
+
23
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
24
+ that you anticipate passing without this Agent receiving an incoming Event.
25
+ MD
26
+ end
27
+
28
+ event_description <<-MD
29
+ Events look like this:
30
+
31
+ {
32
+ "uri": "at://did:plc:XXXXXXXXXXXXXXXXXXXXXXXX/app.bsky.feed.post/XXXXXXXXXXXXX",
33
+ "cid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
34
+ }
35
+ MD
36
+
37
+ def default_options
38
+ {
39
+ 'message' => "{{text}}",
40
+ 'debug' => 'false',
41
+ 'handle' => '',
42
+ 'app_password' => '',
43
+ 'emit_events' => 'true',
44
+ 'expected_receive_period_in_days' => '2',
45
+ }
46
+ end
47
+
48
+ form_configurable :message, type: :string
49
+ form_configurable :debug, type: :boolean
50
+ form_configurable :app_password, type: :string
51
+ form_configurable :handle, type: :string
52
+ form_configurable :emit_events, type: :boolean
53
+ form_configurable :expected_receive_period_in_days, type: :string
54
+ def validate_options
55
+ unless options['message'].present?
56
+ errors.add(:base, "message is a required field")
57
+ end
58
+
59
+ unless options['app_password'].present?
60
+ errors.add(:base, "app_password is a required field")
61
+ end
62
+
63
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
64
+ errors.add(:base, "if provided, emit_events must be true or false")
65
+ end
66
+
67
+ unless options['handle'].present?
68
+ errors.add(:base, "handle is a required field")
69
+ end
70
+
71
+ if options.has_key?('debug') && boolify(options['debug']).nil?
72
+ errors.add(:base, "if provided, debug must be true or false")
73
+ end
74
+
75
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
76
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
77
+ end
78
+ end
79
+
80
+ def working?
81
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
82
+ end
83
+
84
+ def receive(incoming_events)
85
+ incoming_events.each do |event|
86
+ interpolate_with(event) do
87
+ log event
88
+ publish()
89
+ end
90
+ end
91
+ end
92
+
93
+ def check
94
+ publish()
95
+ end
96
+
97
+ private
98
+
99
+ def log_curl_output(code,body)
100
+
101
+ log "request status : #{code}"
102
+
103
+ if interpolated['debug'] == 'true'
104
+ log "body"
105
+ log body
106
+ end
107
+
108
+ end
109
+
110
+ def generate_did()
111
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.identity.resolveHandle")
112
+ params = { :handle => interpolated['handle'] }
113
+ uri.query = URI.encode_www_form(params)
114
+ response = Net::HTTP.get_response(uri)
115
+
116
+ log_curl_output(response.code,response.body)
117
+
118
+ return JSON.parse(response.body)['did']
119
+ end
120
+
121
+ def generate_api_key(did)
122
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.server.createSession")
123
+ request = Net::HTTP::Post.new(uri)
124
+ request.content_type = "application/json"
125
+ request.body = JSON.dump({
126
+ "identifier" => did,
127
+ "password" => interpolated['app_password']
128
+ })
129
+
130
+ req_options = {
131
+ use_ssl: uri.scheme == "https",
132
+ }
133
+
134
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
135
+ http.request(request)
136
+ end
137
+
138
+ log_curl_output(response.code,response.body)
139
+
140
+ return JSON.parse(response.body)['accessJwt']
141
+ end
142
+
143
+ def publish()
144
+
145
+ did = generate_did()
146
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.repo.createRecord")
147
+ request = Net::HTTP::Post.new(uri)
148
+ request.content_type = "application/json"
149
+ request["Authorization"] = "Bearer #{generate_api_key(did)}"
150
+ request.body = JSON.dump({
151
+ "collection" => "app.bsky.feed.post",
152
+ "repo" => did,
153
+ "record" => {
154
+ "text" => interpolated['message'],
155
+ "createdAt" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%3NZ'),
156
+ "$type" => "app.bsky.feed.post"
157
+ }
158
+ })
159
+
160
+ req_options = {
161
+ use_ssl: uri.scheme == "https",
162
+ }
163
+
164
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
165
+ http.request(request)
166
+ end
167
+
168
+ log_curl_output(response.code,response.body)
169
+
170
+ if interpolated['emit_events'] == 'true'
171
+ create_event payload: response.body
172
+ end
173
+
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_bluesky_publish_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_bluesky_publish_agent/bluesky_publish_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::BlueskyPublishAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::BlueskyPublishAgent.new.default_options
7
+ @checker = Agents::BlueskyPublishAgent.new(:name => "BlueskyPublishAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_bluesky_publish_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_bluesky_publish_agent.rb
64
+ - lib/huginn_bluesky_publish_agent/bluesky_publish_agent.rb
65
+ - spec/bluesky_publish_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_bluesky_publish_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/bluesky_publish_agent_spec.rb