huginn_twitter_publishv2_agent 0.1.1

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: 592693f79f50d6ac8a1de08a312afd25e31c1fdb8a93f5a6fe01e7de01f5f37f
4
+ data.tar.gz: 0a8d1a4cf94ced294b3fcc098c2224e06f9f4f2b7ae0779bb2db153e86b96bcc
5
+ SHA512:
6
+ metadata.gz: 69637a40ca8238e801fa6143969a289a5de32072554e8335cd7cebd5adf2e772b051d0d79817de6e27f0d9268ba907f87ff11c350411fcaaa390e40a3bb31232
7
+ data.tar.gz: fab4c4bb2ae18080920e8ca7717fff9b6a3b70c2a4d5b5e53d35eee6c140d43b293e91325d4b06d6211970a4740429c826480a89c86a6660d19cdd865a20cece
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,156 @@
1
+ module Agents
2
+ class TwitterPublishv2Agent < 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 Twitter Publishv2 Agent publishes tweets from the events it receives.
11
+
12
+ #{twitter_dependencies_missing if dependencies_missing?}
13
+
14
+ To be able to use this Agent you need to authenticate with Twitter with [twurl](https://github.com/twitter/twurl).
15
+
16
+ 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.
17
+ Additional parameters can be passed via `parameters`.
18
+
19
+ `debug` is used for verbose mode.
20
+
21
+ `consumer_key` is mandatory for oauth.
22
+
23
+ `consumer_secret` is mandatory for oauth.
24
+
25
+ `access_token` is mandatory for oauth.
26
+
27
+ `token_secret` is mandatory for oauth.
28
+
29
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
30
+ that you anticipate passing without this Agent receiving an incoming Event.
31
+ MD
32
+ end
33
+
34
+ event_description <<-MD
35
+ Events look like this:
36
+
37
+ {
38
+ "data": {
39
+ "edit_history_tweet_ids": [
40
+ "XXXXXXXXXXXXXXXXXXX"
41
+ ],
42
+ "id": "XXXXXXXXXXXXXXXXXXX",
43
+ "text": "test huginn agentv2 part3 with created event"
44
+ }
45
+ }
46
+ MD
47
+
48
+ def default_options
49
+ {
50
+ 'message' => "{{text}}",
51
+ 'debug' => 'false',
52
+ 'consumer_key' => '',
53
+ 'consumer_secret' => '',
54
+ 'access_token' => '',
55
+ 'token_secret' => '',
56
+ 'emit_events' => 'true',
57
+ 'expected_receive_period_in_days' => '2',
58
+ }
59
+ end
60
+
61
+ form_configurable :message, type: :string
62
+ form_configurable :consumer_key, type: :string
63
+ form_configurable :consumer_secret, type: :string
64
+ form_configurable :access_token, type: :string
65
+ form_configurable :token_secret, type: :string
66
+ form_configurable :debug, type: :boolean
67
+ form_configurable :emit_events, type: :boolean
68
+ form_configurable :expected_receive_period_in_days, type: :string
69
+ def validate_options
70
+ unless options['consumer_key'].present?
71
+ errors.add(:base, "consumer_key is a required field")
72
+ end
73
+
74
+ unless options['consumer_secret'].present?
75
+ errors.add(:base, "consumer_secret must be true or false")
76
+ end
77
+
78
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
79
+ errors.add(:base, "if provided, emit_events must be true or false")
80
+ end
81
+
82
+ unless options['access_token'].present?
83
+ errors.add(:base, "access_token must be true or false")
84
+ end
85
+
86
+ unless options['token_secret'].present?
87
+ errors.add(:base, "token_secret must be true or false")
88
+ end
89
+
90
+ if options.has_key?('debug') && boolify(options['debug']).nil?
91
+ errors.add(:base, "if provided, debug must be true or false")
92
+ end
93
+
94
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
95
+ 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")
96
+ end
97
+ end
98
+
99
+ def working?
100
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
101
+ end
102
+
103
+ def receive(incoming_events)
104
+ incoming_events.each do |event|
105
+ interpolate_with(event) do
106
+ log event
107
+ publish()
108
+ end
109
+ end
110
+ end
111
+
112
+ def check
113
+ publish()
114
+ end
115
+
116
+ private
117
+
118
+ def log_curl_output(code,body)
119
+
120
+ log "request status : #{code}"
121
+
122
+ if interpolated['debug'] == 'true'
123
+ log "body"
124
+ log body
125
+ end
126
+
127
+ end
128
+
129
+ def publish()
130
+
131
+ url = URI.parse('https://api.twitter.com/2/tweets')
132
+ data = { 'text' => interpolated['message'] }
133
+ json_data = data.to_json
134
+
135
+ http = Net::HTTP.new(url.host, url.port)
136
+ http.use_ssl = true
137
+
138
+ request = Net::HTTP::Post.new(url.path,
139
+ { 'Content-Type' => 'application/json' })
140
+
141
+ consumer = OAuth::Consumer.new(interpolated['consumer_key'], interpolated['consumer_secret'])
142
+ token = OAuth::AccessToken.new(consumer, interpolated['access_token'], interpolated['token_secret'])
143
+
144
+ request.oauth! http, consumer, token
145
+ request.body = json_data
146
+ response = http.request(request)
147
+
148
+ log_curl_output(response.code,response.body)
149
+
150
+ if interpolated['emit_events'] == 'true'
151
+ create_event payload: response.body
152
+ end
153
+ end
154
+
155
+ end
156
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_twitter_publishv2_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_twitter_publishv2_agent/twitter_publishv2_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::TwitterPublishv2Agent do
5
+ before(:each) do
6
+ @valid_options = Agents::TwitterPublishv2Agent.new.default_options
7
+ @checker = Agents::TwitterPublishv2Agent.new(:name => "TwitterPublishv2Agent", :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_twitter_publishv2_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-27 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_twitter_publishv2_agent.rb
64
+ - lib/huginn_twitter_publishv2_agent/twitter_publishv2_agent.rb
65
+ - spec/twitter_publishv2_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_twitter_publishv2_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/twitter_publishv2_agent_spec.rb