huginn_discord_agent 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +7 -0
- data/lib/huginn_discord_agent/discord_agent.rb +170 -0
- data/lib/huginn_discord_agent.rb +4 -0
- data/spec/discord_agent_spec.rb +13 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cc8454c86e155a8f225ddd14b9423e6e71adaded850546c683ce88a8a3f98624
|
4
|
+
data.tar.gz: dc89f166d45a8f0806b9be632080d708071dd0ec38d433af3b40514c29a7d456
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4e0d67a0f26872efcdfc114020e16fe45aebbb24db7c491254a3e2dd4e118ee0739909b5aa2bd6ef2e5c328a0bc3867a0211c7eceed5c47990b7128618a5785
|
7
|
+
data.tar.gz: 6746d69579ac0ddf6f6f07fa45566267be0b9b660628e171c986d1183ec293c5a16da3318afc87f78e9f75a7354e73c95e3906d8ce04efd2a3c82c82cc35d70e
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2020 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,170 @@
|
|
1
|
+
module Agents
|
2
|
+
class DiscordAgent < Agent
|
3
|
+
include FormConfigurable
|
4
|
+
can_dry_run!
|
5
|
+
no_bulk_receive!
|
6
|
+
|
7
|
+
description <<-MD
|
8
|
+
The Discord Agent interacts with Discord API for sending message for example.
|
9
|
+
|
10
|
+
The `type` can be like sending a message.
|
11
|
+
|
12
|
+
The `content` is needed for sending a message.
|
13
|
+
|
14
|
+
The `channel_id` is needed for sending a message to a specific channel.
|
15
|
+
|
16
|
+
The `emit_events` can create event if wanted.
|
17
|
+
|
18
|
+
The `bot_token` is needed for the authentication.
|
19
|
+
|
20
|
+
The `debug` can add verbosity.
|
21
|
+
|
22
|
+
Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
|
23
|
+
|
24
|
+
MD
|
25
|
+
|
26
|
+
|
27
|
+
event_description <<-MD
|
28
|
+
Events look like this:
|
29
|
+
|
30
|
+
{
|
31
|
+
"id": "XXXXXXXXXXXXXXXXXXX",
|
32
|
+
"type": 0,
|
33
|
+
"content": "XXXXXXXXXXXXXXXXXX",
|
34
|
+
"channel_id": "XXXXXXXXXXXXXXXXXX",
|
35
|
+
"author": {
|
36
|
+
"id": "XXXXXXXXXXXXXXXXXX",
|
37
|
+
"username": "XXXXXX",
|
38
|
+
"avatar": null,
|
39
|
+
"avatar_decoration": null,
|
40
|
+
"discriminator": "XXXX",
|
41
|
+
"public_flags": 0,
|
42
|
+
"bot": true
|
43
|
+
},
|
44
|
+
"attachments": [],
|
45
|
+
"embeds": [],
|
46
|
+
"mentions": [],
|
47
|
+
"mention_roles": [],
|
48
|
+
"pinned": false,
|
49
|
+
"mention_everyone": false,
|
50
|
+
"tts": false,
|
51
|
+
"timestamp": "2023-01-11T13:53:48.308000+00:00",
|
52
|
+
"edited_timestamp": null,
|
53
|
+
"flags": 0,
|
54
|
+
"components": [],
|
55
|
+
"referenced_message": null
|
56
|
+
}
|
57
|
+
MD
|
58
|
+
|
59
|
+
def default_options
|
60
|
+
{
|
61
|
+
'content' => '',
|
62
|
+
'type' => 'send_message',
|
63
|
+
'bot_token' => '',
|
64
|
+
'debug' => 'false',
|
65
|
+
'channel_id' => '',
|
66
|
+
'emit_events' => 'true',
|
67
|
+
'expected_receive_period_in_days' => '7'
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
form_configurable :content, type: :string
|
72
|
+
form_configurable :type, type: :array, values: ['send_message']
|
73
|
+
form_configurable :bot_token, type: :string
|
74
|
+
form_configurable :debug, type: :boolean
|
75
|
+
form_configurable :channel_id, type: :string
|
76
|
+
form_configurable :emit_events, type: :boolean
|
77
|
+
form_configurable :expected_receive_period_in_days, type: :string
|
78
|
+
|
79
|
+
def validate_options
|
80
|
+
errors.add(:base, "type has invalid value: should be 'send_message'") if interpolated['type'].present? && !%w(send_message).include?(interpolated['type'])
|
81
|
+
|
82
|
+
unless options['channel_id'].present? || !['send_message'].include?(options['type'])
|
83
|
+
errors.add(:base, "channel_id is a required field")
|
84
|
+
end
|
85
|
+
|
86
|
+
if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
|
87
|
+
errors.add(:base, "if provided, emit_events must be true or false")
|
88
|
+
end
|
89
|
+
|
90
|
+
unless options['content'].present? || !['send_message'].include?(options['type'])
|
91
|
+
errors.add(:base, "content is a required field")
|
92
|
+
end
|
93
|
+
|
94
|
+
unless options['bot_token'].present? || !['send_message'].include?(options['type'])
|
95
|
+
errors.add(:base, "bot_token is a required field")
|
96
|
+
end
|
97
|
+
|
98
|
+
if options.has_key?('debug') && boolify(options['debug']).nil?
|
99
|
+
errors.add(:base, "if provided, debug must be true or false")
|
100
|
+
end
|
101
|
+
|
102
|
+
unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
|
103
|
+
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")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def working?
|
108
|
+
event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
|
109
|
+
end
|
110
|
+
|
111
|
+
def receive(incoming_events)
|
112
|
+
incoming_events.each do |event|
|
113
|
+
interpolate_with(event) do
|
114
|
+
log event
|
115
|
+
trigger_action
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def check
|
121
|
+
trigger_action
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def log_curl_output(code,body)
|
127
|
+
|
128
|
+
log "request status : #{code}"
|
129
|
+
|
130
|
+
if interpolated['debug'] == 'true'
|
131
|
+
log "body"
|
132
|
+
log body
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
def send_message()
|
138
|
+
|
139
|
+
message = { content: interpolated['content'] }.to_json
|
140
|
+
|
141
|
+
# Build the HTTP request to send the message
|
142
|
+
uri = URI("https://discordapp.com/api/v6/channels/#{interpolated['channel_id']}/messages")
|
143
|
+
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
|
144
|
+
req['Authorization'] = "Bot #{interpolated['bot_token']}"
|
145
|
+
req.body = message
|
146
|
+
|
147
|
+
# Send the request and print the response
|
148
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
149
|
+
http.request(req)
|
150
|
+
end
|
151
|
+
|
152
|
+
log_curl_output(response.code,response.body)
|
153
|
+
|
154
|
+
if interpolated['emit_events'] == 'true'
|
155
|
+
create_event payload: response.body
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
def trigger_action
|
161
|
+
|
162
|
+
case interpolated['type']
|
163
|
+
when "send_message"
|
164
|
+
send_message()
|
165
|
+
else
|
166
|
+
log "Error: type has an invalid value (#{type})"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
require 'huginn_agent/spec_helper'
|
3
|
+
|
4
|
+
describe Agents::DiscordAgent do
|
5
|
+
before(:each) do
|
6
|
+
@valid_options = Agents::DiscordAgent.new.default_options
|
7
|
+
@checker = Agents::DiscordAgent.new(:name => "DiscordAgent", :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_discord_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_discord_agent.rb
|
64
|
+
- lib/huginn_discord_agent/discord_agent.rb
|
65
|
+
- spec/discord_agent_spec.rb
|
66
|
+
homepage: https://github.com/hihouhou/huginn_discord_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/discord_agent_spec.rb
|