huginn_bluesky_action_agent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 50c1c995ea565e6fd598b517b0e64629b3d821d34ff9de9fc1d6f2e4f04e77b7
4
+ data.tar.gz: 81ad011f9fa72e374fcfaf554e49bef712bd4de8fc9229c9107fb88501dd7fc0
5
+ SHA512:
6
+ metadata.gz: b5bebe8619973ed1191ee929c209114c4e287d1e1834bc9d6b393091dae1d45ad80bd520f06a7b12314f36bc563a083b25fe3d3b95d24f24594c73de54e359b1
7
+ data.tar.gz: 743d3ed97aa1c2133744573e514ec61a5ea590e792e36ab7b38c00dfb7c4e9596712c9cb0f87746e6bdcb71814e286bd6c04346a3bcda29da91de1be39925968
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,254 @@
1
+ module Agents
2
+ class BlueskyActionAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'never'
7
+
8
+ description do
9
+ <<-MD
10
+ The Bluesky Action Agent can like or repost feeds from the events it receives.
11
+
12
+ `debug` is used for verbose mode.
13
+
14
+ `like` if you want to like the feed.
15
+
16
+ `repost` if you want to repost the feed.
17
+
18
+ `uri` is mandatory to interact with the wanted feed.
19
+
20
+ `cid` is mandatory to interact with the wanted feed.
21
+
22
+ `author_did` is the author of the feed.
23
+
24
+ `handle` is mandatory for authentication.
25
+
26
+ `app_password` is mandatory for authentication.
27
+
28
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
29
+ that you anticipate passing without this Agent receiving an incoming Event.
30
+ MD
31
+ end
32
+
33
+ event_description <<-MD
34
+ Events look like this:
35
+
36
+ {
37
+ "uri": "at://did:plc:XXXXXXXXXXXXXXXXXXXXXXXX/app.bsky.feed.like/XXXXXXXXXXXXX",
38
+ "cid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
39
+ MD
40
+
41
+ def default_options
42
+ {
43
+ 'uri' => "{{ post.uri }}",
44
+ 'cid' => "{{ post.cid }}",
45
+ 'author_did' => "{{ post.author.did }}",
46
+ 'debug' => 'false',
47
+ 'repost' => 'false',
48
+ 'like' => 'true',
49
+ 'handle' => '',
50
+ 'app_password' => '',
51
+ 'emit_events' => 'true',
52
+ 'expected_receive_period_in_days' => '2',
53
+ }
54
+ end
55
+
56
+ form_configurable :uri, type: :string
57
+ form_configurable :cid, type: :string
58
+ form_configurable :author_did, type: :string
59
+ form_configurable :debug, type: :boolean
60
+ form_configurable :repost, type: :boolean
61
+ form_configurable :like, type: :boolean
62
+ form_configurable :app_password, type: :string
63
+ form_configurable :handle, type: :string
64
+ form_configurable :emit_events, type: :boolean
65
+ form_configurable :expected_receive_period_in_days, type: :string
66
+ def validate_options
67
+ unless options['uri'].present?
68
+ errors.add(:base, "uri is a required field")
69
+ end
70
+
71
+ unless options['cid'].present?
72
+ errors.add(:base, "cid is a required field")
73
+ end
74
+
75
+ unless options['author_did'].present?
76
+ errors.add(:base, "author_did is a required field")
77
+ end
78
+
79
+ unless options['app_password'].present?
80
+ errors.add(:base, "app_password is a required field")
81
+ end
82
+
83
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
84
+ errors.add(:base, "if provided, emit_events must be true or false")
85
+ end
86
+
87
+ unless options['handle'].present?
88
+ errors.add(:base, "handle is a required field")
89
+ end
90
+
91
+ if options.has_key?('debug') && boolify(options['debug']).nil?
92
+ errors.add(:base, "if provided, debug must be true or false")
93
+ end
94
+
95
+ if options.has_key?('repost') && boolify(options['repost']).nil?
96
+ errors.add(:base, "if provided, repost must be true or false")
97
+ end
98
+
99
+ if options.has_key?('like') && boolify(options['like']).nil?
100
+ errors.add(:base, "if provided, like must be true or false")
101
+ end
102
+
103
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
104
+ 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")
105
+ end
106
+ end
107
+
108
+ def working?
109
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
110
+ end
111
+
112
+ def receive(incoming_events)
113
+ incoming_events.each do |event|
114
+ interpolate_with(event) do
115
+ log event
116
+ action()
117
+ end
118
+ end
119
+ end
120
+
121
+ def check
122
+ action()
123
+ end
124
+
125
+ private
126
+
127
+ def log_curl_output(code,body)
128
+
129
+ log "request status : #{code}"
130
+
131
+ if interpolated['debug'] == 'true'
132
+ log "body"
133
+ log body
134
+ end
135
+
136
+ end
137
+
138
+ def generate_did()
139
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.identity.resolveHandle")
140
+ params = { :handle => interpolated['handle'] }
141
+ uri.query = URI.encode_www_form(params)
142
+ response = Net::HTTP.get_response(uri)
143
+
144
+ log_curl_output(response.code,response.body)
145
+
146
+ return JSON.parse(response.body)['did']
147
+ end
148
+
149
+ def generate_api_key(did)
150
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.server.createSession")
151
+ request = Net::HTTP::Post.new(uri)
152
+ request.content_type = "application/json"
153
+ request.body = JSON.dump({
154
+ "identifier" => did,
155
+ "password" => interpolated['app_password']
156
+ })
157
+
158
+ req_options = {
159
+ use_ssl: uri.scheme == "https",
160
+ }
161
+
162
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
163
+ http.request(request)
164
+ end
165
+
166
+ log_curl_output(response.code,response.body)
167
+
168
+ return JSON.parse(response.body)['accessJwt']
169
+ end
170
+
171
+ def repost_feed()
172
+
173
+ did = generate_did()
174
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.repo.createRecord")
175
+ request = Net::HTTP::Post.new(uri)
176
+ request.content_type = "application/json"
177
+ request["Authorization"] = "Bearer #{generate_api_key(did)}"
178
+ request.body = JSON.dump({
179
+ "collection" => "app.bsky.feed.repost",
180
+ "repo" => interpolated['author_did'],
181
+ "record" => {
182
+ "subject" => {
183
+ "uri" => interpolated['uri'],
184
+ "cid" => interpolated['cid']
185
+ },
186
+ "createdAt" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%3NZ'),
187
+ "$type" => "app.bsky.feed.repost"
188
+ }
189
+ })
190
+
191
+ req_options = {
192
+ use_ssl: uri.scheme == "https",
193
+ }
194
+
195
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
196
+ http.request(request)
197
+ end
198
+
199
+ log_curl_output(response.code,response.body)
200
+
201
+ if interpolated['emit_events'] == 'true'
202
+ create_event payload: response.body
203
+ end
204
+
205
+ end
206
+
207
+ def like_feed()
208
+
209
+ did = generate_did()
210
+ uri = URI.parse("https://bsky.social/xrpc/com.atproto.repo.createRecord")
211
+ request = Net::HTTP::Post.new(uri)
212
+ request.content_type = "application/json"
213
+ request["Authorization"] = "Bearer #{generate_api_key(did)}"
214
+ request.body = JSON.dump({
215
+ "collection" => "app.bsky.feed.like",
216
+ "repo" => interpolated['author_did'],
217
+ "record" => {
218
+ "subject" => {
219
+ "uri" => interpolated['uri'],
220
+ "cid" => interpolated['cid']
221
+ },
222
+ "createdAt" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%3NZ'),
223
+ "$type" => "app.bsky.feed.like"
224
+ }
225
+ })
226
+
227
+ req_options = {
228
+ use_ssl: uri.scheme == "https",
229
+ }
230
+
231
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
232
+ http.request(request)
233
+ end
234
+
235
+ log_curl_output(response.code,response.body)
236
+
237
+ if interpolated['emit_events'] == 'true'
238
+ create_event payload: response.body
239
+ end
240
+
241
+ end
242
+
243
+ def action()
244
+
245
+ if interpolated['repost'] == 'true'
246
+ repost_feed()
247
+ end
248
+ if interpolated['like'] == 'true'
249
+ like_feed()
250
+ end
251
+
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_bluesky_action_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_bluesky_action_agent/bluesky_action_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::BlueskyActionAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::BlueskyActionAgent.new.default_options
7
+ @checker = Agents::BlueskyActionAgent.new(:name => "BlueskyActionAgent", :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_action_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-16 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_action_agent.rb
64
+ - lib/huginn_bluesky_action_agent/bluesky_action_agent.rb
65
+ - spec/bluesky_action_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_bluesky_action_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_action_agent_spec.rb