rubin-squarebot 0.7.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.1
2
+ Name: rubin-squarebot
3
+ Version: 0.7.0
4
+ Summary: Client library for the Rubin Observatory Squarebot
5
+ License: The MIT License (MIT)
6
+
7
+ Copyright (c) 2018-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+
27
+ Project-URL: Homepage, https://github.com/lsst-sqre/squarebot
28
+ Project-URL: Source, https://github.com/lsst-sqre/squarebot
29
+ Keywords: rubin,lsst
30
+ Classifier: Development Status :: 4 - Beta
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Programming Language :: Python
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: Programming Language :: Python :: 3.11
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: Natural Language :: English
37
+ Classifier: Operating System :: POSIX
38
+ Classifier: Typing :: Typed
39
+ Requires-Python: >=3.11
40
+ Description-Content-Type: text/markdown
41
+ Provides-Extra: dev
42
+ License-File: LICENSE
43
+
44
+ # Squarebot
45
+
46
+ Squarebot is the nexus service for ChatOps and event-driven bots for Rubin Observatory.
47
+ As a Slack app, Squarebot receives events from Slack and publishes them into specific _Square Events_ Kafka topics in the [Roundtable Kubernetes cluster](https://phalanx.lsst.io/environments/roundtable-prod/index.html).
48
+ Other applications on Roundtable can consume these events and act on them, such as by posting messages back to Slack or by performing some other background automation.
49
+
50
+ ```mermaid
51
+ flowchart LR
52
+ subgraph Slack
53
+ message[Slack message]
54
+ interaction[Slack interaction]
55
+ end
56
+ subgraph sqrbot ["Squarebot"]
57
+ eventapi["/slack/event"]
58
+ interactionapi["/slack/interaction"]
59
+ message -->|HTTP POST| eventapi
60
+ interaction -->|HTTP POST| interactionapi
61
+ end
62
+ subgraph kafka ["Kafka Topics"]
63
+ topicmention["lsst.square-events.squarebot.slack.app.mention"]
64
+ topicchannel["lsst.square-events.squarebot.slack.message.channel"]
65
+ topicgroup["lsst.square-events.squarebot.slack.message.group"]
66
+ topicim["lsst.square-events.squarebot.slack.message.im"]
67
+ topicmpim["lsst.square-events.squarebot.slack.message.mpim"]
68
+ topicinteraction["lsst.square-events.squarebot.slack.interaction"]
69
+ end
70
+ eventapi --> topicmention
71
+ eventapi --> topicchannel
72
+ eventapi --> topicgroup
73
+ eventapi --> topicim
74
+ eventapi --> topicmpim
75
+ interactionapi --> topicinteraction
76
+ subgraph backends ["Backends"]
77
+ backend1["Backend 1"]
78
+ backend2["Backend 2"]
79
+ end
80
+ topicchannel --> backend1
81
+ topicgroup --> backend1
82
+ topicmention --> backend2
83
+ topicinteraction --> backend2
84
+ ```
85
+
86
+ Slack integration is implemented at the time.
87
+ We plan to add support for other event sources, such as GitHub, in the future.
88
+
89
+ Squarebot is built on top of [FastAPI](https://fastapi.tiangolo.com/), a modern Python web framework, with Rubin/SQuaRE libraries [Safir](https://safir.lsst.io) (FastAPI application libraries) and [Kafkit](https://kafkit.lsst.io) (Kafka integration).
@@ -0,0 +1,46 @@
1
+ # Squarebot
2
+
3
+ Squarebot is the nexus service for ChatOps and event-driven bots for Rubin Observatory.
4
+ As a Slack app, Squarebot receives events from Slack and publishes them into specific _Square Events_ Kafka topics in the [Roundtable Kubernetes cluster](https://phalanx.lsst.io/environments/roundtable-prod/index.html).
5
+ Other applications on Roundtable can consume these events and act on them, such as by posting messages back to Slack or by performing some other background automation.
6
+
7
+ ```mermaid
8
+ flowchart LR
9
+ subgraph Slack
10
+ message[Slack message]
11
+ interaction[Slack interaction]
12
+ end
13
+ subgraph sqrbot ["Squarebot"]
14
+ eventapi["/slack/event"]
15
+ interactionapi["/slack/interaction"]
16
+ message -->|HTTP POST| eventapi
17
+ interaction -->|HTTP POST| interactionapi
18
+ end
19
+ subgraph kafka ["Kafka Topics"]
20
+ topicmention["lsst.square-events.squarebot.slack.app.mention"]
21
+ topicchannel["lsst.square-events.squarebot.slack.message.channel"]
22
+ topicgroup["lsst.square-events.squarebot.slack.message.group"]
23
+ topicim["lsst.square-events.squarebot.slack.message.im"]
24
+ topicmpim["lsst.square-events.squarebot.slack.message.mpim"]
25
+ topicinteraction["lsst.square-events.squarebot.slack.interaction"]
26
+ end
27
+ eventapi --> topicmention
28
+ eventapi --> topicchannel
29
+ eventapi --> topicgroup
30
+ eventapi --> topicim
31
+ eventapi --> topicmpim
32
+ interactionapi --> topicinteraction
33
+ subgraph backends ["Backends"]
34
+ backend1["Backend 1"]
35
+ backend2["Backend 2"]
36
+ end
37
+ topicchannel --> backend1
38
+ topicgroup --> backend1
39
+ topicmention --> backend2
40
+ topicinteraction --> backend2
41
+ ```
42
+
43
+ Slack integration is implemented at the time.
44
+ We plan to add support for other event sources, such as GitHub, in the future.
45
+
46
+ Squarebot is built on top of [FastAPI](https://fastapi.tiangolo.com/), a modern Python web framework, with Rubin/SQuaRE libraries [Safir](https://safir.lsst.io) (FastAPI application libraries) and [Kafkit](https://kafkit.lsst.io) (Kafka integration).
@@ -0,0 +1,130 @@
1
+ [project]
2
+ # https://packaging.python.org/en/latest/specifications/declaring-project-metadata/
3
+ name = "rubin-squarebot"
4
+ description = "Client library for the Rubin Observatory Squarebot"
5
+ license = { file = "LICENSE" }
6
+ readme = "README.md"
7
+ keywords = ["rubin", "lsst"]
8
+ # https://pypi.org/classifiers/
9
+ classifiers = [
10
+ "Development Status :: 4 - Beta",
11
+ "License :: OSI Approved :: MIT License",
12
+ "Programming Language :: Python",
13
+ "Programming Language :: Python :: 3",
14
+ "Programming Language :: Python :: 3.11",
15
+ "Intended Audience :: Developers",
16
+ "Natural Language :: English",
17
+ "Operating System :: POSIX",
18
+ "Typing :: Typed",
19
+ ]
20
+ requires-python = ">=3.11"
21
+ dependencies = ["pydantic", "safir"]
22
+ dynamic = ["version"]
23
+
24
+ [project.optional-dependencies]
25
+ dev = [
26
+ # Testing
27
+ "mypy",
28
+ ]
29
+
30
+ [project.urls]
31
+ Homepage = "https://github.com/lsst-sqre/squarebot"
32
+ Source = "https://github.com/lsst-sqre/squarebot"
33
+
34
+ [build-system]
35
+ requires = ["setuptools>=61", "wheel", "setuptools_scm[toml]>=6.2"]
36
+ build-backend = "setuptools.build_meta"
37
+
38
+ [tool.setuptools.packages.find]
39
+ where = ["src"] # for namespace package discovery
40
+
41
+ [tool.setuptools_scm]
42
+ root = "../"
43
+
44
+ [tool.coverage.run]
45
+ parallel = true
46
+ branch = true
47
+ source = ["rubinobs.square.squarebot"]
48
+
49
+ [tool.coverage.paths]
50
+ source = ["src", ".tox/*/site-packages"]
51
+
52
+ [tool.coverage.report]
53
+ show_missing = true
54
+ exclude_lines = [
55
+ "pragma: no cover",
56
+ "def __repr__",
57
+ "if self.debug:",
58
+ "if settings.DEBUG",
59
+ "raise AssertionError",
60
+ "raise NotImplementedError",
61
+ "if 0:",
62
+ "if __name__ == .__main__.:",
63
+ "if TYPE_CHECKING:",
64
+ ]
65
+
66
+ [tool.black]
67
+ line-length = 79
68
+ exclude = '''
69
+ /(
70
+ \.eggs
71
+ | \.git
72
+ | \.mypy_cache
73
+ | \.tox
74
+ | \.venv
75
+ | _build
76
+ | build
77
+ | dist
78
+ )/
79
+ '''
80
+ # Use single-quoted strings so TOML treats the string like a Python r-string
81
+ # Multi-line strings are implicitly treated by black as regular expressions
82
+
83
+ [tool.pydocstyle]
84
+ # Reference: http://www.pydocstyle.org/en/stable/error_codes.html
85
+ convention = "numpy"
86
+ add_select = [
87
+ "D212", # Multi-line docstring summary should start at the first line
88
+ ]
89
+ add-ignore = [
90
+ "D105", # Missing docstring in magic method
91
+ "D102", # Missing docstring in public method (needed for docstring inheritance)
92
+ "D100", # Missing docstring in public module
93
+ # Below are required to allow multi-line summaries.
94
+ "D200", # One-line docstring should fit on one line with quotes
95
+ "D205", # 1 blank line required between summary line and description
96
+ "D400", # First line should end with a period
97
+ # Properties shouldn't be written in imperative mode. This will be fixed
98
+ # post 6.1.1, see https://github.com/PyCQA/pydocstyle/pull/546
99
+ "D401",
100
+ ]
101
+
102
+ [tool.isort]
103
+ profile = "black"
104
+ line_length = 79
105
+ # This tox config might be used for the server isort with pre-commit; so add safir demo as a firstparty
106
+ known_first_party = ["rubinobs.square.squarebot", "squarebot", "tests"]
107
+ skip = ["docs/conf.py"]
108
+
109
+ [tool.pytest.ini_options]
110
+ asyncio_mode = "strict"
111
+ python_files = ["tests/*.py", "tests/*/*.py"]
112
+
113
+ [tool.mypy]
114
+ disallow_untyped_defs = true
115
+ disallow_incomplete_defs = true
116
+ ignore_missing_imports = true
117
+ local_partial_types = true
118
+ plugins = ["pydantic.mypy"]
119
+ no_implicit_reexport = true
120
+ show_error_codes = true
121
+ strict_equality = true
122
+ warn_redundant_casts = true
123
+ warn_unreachable = true
124
+ warn_unused_ignores = true
125
+
126
+ [tool.pydantic-mypy]
127
+ init_forbid_extra = true
128
+ init_typed = true
129
+ warn_required_dynamic_aliases = true
130
+ warn_untyped_fields = true
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.1
2
+ Name: rubin-squarebot
3
+ Version: 0.7.0
4
+ Summary: Client library for the Rubin Observatory Squarebot
5
+ License: The MIT License (MIT)
6
+
7
+ Copyright (c) 2018-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+
27
+ Project-URL: Homepage, https://github.com/lsst-sqre/squarebot
28
+ Project-URL: Source, https://github.com/lsst-sqre/squarebot
29
+ Keywords: rubin,lsst
30
+ Classifier: Development Status :: 4 - Beta
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Programming Language :: Python
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: Programming Language :: Python :: 3.11
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: Natural Language :: English
37
+ Classifier: Operating System :: POSIX
38
+ Classifier: Typing :: Typed
39
+ Requires-Python: >=3.11
40
+ Description-Content-Type: text/markdown
41
+ Provides-Extra: dev
42
+ License-File: LICENSE
43
+
44
+ # Squarebot
45
+
46
+ Squarebot is the nexus service for ChatOps and event-driven bots for Rubin Observatory.
47
+ As a Slack app, Squarebot receives events from Slack and publishes them into specific _Square Events_ Kafka topics in the [Roundtable Kubernetes cluster](https://phalanx.lsst.io/environments/roundtable-prod/index.html).
48
+ Other applications on Roundtable can consume these events and act on them, such as by posting messages back to Slack or by performing some other background automation.
49
+
50
+ ```mermaid
51
+ flowchart LR
52
+ subgraph Slack
53
+ message[Slack message]
54
+ interaction[Slack interaction]
55
+ end
56
+ subgraph sqrbot ["Squarebot"]
57
+ eventapi["/slack/event"]
58
+ interactionapi["/slack/interaction"]
59
+ message -->|HTTP POST| eventapi
60
+ interaction -->|HTTP POST| interactionapi
61
+ end
62
+ subgraph kafka ["Kafka Topics"]
63
+ topicmention["lsst.square-events.squarebot.slack.app.mention"]
64
+ topicchannel["lsst.square-events.squarebot.slack.message.channel"]
65
+ topicgroup["lsst.square-events.squarebot.slack.message.group"]
66
+ topicim["lsst.square-events.squarebot.slack.message.im"]
67
+ topicmpim["lsst.square-events.squarebot.slack.message.mpim"]
68
+ topicinteraction["lsst.square-events.squarebot.slack.interaction"]
69
+ end
70
+ eventapi --> topicmention
71
+ eventapi --> topicchannel
72
+ eventapi --> topicgroup
73
+ eventapi --> topicim
74
+ eventapi --> topicmpim
75
+ interactionapi --> topicinteraction
76
+ subgraph backends ["Backends"]
77
+ backend1["Backend 1"]
78
+ backend2["Backend 2"]
79
+ end
80
+ topicchannel --> backend1
81
+ topicgroup --> backend1
82
+ topicmention --> backend2
83
+ topicinteraction --> backend2
84
+ ```
85
+
86
+ Slack integration is implemented at the time.
87
+ We plan to add support for other event sources, such as GitHub, in the future.
88
+
89
+ Squarebot is built on top of [FastAPI](https://fastapi.tiangolo.com/), a modern Python web framework, with Rubin/SQuaRE libraries [Safir](https://safir.lsst.io) (FastAPI application libraries) and [Kafkit](https://kafkit.lsst.io) (Kafka integration).
@@ -0,0 +1,14 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ tox.ini
5
+ src/rubin_squarebot.egg-info/PKG-INFO
6
+ src/rubin_squarebot.egg-info/SOURCES.txt
7
+ src/rubin_squarebot.egg-info/dependency_links.txt
8
+ src/rubin_squarebot.egg-info/requires.txt
9
+ src/rubin_squarebot.egg-info/top_level.txt
10
+ src/rubinobs/square/squarebot/__init__.py
11
+ src/rubinobs/square/squarebot/py.typed
12
+ src/rubinobs/square/squarebot/models/__init__.py
13
+ src/rubinobs/square/squarebot/models/kafka.py
14
+ src/rubinobs/square/squarebot/models/slack.py
@@ -0,0 +1,5 @@
1
+ pydantic
2
+ safir
3
+
4
+ [dev]
5
+ mypy
@@ -0,0 +1,179 @@
1
+ """Models for Kafka messages produced by Squarebot."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from typing import Any, Self
7
+
8
+ from dataclasses_avroschema.avrodantic import AvroBaseModel
9
+ from pydantic import Field
10
+
11
+ from .slack import SlackChannelType, SlackMessageEvent, SlackMessageType
12
+
13
+ __all__ = [
14
+ "SquarebotSlackMessageKey",
15
+ "SquarebotSlackMessageValue",
16
+ "SquarebotSlackAppMentionValue",
17
+ ]
18
+
19
+
20
+ class SquarebotSlackMessageKey(AvroBaseModel):
21
+ """Kafka message key model for Slack messages sent by Squarebot."""
22
+
23
+ channel: str = Field(..., description="The Slack channel ID.")
24
+
25
+ class Meta:
26
+ """Metadata for the model."""
27
+
28
+ namespace = "lsst.square-events.squarebot.messages"
29
+ schema_name = "key"
30
+
31
+ @classmethod
32
+ def from_event(cls, event: SlackMessageEvent) -> Self:
33
+ """Create a Kafka key for a Slack message from a Slack event.
34
+
35
+ Parameters
36
+ ----------
37
+ event
38
+ The Slack event.
39
+
40
+ Returns
41
+ -------
42
+ key
43
+ The Squarebot message key.
44
+ """
45
+ return cls(channel=event.event.channel)
46
+
47
+
48
+ class SquarebotSlackMessageValue(AvroBaseModel):
49
+ """Kafka message value model for Slack messages sent by Squarebot.
50
+
51
+ This value schema should be paired with `SquarebotSlackMessageKey` for
52
+ the key schema.
53
+ """
54
+
55
+ type: SlackMessageType = Field(..., description="The Slack event type.")
56
+
57
+ channel: str = Field(..., description="The Slack channel ID.")
58
+
59
+ channel_type: SlackChannelType = Field(
60
+ ..., description="The Slack channel type."
61
+ )
62
+
63
+ user: str = Field(
64
+ ..., description="The ID of the user that sent the message."
65
+ )
66
+
67
+ ts: str = Field(
68
+ ...,
69
+ description=(
70
+ "The Slack message timestamp. This is string-formatted to allow "
71
+ "comparison with other Slack messges which use the ``ts`` to "
72
+ "identify and reference messages."
73
+ ),
74
+ )
75
+
76
+ text: str = Field(..., description="The Slack message text content.")
77
+
78
+ slack_event: str = Field(
79
+ ..., description="The original Slack event JSON string."
80
+ )
81
+
82
+ class Meta:
83
+ """Metadata for the model."""
84
+
85
+ namespace = "lsst.square-events.squarebot.messages"
86
+ schema_name = "value"
87
+
88
+ @classmethod
89
+ def from_event(cls, event: SlackMessageEvent, raw: dict[str, Any]) -> Self:
90
+ """Create a Kafka value for a Slack message from a Slack event.
91
+
92
+ Parameters
93
+ ----------
94
+ event
95
+ The Slack event.
96
+ raw
97
+ The raw Slack event JSON.
98
+
99
+ Returns
100
+ -------
101
+ value
102
+ The Squarebot message value.
103
+ """
104
+ return cls(
105
+ type=event.event.type,
106
+ channel=event.event.channel,
107
+ channel_type=event.event.channel_type,
108
+ user=event.event.user,
109
+ ts=event.event.ts,
110
+ text=event.event.text,
111
+ slack_event=json.dumps(raw),
112
+ )
113
+
114
+
115
+ class SquarebotSlackAppMentionValue(AvroBaseModel):
116
+ """Kafka message value model for Slack app_mention message sent by
117
+ Squarebot.
118
+
119
+ These are like `SquarebotSlackMessageValue` but lack a `channel_type`
120
+ field.
121
+
122
+ This value schema should be paired with `SquarebotSlackMessageKey` for
123
+ the key schema.
124
+ """
125
+
126
+ type: SlackMessageType = Field(..., description="The Slack event type.")
127
+
128
+ channel: str = Field(..., description="The Slack channel ID.")
129
+
130
+ user: str = Field(
131
+ ..., description="The ID of the user that sent the message."
132
+ )
133
+
134
+ ts: str = Field(
135
+ ...,
136
+ description=(
137
+ "The Slack message timestamp. This is string-formatted to allow "
138
+ "comparison with other Slack messges which use the ``ts`` to "
139
+ "identify and reference messages."
140
+ ),
141
+ )
142
+
143
+ text: str = Field(..., description="The Slack message text content.")
144
+
145
+ slack_event: str = Field(
146
+ ..., description="The original Slack event JSON string."
147
+ )
148
+
149
+ class Meta:
150
+ """Metadata for the model."""
151
+
152
+ namespace = "lsst.square-events.squarebot.appmention"
153
+ schema_name = "value"
154
+
155
+ @classmethod
156
+ def from_event(cls, event: SlackMessageEvent, raw: dict[str, Any]) -> Self:
157
+ """Create a Kafka value for a Slack message from a Slack event.
158
+
159
+ Parameters
160
+ ----------
161
+ event
162
+ The Slack event.
163
+ raw
164
+ The raw Slack event JSON.
165
+
166
+ Returns
167
+ -------
168
+ value
169
+ The Squarebot message value.
170
+ """
171
+ return cls(
172
+ type=event.event.type,
173
+ channel=event.event.channel,
174
+ channel_type=event.event.channel_type,
175
+ user=event.event.user,
176
+ ts=event.event.ts,
177
+ text=event.event.text,
178
+ slack_event=json.dumps(raw),
179
+ )
@@ -0,0 +1,207 @@
1
+ """Slack API models."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from enum import Enum
6
+ from typing import Optional
7
+
8
+ from pydantic import BaseModel, Field
9
+
10
+ __all__ = [
11
+ "BaseSlackEvent",
12
+ "SlackUrlVerificationEvent",
13
+ "SlackMessageEvent",
14
+ "SlackMessageType",
15
+ "SlackChannelType",
16
+ "SlackMessageEventContent",
17
+ "SlackBlockAction",
18
+ "SlackUser",
19
+ "SlackTeam",
20
+ "SlackChannel",
21
+ ]
22
+
23
+
24
+ class BaseSlackEvent(BaseModel):
25
+ """A model for the minimal request payload from Slack for an event.
26
+
27
+ Any event message is gauranteed to have these fields. For specific types of
28
+ events, re-parse the request body with a specific model such as
29
+ `SlackMessageEvent`. For information about all the Slack event types, see
30
+ https://api.slack.com/events.
31
+ """
32
+
33
+ type: str = Field(
34
+ ...,
35
+ description=(
36
+ "The Slack event type in the message's outer scope; typically "
37
+ "this is ``url_verification`` or ``event_callback``."
38
+ ),
39
+ )
40
+
41
+
42
+ class SlackUrlVerificationEvent(BaseSlackEvent):
43
+ """A Slack ``url_verification`` event."""
44
+
45
+ challenge: str = Field(..., description="URL challenge content.")
46
+
47
+
48
+ class SlackMessageType(str, Enum):
49
+ app_mention = "app_mention"
50
+ message = "message"
51
+
52
+
53
+ class SlackChannelType(str, Enum):
54
+ channel = "channel" # public channel
55
+ group = "group" # private channel
56
+ im = "im" # direct message
57
+ mpim = "mpim" # multi-persion direct message
58
+
59
+
60
+ class SlackMessageEventContent(BaseModel):
61
+ """A model for the ``event`` field inside a message event.
62
+
63
+ See https://api.slack.com/events/app_mention and
64
+ https://api.slack.com/events/message.
65
+ """
66
+
67
+ type: SlackMessageType = Field(description="The Slack message type.")
68
+
69
+ channel: str = Field(
70
+ description=(
71
+ "ID of the channel where the message was sent "
72
+ "(e.g., C0LAN2Q65)."
73
+ )
74
+ )
75
+
76
+ channel_type: Optional[SlackChannelType] = Field(
77
+ description=(
78
+ "The type of channel (public, direct im, etc..). This is null for "
79
+ "``app_mention`` events."
80
+ )
81
+ )
82
+
83
+ user: str = Field(
84
+ description="The ID of the user that sent the message (eg U061F7AUR)."
85
+ )
86
+
87
+ text: str = Field(description="Content of the message.")
88
+
89
+ ts: str = Field(description="Timestamp of the message.")
90
+
91
+ event_ts: str = Field(description="When the event was dispatched.")
92
+
93
+
94
+ class SlackMessageEvent(BaseSlackEvent):
95
+ """A Slack event for message events in general.
96
+
97
+ See https://api.slack.com/events/app_mention and
98
+ https://api.slack.com/events/message.
99
+ """
100
+
101
+ team_id: str = Field(
102
+ description=(
103
+ "The unique identifier of the workspace where the event occurred."
104
+ )
105
+ )
106
+
107
+ api_app_id: str = Field(
108
+ description=(
109
+ "The unique identifier of your installed Slack application. Use "
110
+ "this to distinguish which app the event belongs to if you use "
111
+ "multiple apps with the same Request URL."
112
+ )
113
+ )
114
+
115
+ event_id: str = Field(
116
+ description=(
117
+ "A unique identifier for this specific event, globally unique "
118
+ "across all workspaces."
119
+ )
120
+ )
121
+
122
+ event_time: int = Field(
123
+ description=(
124
+ "The epoch timestamp in seconds indicating when this event was "
125
+ "dispatched."
126
+ )
127
+ )
128
+
129
+ authed_users: Optional[list[str]] = Field(
130
+ None,
131
+ description=(
132
+ "An array of string-based User IDs. Each member of the collection "
133
+ "represents a user that has installed your application/bot and "
134
+ "indicates the described event would be visible to those users."
135
+ ),
136
+ )
137
+
138
+ event: SlackMessageEventContent
139
+
140
+
141
+ class SlackUser(BaseModel):
142
+ """A model for the user field in Slack interaction payloads."""
143
+
144
+ id: str = Field(description="ID of the user.")
145
+
146
+ username: str = Field(description="User name of the user.")
147
+
148
+ team_id: str = Field(description="The user's team.")
149
+
150
+
151
+ class SlackTeam(BaseModel):
152
+ """A model for the team field in Slack interaction payloads."""
153
+
154
+ id: str = Field(description="ID of the team.")
155
+
156
+ domain: str = Field(description="Domain name of the team.")
157
+
158
+
159
+ class SlackChannel(BaseModel):
160
+ """A model for the channel field in Slack interaction payloads."""
161
+
162
+ id: str = Field(description="ID of the channel.")
163
+
164
+ name: str = Field(description="Name of the channel.")
165
+
166
+
167
+ class SlackBlockAction(BaseModel):
168
+ """A model for a Slack Block kit interaction.
169
+
170
+ This isn't yet a full model for a block action payload; experience is
171
+ needed to fully understand what the payloads are for the types of
172
+ interactions we use.
173
+
174
+ See https://api.slack.com/reference/interaction-payloads/block-actions
175
+ """
176
+
177
+ type: str = Field(description="Should be `block_actions`.")
178
+
179
+ trigger_id: str = Field(
180
+ description="A short-lived ID used to launch modals."
181
+ )
182
+
183
+ api_app_id: str = Field(
184
+ description=(
185
+ "The unique identifier of your installed Slack application. Use "
186
+ "this to distinguish which app the event belongs to if you use "
187
+ "multiple apps with the same Request URL."
188
+ )
189
+ )
190
+
191
+ response_url: str = Field(
192
+ description=(
193
+ "A short-lived URL to send message in response to interactions."
194
+ )
195
+ )
196
+
197
+ user: SlackUser = Field(
198
+ description=(
199
+ "Information about the user that triggered the interaction."
200
+ )
201
+ )
202
+
203
+ team: SlackTeam = Field(description="Information about the Slack team.")
204
+
205
+ channel: SlackChannel = Field(
206
+ description="Information about the Slack channel."
207
+ )
@@ -0,0 +1,20 @@
1
+ [tox]
2
+ envlist = typing,lint
3
+ isolated_build = True
4
+
5
+ [testenv]
6
+ description = Base testenv ({envname}).
7
+ extras =
8
+ dev
9
+
10
+ [testenv:typing]
11
+ description = Run mypy.
12
+ commands =
13
+ mypy src
14
+
15
+ [testenv:lint]
16
+ description = Lint codebase by running pre-commit (Black, isort, Flake8).
17
+ skip_install = true
18
+ deps =
19
+ pre-commit
20
+ commands = pre-commit run --all-files