ruboty-slack_events 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 +7 -0
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/Rakefile +8 -0
- data/lib/ruboty/adapters/slack_events/slack_events_handler.rb +61 -0
- data/lib/ruboty/adapters/slack_events.rb +93 -0
- data/lib/ruboty/slack_events/api/socket_client.rb +54 -0
- data/lib/ruboty/slack_events/app_info.rb +7 -0
- data/lib/ruboty/slack_events/logger.rb +32 -0
- data/lib/ruboty/slack_events/mention/rubotify.rb +34 -0
- data/lib/ruboty/slack_events/mention/slackify.rb +32 -0
- data/lib/ruboty/slack_events/resolvers/user_resolver.rb +71 -0
- data/lib/ruboty/slack_events/resolvers.rb +20 -0
- data/lib/ruboty/slack_events/version.rb +7 -0
- data/lib/ruboty/slack_events.rb +17 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c3c6b89b5885ab7eaa934f31af73aa86b58e04a2fb1ff8774ea4cdcda172d91e
|
4
|
+
data.tar.gz: 07ec6b64b4ac971d9c7d6aa9962a8ffdc8b1daa9c0f7ada47da88fd6ff3b6ffe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ad6dc3172b328f2934a2414548de2f04c57289a7063a9aa874afc17fc8002126c2424bfbd77f109be0a0c544204272126c85c327a8c06c3eb8661864eeb1c6c
|
7
|
+
data.tar.gz: a3f4ec550a4745fb62902578f04bcffe79fcc714e1f857723a8b7129996700ae7902849ae1d2d98bb535476a3ff3e998b05c0dfa89bc139227d6c63fbadc14e5
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rbs_inline
|
3
|
+
|
4
|
+
plugins:
|
5
|
+
- rubocop-rake
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
TargetRubyVersion: 3.2
|
9
|
+
NewCops: enable
|
10
|
+
|
11
|
+
Style/Documentation:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/StringLiterals:
|
15
|
+
EnforcedStyle: double_quotes
|
16
|
+
|
17
|
+
Style/StringLiteralsInInterpolation:
|
18
|
+
EnforcedStyle: double_quotes
|
19
|
+
|
20
|
+
Layout/LeadingCommentSpace:
|
21
|
+
AllowRBSInlineAnnotation: true
|
22
|
+
AllowSteepAnnotation: true
|
23
|
+
|
24
|
+
Metrics:
|
25
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Tomoya Chiba
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Ruboty::SlackEvents
|
2
|
+
|
3
|
+
Slack ([Events API](https://api.slack.com/apis/events-api)) adapter for [Ruboty](https://github.com/r7kamura/ruboty).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'ruboty-slack_events'
|
9
|
+
```
|
10
|
+
|
11
|
+
### Configure Slack App
|
12
|
+
|
13
|
+
This gem uses [Slack Events API](https://api.slack.com/apis/events-api) and its [Socket mode](https://api.slack.com/apis/socket-mode) to receive events from Slack.
|
14
|
+
|
15
|
+
1. Create a Slack App at [Slack API](https://api.slack.com/apps) with the following App manifest.
|
16
|
+
2. Generate an app-level token with `connections:write` scope from your App's Basic Information settings.
|
17
|
+
|
18
|
+
<details><summary>App manifest</summary>
|
19
|
+
|
20
|
+
(Rewrite `Demo App` to your app name)
|
21
|
+
|
22
|
+
```yaml
|
23
|
+
_metadata:
|
24
|
+
major_version: 1
|
25
|
+
display_information:
|
26
|
+
name: Demo App
|
27
|
+
features:
|
28
|
+
bot_user:
|
29
|
+
display_name: Demo App
|
30
|
+
always_online: false
|
31
|
+
oauth_config:
|
32
|
+
scopes:
|
33
|
+
bot:
|
34
|
+
- channels:history
|
35
|
+
- users:read
|
36
|
+
- chat:write
|
37
|
+
settings:
|
38
|
+
event_subscriptions:
|
39
|
+
bot_events:
|
40
|
+
- message.channels
|
41
|
+
interactivity:
|
42
|
+
is_enabled: true
|
43
|
+
org_deploy_enabled: false
|
44
|
+
socket_mode_enabled: true
|
45
|
+
token_rotation_enabled: false
|
46
|
+
```
|
47
|
+
|
48
|
+
</details>
|
49
|
+
|
50
|
+
For details of configurations for Socket Mode, see: https://api.slack.com/apis/socket-mode.
|
51
|
+
|
52
|
+
### ENV
|
53
|
+
|
54
|
+
- `SLACK_TOKEN`: Bot token of your Slack App.
|
55
|
+
- `SLACK_APP_TOKEN`: App-level token of your Slack App.
|
56
|
+
- `SLACK_IGNORE_BOT_MESSAGE`: If this has truthy value, bot ignores messages by bots (optional)
|
57
|
+
- `SLACK_AUTO_RECONNECT`: If this has truthy value, reconnect websocket automatically (optional)
|
58
|
+
|
59
|
+
For details of token types, See: https://api.slack.com/concepts/token-types
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tomoasleep/ruboty-slack_events. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tomoasleep/ruboty-slack_events/blob/main/CODE_OF_CONDUCT.md).
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
68
|
+
|
69
|
+
## Code of Conduct
|
70
|
+
|
71
|
+
Everyone interacting in the Ruboty::SlackEvents project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tomoasleep/ruboty-slack_events/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module Adapters
|
5
|
+
class SlackEvents
|
6
|
+
# Handle events from Slack Events API
|
7
|
+
class SlackEventsHandler
|
8
|
+
attr_reader :adapter #: Ruboty::Adapters::SlackEvents
|
9
|
+
|
10
|
+
# @rbs adapter: Ruboty::Adapters::SlackEvents
|
11
|
+
def initialize(adapter)
|
12
|
+
@adapter = adapter
|
13
|
+
end
|
14
|
+
|
15
|
+
# @rbs message: Slack::Messages::Message -- Event payload of Slack Events API
|
16
|
+
def handle_event(message) #: void
|
17
|
+
Ruboty::SlackEvents::Logger.debug("handle_event") { message.to_json }
|
18
|
+
|
19
|
+
case message.type
|
20
|
+
when "message"
|
21
|
+
on_message(message)
|
22
|
+
when "events_api"
|
23
|
+
on_events_api(message)
|
24
|
+
when "event_callback"
|
25
|
+
on_event_callback(message)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# @rbs message: Slack::Messages::Message -- `message` type event
|
30
|
+
def on_message(message) #: void
|
31
|
+
user_id = message.user || message.bot_id
|
32
|
+
user = user_id && adapter.resolvers.user_resolver.user_info_by_id(user_id)
|
33
|
+
channel = message.channel
|
34
|
+
|
35
|
+
return if message.subtype == "bot_message" && adapter.ignore_bot_message?
|
36
|
+
|
37
|
+
message_for_robot = {
|
38
|
+
body: adapter.rubotify.call(message.text),
|
39
|
+
from: channel,
|
40
|
+
from_name: user&.name,
|
41
|
+
to: channel
|
42
|
+
} #: RobotMessage
|
43
|
+
|
44
|
+
Ruboty::SlackEvents::Logger.debug("robot.receive") { message_for_robot.to_json }
|
45
|
+
|
46
|
+
adapter.robot.receive(message_for_robot)
|
47
|
+
end
|
48
|
+
|
49
|
+
# @rbs message: Slack::Messages::Message -- `event_callback` type event
|
50
|
+
def on_event_callback(message)
|
51
|
+
handle_event(message.event)
|
52
|
+
end
|
53
|
+
|
54
|
+
# @rbs message: Slack::Messages::Message -- `events_api` type event
|
55
|
+
def on_events_api(message)
|
56
|
+
handle_event(message.payload)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ruboty"
|
4
|
+
|
5
|
+
module Ruboty
|
6
|
+
module Adapters
|
7
|
+
class SlackEvents < Base
|
8
|
+
env :SLACK_TOKEN, "Bot token. Ref: https://api.slack.com/concepts/token-types#bot"
|
9
|
+
env :SLACK_APP_TOKEN, "App-level token. Ref: https://api.slack.com/concepts/token-types#app-level"
|
10
|
+
env :SLACK_IGNORE_BOT_MESSAGE, "If this has truthy value, bot ignores messages by bots", optional: true
|
11
|
+
env :SLACK_AUTO_RECONNECT, "If this has truthy value, reconnect websocket automatically", optional: true
|
12
|
+
|
13
|
+
# @rbs!
|
14
|
+
# type robotMessage = {
|
15
|
+
# body: String,
|
16
|
+
# from: String,
|
17
|
+
# from_name: String,
|
18
|
+
# to: String,
|
19
|
+
# }
|
20
|
+
|
21
|
+
def run
|
22
|
+
init
|
23
|
+
connect
|
24
|
+
end
|
25
|
+
|
26
|
+
# @rbs message: robotMessage
|
27
|
+
def say(message)
|
28
|
+
Ruboty::SlackEvents::Logger.debug("say") { message.to_json }
|
29
|
+
|
30
|
+
text =
|
31
|
+
if message[:code]
|
32
|
+
<<~MARKDOWN
|
33
|
+
```
|
34
|
+
#{message[:code]}
|
35
|
+
```
|
36
|
+
MARKDOWN
|
37
|
+
else
|
38
|
+
slackify.call(message[:body])
|
39
|
+
end
|
40
|
+
|
41
|
+
slack_client.chat_postMessage(
|
42
|
+
channel: message[:to],
|
43
|
+
attachments: [],
|
44
|
+
markdown_text: text
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def slack_client #: Slack::Web::Client
|
49
|
+
@slack_client ||= ::Slack::Web::Client.new(token: ENV.fetch("SLACK_TOKEN", nil))
|
50
|
+
end
|
51
|
+
|
52
|
+
def resolvers #: ::Ruboty::SlackEvents::UserRepository
|
53
|
+
@resolvers ||= ::Ruboty::SlackEvents::Resolvers.new(slack_client:)
|
54
|
+
end
|
55
|
+
|
56
|
+
def slackify #: ::Ruboty::SlackEvents::Mention::Slackify
|
57
|
+
@slackify ||= ::Ruboty::SlackEvents::Mention::Slackify.new(resolvers:)
|
58
|
+
end
|
59
|
+
|
60
|
+
def rubotify #: ::Ruboty::SlackEvents::Mention::Rubotify
|
61
|
+
@rubotify ||= ::Ruboty::SlackEvents::Mention::Rubotify.new(resolvers:)
|
62
|
+
end
|
63
|
+
|
64
|
+
def ignore_bot_message?
|
65
|
+
[nil, "", "false"].none?(ENV.fetch("SLACK_IGNORE_BOT_MESSAGE", nil))
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def init
|
71
|
+
response = slack_client.auth_test
|
72
|
+
|
73
|
+
ENV["RUBOTY_NAME"] ||= response["user"]
|
74
|
+
end
|
75
|
+
|
76
|
+
def connect #: void
|
77
|
+
# Socket mode requires app-level token. so, construct another client with app-level token.
|
78
|
+
# Ref: https://api.slack.com/methods/apps.connections.open
|
79
|
+
slack_app_client = ::Slack::Web::Client.new(token: ENV.fetch("SLACK_APP_TOKEN", nil))
|
80
|
+
|
81
|
+
handler = SlackEventsHandler.new(self)
|
82
|
+
auto_reconnect = [nil, "", "false"].none?(ENV.fetch("SLACK_AUTO_RECONNECT", nil))
|
83
|
+
|
84
|
+
::Ruboty::SlackEvents::Api::SocketClient.new(slack_client: slack_app_client,
|
85
|
+
auto_reconnect:).connect do |message|
|
86
|
+
handler.handle_event(message)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
require_relative "slack_events/slack_events_handler"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "async"
|
4
|
+
require "async/http/endpoint"
|
5
|
+
require "async/websocket/client"
|
6
|
+
|
7
|
+
require "slack-ruby-client"
|
8
|
+
require "json"
|
9
|
+
|
10
|
+
module Ruboty
|
11
|
+
module SlackEvents
|
12
|
+
module Api
|
13
|
+
class SocketClient
|
14
|
+
attr_reader :slack_client #: Slack::Web::Client
|
15
|
+
|
16
|
+
# @rbs slack_client: Slack::Web::Client
|
17
|
+
def initialize(slack_client:, auto_reconnect: false)
|
18
|
+
@slack_client = slack_client
|
19
|
+
@auto_reconnect = auto_reconnect
|
20
|
+
end
|
21
|
+
|
22
|
+
def connect(&callback) #: Async::Task
|
23
|
+
res = slack_client.apps_connections_open #: Slack::Messages::Message
|
24
|
+
raise res.error unless res.ok
|
25
|
+
|
26
|
+
url = res.url
|
27
|
+
endpoint = Async::HTTP::Endpoint.parse(url)
|
28
|
+
|
29
|
+
Async do
|
30
|
+
Async::WebSocket::Client.connect(endpoint) do |connection|
|
31
|
+
while (payload = connection.read) #: Protocol::WebSocket::TextMessage
|
32
|
+
response = payload.to_h
|
33
|
+
message = Slack::Messages::Message.new(response)
|
34
|
+
|
35
|
+
if message.envelope_id
|
36
|
+
# https://api.slack.com/apis/socket-mode#acknowledge
|
37
|
+
ack_message = Protocol::WebSocket::TextMessage.generate({ envelope_id: message.envelope_id })
|
38
|
+
ack_message.send(connection)
|
39
|
+
connection.flush
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
callback&.call(message)
|
44
|
+
rescue StandardError => e
|
45
|
+
SlackEvents::Logger.error("UnhandledError on socket API callback") { e.full_message }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "logger"
|
4
|
+
require "forwardable"
|
5
|
+
|
6
|
+
module Ruboty
|
7
|
+
module SlackEvents
|
8
|
+
module Logger
|
9
|
+
class << self
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
def instance
|
13
|
+
@instance ||= ::Logger.new($stderr, log_level)
|
14
|
+
end
|
15
|
+
|
16
|
+
def log_level #: Integer
|
17
|
+
case ENV.fetch("DEBUG", nil)
|
18
|
+
when "1", "true"
|
19
|
+
::Logger::Severity::DEBUG
|
20
|
+
when "2", "info"
|
21
|
+
::Logger::Severity::INFO
|
22
|
+
else
|
23
|
+
::Logger::Severity::WARN
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
delegate %i[debug? info? warn? error? fatal?] => :instance
|
28
|
+
delegate %i[debug info warn error fatal] => :instance
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module SlackEvents
|
5
|
+
module Mention
|
6
|
+
class Rubotify
|
7
|
+
attr_reader :resolvers #: Resolvers
|
8
|
+
|
9
|
+
# @rbs resolvers: Resolvers
|
10
|
+
def initialize(resolvers:)
|
11
|
+
@resolvers = resolvers
|
12
|
+
end
|
13
|
+
|
14
|
+
# @rbs text: String
|
15
|
+
def call(text) #: String
|
16
|
+
replace_user_mentions(text)
|
17
|
+
end
|
18
|
+
|
19
|
+
# @rbs text: String
|
20
|
+
def replace_user_mentions(text) #: String
|
21
|
+
text.gsub(/<@(?<user_id>\w+)>/) do |user_mention|
|
22
|
+
user_id = Regexp.last_match[:user_id]
|
23
|
+
|
24
|
+
user_info = resolvers.user_resolver.user_info_by_id(user_id)
|
25
|
+
|
26
|
+
next user_mention unless user_info
|
27
|
+
|
28
|
+
"@#{user_info.name}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module SlackEvents
|
5
|
+
module Mention
|
6
|
+
class Slackify
|
7
|
+
attr_reader :resolvers #: Resolvers
|
8
|
+
|
9
|
+
# @rbs resolvers: Resolvers
|
10
|
+
def initialize(resolvers:)
|
11
|
+
@resolvers = resolvers
|
12
|
+
end
|
13
|
+
|
14
|
+
# @rbs text: String
|
15
|
+
def call(text) #: String
|
16
|
+
replace_user_mentions(text)
|
17
|
+
end
|
18
|
+
|
19
|
+
# @rbs text: String
|
20
|
+
def replace_user_mentions(text) #: String
|
21
|
+
text.gsub(/@(?<user_name>\w+)/) do |user_mention|
|
22
|
+
user_name = Regexp.last_match[:user_name]
|
23
|
+
user_info = resolvers.user_resolver.user_info_by_name(user_name)
|
24
|
+
next user_mention unless user_info
|
25
|
+
|
26
|
+
"<@#{user_info.id}>"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module SlackEvents
|
5
|
+
class Resolvers
|
6
|
+
class UserResolver
|
7
|
+
attr_reader :slack_client #: Slack::Web::Client
|
8
|
+
|
9
|
+
# @rbs slack_client: Slack::Web::Client
|
10
|
+
def initialize(slack_client:)
|
11
|
+
@slack_client = slack_client
|
12
|
+
|
13
|
+
@user_caches = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# @rbs!
|
17
|
+
# # Full fields are described in https://api.slack.com/methods/users.info
|
18
|
+
# type userInfo = {
|
19
|
+
# id: String,
|
20
|
+
# name: String,
|
21
|
+
# real_name: String,
|
22
|
+
# is_bot: boolish,
|
23
|
+
# is_app_user: boolish,
|
24
|
+
# }
|
25
|
+
|
26
|
+
# Calls user_info API with cache.
|
27
|
+
# @rbs user_id: String
|
28
|
+
def user_info_by_id(user_id) #: userInfo?
|
29
|
+
return nil if user_id.nil?
|
30
|
+
|
31
|
+
ensure_users
|
32
|
+
|
33
|
+
@user_caches[user_id] ||= begin
|
34
|
+
Logger.debug { "Slack API: users.info(#{user_id})" }
|
35
|
+
res = slack_client.users_info(user: user_id)
|
36
|
+
res.user
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# @rbs user_name: String
|
41
|
+
def user_info_by_name(user_name) #: userInfo?
|
42
|
+
return nil if user_name.nil?
|
43
|
+
|
44
|
+
ensure_users
|
45
|
+
|
46
|
+
@user_caches.find do |_, user|
|
47
|
+
user.name == user_name
|
48
|
+
end&.last
|
49
|
+
end
|
50
|
+
|
51
|
+
# Clears all user caches.
|
52
|
+
def forget #: void
|
53
|
+
@user_caches.clear
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def ensure_users
|
59
|
+
return unless @user_caches.empty?
|
60
|
+
|
61
|
+
Logger.debug { "Slack API: users.list()" }
|
62
|
+
res = slack_client.users_list
|
63
|
+
|
64
|
+
res.members.each do |user|
|
65
|
+
@user_caches[user.id] = user
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module SlackEvents
|
5
|
+
class Resolvers
|
6
|
+
attr_reader :slack_client #: ::Slack::Web::Client
|
7
|
+
|
8
|
+
# @rbs slack_client: ::Slack::Web::Client
|
9
|
+
def initialize(slack_client:)
|
10
|
+
@slack_client = slack_client
|
11
|
+
end
|
12
|
+
|
13
|
+
def user_resolver
|
14
|
+
@user_resolver ||= UserResolver.new(slack_client:)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
require_relative "resolvers/user_resolver"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module SlackEvents
|
5
|
+
class Error < StandardError; end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
require_relative "slack_events/version"
|
10
|
+
|
11
|
+
require_relative "slack_events/api/socket_client"
|
12
|
+
require_relative "slack_events/mention/slackify"
|
13
|
+
require_relative "slack_events/mention/rubotify"
|
14
|
+
require_relative "slack_events/resolvers"
|
15
|
+
require_relative "slack_events/logger"
|
16
|
+
|
17
|
+
require "ruboty/adapters/slack_events"
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruboty-slack_events
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomoya Chiba
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-04-01 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: async-websocket
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0.25'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0.25'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: ruboty
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.4
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.1.4
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: slack-ruby-client
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.5'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.5'
|
54
|
+
description: Ruboty adapter with Slack Events API
|
55
|
+
email:
|
56
|
+
- tomo.asleep@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".rubocop.yml"
|
62
|
+
- CHANGELOG.md
|
63
|
+
- CODE_OF_CONDUCT.md
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/ruboty/adapters/slack_events.rb
|
68
|
+
- lib/ruboty/adapters/slack_events/slack_events_handler.rb
|
69
|
+
- lib/ruboty/slack_events.rb
|
70
|
+
- lib/ruboty/slack_events/api/socket_client.rb
|
71
|
+
- lib/ruboty/slack_events/app_info.rb
|
72
|
+
- lib/ruboty/slack_events/logger.rb
|
73
|
+
- lib/ruboty/slack_events/mention/rubotify.rb
|
74
|
+
- lib/ruboty/slack_events/mention/slackify.rb
|
75
|
+
- lib/ruboty/slack_events/resolvers.rb
|
76
|
+
- lib/ruboty/slack_events/resolvers/user_resolver.rb
|
77
|
+
- lib/ruboty/slack_events/version.rb
|
78
|
+
homepage: https://github.com/tomoasleep/ruboty-slack_events
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata:
|
82
|
+
homepage_uri: https://github.com/tomoasleep/ruboty-slack_events
|
83
|
+
source_code_uri: https://github.com/tomoasleep/ruboty-slack_events
|
84
|
+
changelog_uri: https://github.com/tomoasleep/ruboty-slack_events/blob/main/CHANGELOG.md
|
85
|
+
rubygems_mfa_required: 'true'
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 3.2.0
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubygems_version: 3.6.2
|
101
|
+
specification_version: 4
|
102
|
+
summary: Ruboty adapter with Slack Events API
|
103
|
+
test_files: []
|