huginn_telegrambis_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: c7c7c81808707c35ff323750e967cf4904abde90e93e628777ba2a2126e91e24
4
+ data.tar.gz: d242265a461af80acea75a986d318dabe4126cceeea2909eaa718dfa5e3a41ae
5
+ SHA512:
6
+ metadata.gz: 64cd02a0fcdb576bb2ef68f2f1aa213b9bc5508c3b38bd8666deff89ab97c126fdd5080510abfa5cc2f231a7a5e9209fc8d64ccb6d659722593b67b7ba89ff90
7
+ data.tar.gz: f5cf2d415bc53c746b0cc7fe33715f03845ef45061297b161a5238a0ba3a5b1f334ac0b3fc1c19df8bf180f72cd12227bdf64fa865ad8a3f9e6e6f2652d4df6b
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 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,183 @@
1
+ module Agents
2
+ class TelegrambisAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_12h'
7
+
8
+ description do
9
+ <<-MD
10
+ The Telegrambis Agent complements the very good telegram agent, it adds other interactions with the Telegram api.
11
+
12
+ `chat_id` for the target channel.
13
+
14
+ `message_id` is needed to select the message for possible interactions like pinned message.
15
+
16
+ `debug` is used for verbose mode.
17
+
18
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
19
+ that you anticipate passing without this Agent receiving an incoming Event.
20
+ MD
21
+ end
22
+
23
+ event_description <<-MD
24
+ Events look like this:
25
+
26
+ {
27
+ "ok": true,
28
+ "result": true
29
+ }
30
+ MD
31
+
32
+ def default_options
33
+ {
34
+ 'chat_id' => '',
35
+ 'message_id' => '',
36
+ 'debug' => 'false',
37
+ 'emit_events' => 'true',
38
+ 'expected_receive_period_in_days' => '7',
39
+ 'token' => ''
40
+ }
41
+ end
42
+
43
+ form_configurable :chat_id, type: :string
44
+ form_configurable :message_id, type: :string
45
+ form_configurable :debug, type: :boolean
46
+ form_configurable :emit_events, type: :boolean
47
+ form_configurable :token, type: :string
48
+ form_configurable :expected_receive_period_in_days, type: :string
49
+ form_configurable :type, type: :array, values: ['pin_chat_message', 'unpin_chat_message']
50
+
51
+ def validate_options
52
+ errors.add(:base, "type has invalid value: should be 'pin_chat_message', 'unpin_chat_message'") if interpolated['type'].present? && !%w(pin_chat_message unpin_chat_message).include?(interpolated['type'])
53
+
54
+ unless options['chat_id'].present? || !['pin_chat_message' 'unpin_chat_message'].include?(options['type'])
55
+ errors.add(:base, "chat_id is a required field")
56
+ end
57
+
58
+ unless options['message_id'].present? || !['pin_chat_message' 'unpin_chat_message'].include?(options['type'])
59
+ errors.add(:base, "message_id is a required field")
60
+ end
61
+
62
+ if options.has_key?('debug') && boolify(options['debug']).nil?
63
+ errors.add(:base, "if provided, debug must be true or false")
64
+ end
65
+
66
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
67
+ errors.add(:base, "if provided, emit_events must be true or false")
68
+ end
69
+
70
+ unless options['token'].present?
71
+ errors.add(:base, "token is a required field")
72
+ end
73
+
74
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
75
+ 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")
76
+ end
77
+ end
78
+
79
+ def working?
80
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
81
+ end
82
+
83
+ def receive(incoming_events)
84
+ incoming_events.each do |event|
85
+ interpolate_with(event) do
86
+ log event
87
+ trigger_action
88
+ end
89
+ end
90
+ end
91
+
92
+ def check
93
+ trigger_action
94
+ end
95
+
96
+ private
97
+
98
+ def log_curl_output(code,body)
99
+
100
+ log "request status : #{code}"
101
+
102
+ if interpolated['debug'] == 'true'
103
+ log "body"
104
+ log body
105
+ end
106
+
107
+ end
108
+
109
+ def pin_chat_message
110
+
111
+ uri = URI.parse("https://api.telegram.org/bot#{interpolated['token']}/pinChatMessage")
112
+ request = Net::HTTP::Post.new(uri)
113
+ request.content_type = "application/json"
114
+ request.body = JSON.dump({
115
+ "chat_id" => interpolated['chat_id'],
116
+ "message_id" => interpolated['message_id']
117
+ })
118
+
119
+ req_options = {
120
+ use_ssl: uri.scheme == "https",
121
+ }
122
+
123
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
124
+ http.request(request)
125
+ end
126
+
127
+ log_curl_output(response.code,response.body)
128
+
129
+ payload = JSON.parse(response.body)
130
+
131
+ if interpolated['emit_events'] == 'true'
132
+ payload['action'] = 'pinChatMessage'
133
+ payload['chat_id'] = interpolated['chat_id']
134
+ payload['message_id'] = interpolated['message_id']
135
+ create_event payload: payload
136
+ end
137
+
138
+ end
139
+
140
+ def unpin_chat_message
141
+
142
+ uri = URI.parse("https://api.telegram.org/bot#{interpolated['token']}/unpinChatMessage")
143
+ request = Net::HTTP::Post.new(uri)
144
+ request.content_type = "application/json"
145
+ request.body = JSON.dump({
146
+ "chat_id" => interpolated['chat_id'],
147
+ "message_id" => interpolated['message_id']
148
+ })
149
+
150
+ req_options = {
151
+ use_ssl: uri.scheme == "https",
152
+ }
153
+
154
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
155
+ http.request(request)
156
+ end
157
+
158
+ log_curl_output(response.code,response.body)
159
+
160
+ payload = JSON.parse(response.body)
161
+
162
+ if interpolated['emit_events'] == 'true'
163
+ payload['action'] = 'unpinChatMessage'
164
+ payload['chat_id'] = interpolated['chat_id']
165
+ payload['message_id'] = interpolated['message_id']
166
+ create_event payload: payload
167
+ end
168
+
169
+ end
170
+
171
+ def trigger_action
172
+
173
+ case interpolated['type']
174
+ when "pin_chat_message"
175
+ pin_chat_message()
176
+ when "unpin_chat_message"
177
+ unpin_chat_message()
178
+ else
179
+ log "Error: type has an invalid value (#{interpolated['type']})"
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_telegrambis_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_telegrambis_agent/telegrambis_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::TelegrambisAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::TelegrambisAgent.new.default_options
7
+ @checker = Agents::TelegrambisAgent.new(:name => "TelegrambisAgent", :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_telegrambis_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: 2024-12-28 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_telegrambis_agent.rb
64
+ - lib/huginn_telegrambis_agent/telegrambis_agent.rb
65
+ - spec/telegrambis_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_telegrambis_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/telegrambis_agent_spec.rb