huginn_ntfy_agent 0.1.10

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: 228e3a303e8e299a566995a3f9be2c428cb3ab2abb00e42f49a9e09f1bc02afa
4
+ data.tar.gz: 192aa95902a9e8b28ec97deee26907381f443e6cacdaa214d18b21825afb2fbc
5
+ SHA512:
6
+ metadata.gz: 9b0768fd027dcd1968911ba7b5e05601be9759911f5532a8b1e2d9bee545d0bb60519a5dd9bccb45d8a9c412d207052f7b1b1f4d6285fb519bd4c40ebb998898
7
+ data.tar.gz: 33b951940143e4fcaec740cef75ebb081b9aa82d1a0d81e33803f010f73c6d57b27bd6210ea46f327fb5719056ea2556b26cd5e8afd069c223a0f134f12486bb
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,202 @@
1
+ module Agents
2
+ class NtfyAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description <<-MD
9
+ The Ntfy Agent interacts with a Ntfy server via its API can publish messages.
10
+
11
+ The `type` can be like pubish.
12
+
13
+ The `topic` is required to publish a message.
14
+
15
+ The `message` is a Message body; set to triggered if empty or not passed
16
+
17
+ The `title` is a Message title
18
+
19
+ The `tags` is a list of tags that may or not map to emojis
20
+
21
+ The `priority` (one of: 1, 2, 3, 4, or 5) Message priority with 1=min, 3=default and 5=max
22
+
23
+ The `actions`is a JSON array (see action buttons) Custom user action buttons for notifications
24
+
25
+ The `click` is an URL ,a Website opened when notification is clicked
26
+
27
+ The `attach` is an URL of an attachment, see attach via URL
28
+
29
+ The `delay` is Timestamp or duration for delayed delivery (30min, 9am).
30
+
31
+ The `email` is an e-mail address for e-mail notifications
32
+
33
+
34
+ MD
35
+
36
+ event_description <<-MD
37
+ Events look like this:
38
+
39
+ {
40
+ "id": "XXXXXXXXXXXX",
41
+ "time": 1674661199,
42
+ "event": "message",
43
+ "topic": "test1",
44
+ "message": "Backup successful part2"
45
+ }
46
+ MD
47
+
48
+ def default_options
49
+ {
50
+ 'debug' => 'false',
51
+ 'emit_events' => 'true',
52
+ 'changes_only' => 'true',
53
+ 'server' => 'https://ntfy.sh',
54
+ 'user' => '',
55
+ 'password' => '',
56
+ 'topic' => '',
57
+ 'message' => '',
58
+ 'title' => '',
59
+ 'tags' => '',
60
+ 'priority' => '3',
61
+ 'click' => '',
62
+ 'attach' => '',
63
+ 'delay' => '',
64
+ 'actions' => '',
65
+ 'expected_receive_period_in_days' => '2',
66
+ 'type' => 'publish'
67
+ }
68
+ end
69
+
70
+ form_configurable :changes_only, type: :boolean
71
+ form_configurable :emit_events, type: :boolean
72
+ form_configurable :debug, type: :boolean
73
+ form_configurable :server, type: :string
74
+ form_configurable :user, type: :string
75
+ form_configurable :password, type: :string
76
+ form_configurable :topic, type: :string
77
+ form_configurable :message, type: :string
78
+ form_configurable :title, type: :string
79
+ form_configurable :tags, type: :string
80
+ form_configurable :priority, type: :string
81
+ form_configurable :click, type: :string
82
+ form_configurable :attach, type: :string
83
+ form_configurable :delay, type: :string
84
+ form_configurable :actions, type: :string
85
+ form_configurable :expected_receive_period_in_days, type: :string
86
+ form_configurable :type, type: :array, values: ['publish', 'subscribe']
87
+ def validate_options
88
+ errors.add(:base, "type has invalid value: should be 'publish' 'subscribe'") if interpolated['type'].present? && !%w(publish subscribe).include?(interpolated['type'])
89
+
90
+ unless options['topic'].present? || !['publish' 'subscribe'].include?(options['type'])
91
+ errors.add(:base, "topic is a required field")
92
+ end
93
+
94
+ unless options['server'].present? || !['publish' 'subscribe'].include?(options['type'])
95
+ errors.add(:base, "server is a required field")
96
+ end
97
+
98
+ unless options['message'].present? || !['publish'].include?(options['type'])
99
+ errors.add(:base, "message is a required field")
100
+ end
101
+
102
+ if options.has_key?('debug') && boolify(options['debug']).nil?
103
+ errors.add(:base, "if provided, debug must be true or false")
104
+ end
105
+
106
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
107
+ errors.add(:base, "if provided, emit_events must be true or false")
108
+ end
109
+
110
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
111
+ 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")
112
+ end
113
+ end
114
+
115
+ def working?
116
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
117
+ end
118
+
119
+ def receive(incoming_events)
120
+ incoming_events.each do |event|
121
+ interpolate_with(event) do
122
+ log event
123
+ trigger_action
124
+ end
125
+ end
126
+ end
127
+
128
+ def check
129
+ trigger_action
130
+ end
131
+
132
+ private
133
+
134
+ def log_curl_output(code,body)
135
+
136
+ log "request status : #{code}"
137
+
138
+ if interpolated['debug'] == 'true'
139
+ log "body"
140
+ log body
141
+ end
142
+
143
+ end
144
+
145
+ def subscribe
146
+
147
+ uri = URI.parse("#{interpolated['server']}/#{interpolated['topic']}/json?since=10m&poll=1")
148
+ response = Net::HTTP.get_response(uri)
149
+
150
+ log_curl_output(response.code,response.body)
151
+
152
+ if !response.body.empty?
153
+ data_lines = response.body.split("\n")
154
+ data_lines.map do |line|
155
+ create_event payload: JSON.parse(line)
156
+ end
157
+ end
158
+
159
+ end
160
+
161
+ def publish
162
+
163
+ data = {}
164
+ data["topic"] = interpolated['topic'] if interpolated['topic'].present?
165
+ data["message"] = interpolated['message'] if interpolated['message'].present?
166
+ data["title"] = interpolated['title'] if interpolated['title'].present?
167
+ data["tags"] = interpolated['tags'].split(" ") if interpolated['tags'].present?
168
+ data["priority"] = interpolated['priority'].to_i if interpolated['priority'].present?
169
+ data["attach"] = interpolated['attach'] if interpolated['attach'].present?
170
+ data["delay"] = interpolated['delay'] if interpolated['delay'].present?
171
+ data["click"] = interpolated['click'] if interpolated['click'].present?
172
+ data["actions"] = interpolated['actions'] if interpolated['actions'].present?
173
+
174
+ if interpolated['debug'] == 'true'
175
+ log "data"
176
+ log data
177
+ end
178
+
179
+ url = URI.parse(interpolated['server'])
180
+ response = HTTParty.post(url, body: data.to_json)
181
+
182
+ log_curl_output(response.code,response.body)
183
+
184
+ if interpolated['emit_events'] == 'true'
185
+ create_event payload: response.body
186
+ end
187
+
188
+ end
189
+
190
+ def trigger_action
191
+
192
+ case interpolated['type']
193
+ when "publish"
194
+ publish()
195
+ when "subscribe"
196
+ subscribe()
197
+ else
198
+ log "Error: type has an invalid value (#{type})"
199
+ end
200
+ end
201
+ end
202
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_ntfy_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_ntfy_agent/ntfy_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::NtfyAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::NtfyAgent.new.default_options
7
+ @checker = Agents::NtfyAgent.new(:name => "NtfyAgent", :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_ntfy_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.10
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-11 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_ntfy_agent.rb
64
+ - lib/huginn_ntfy_agent/ntfy_agent.rb
65
+ - spec/ntfy_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_ntfy_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/ntfy_agent_spec.rb