rusticai-slack 0.0.1__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,497 @@
1
+ Metadata-Version: 2.4
2
+ Name: rusticai-slack
3
+ Version: 0.0.1
4
+ Summary: A data connector for slack integration.
5
+ License-Expression: Apache-2.0
6
+ Author: Dragonscale Industries Inc.
7
+ Author-email: dev@dragonscale.ai
8
+ Requires-Python: >=3.13,<3.14
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Provides-Extra: test
12
+ Requires-Dist: aiohttp (>=3.9.0,<4.0.0)
13
+ Requires-Dist: cachetools (>=5.3.0,<6.0.0)
14
+ Requires-Dist: python-dotenv (>=1.0.0,<2.0.0)
15
+ Requires-Dist: requests (>=2.31.0,<3.0.0)
16
+ Requires-Dist: rusticai-core (>=1.2.3,<1.3.0)
17
+ Requires-Dist: rusticai-litellm (>=1.2.2,<1.3.0)
18
+ Requires-Dist: rusticai-llm-agent (>=1.2.4,<1.3.0)
19
+ Requires-Dist: slack-sdk (>=3.41.0,<4.0.0)
20
+ Project-URL: Homepage, https://www.rustic.ai/
21
+ Project-URL: Repository, https://github.com/dragonscale-ai/rustic-ai
22
+ Project-URL: Rustic AI Core, https://pypi.org/project/rusticai-core/
23
+ Description-Content-Type: text/markdown
24
+
25
+ # Rustic AI Slack Integration Module
26
+
27
+ **Bidirectional** Slack integration for Rustic AI guilds with real-time Socket Mode support.
28
+
29
+ ## Features
30
+
31
+ ✅ **Socket Mode (Real-time)** - Receive Slack events via WebSocket (no public endpoint needed!)
32
+ ✅ **Natural Language Interface** - Send messages and manage channels using conversational requests
33
+ ✅ **Direct API Access** - Send messages, list channels, create channels programmatically
34
+ ✅ **Bidirectional Communication** - Bot responds to @mentions and DMs automatically
35
+ ✅ **Thread Support** - Replies stay in threads for organized conversations
36
+ ✅ **Simple Token Management** - Environment variable token storage
37
+ ✅ **LLM Tool Calling** - Automatic conversion of natural language to Slack API calls
38
+ ✅ **Multi-workspace Support** - Manage multiple Slack workspaces
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ cd slack
44
+ poetry install --with dev --all-extras
45
+ ```
46
+
47
+ ## Quick Start
48
+
49
+ ### 1. Create Slack App with Socket Mode
50
+
51
+ 1. Visit https://api.slack.com/apps
52
+ 2. Create new app → "From scratch"
53
+ 3. **Socket Mode** → Enable Socket Mode
54
+ 4. **Generate App-Level Token**:
55
+ - Token Name: "Socket Mode Token"
56
+ - Scopes: `connections:write`
57
+ - Copy token (starts with `xapp-`)
58
+ 5. **Event Subscriptions** → Enable Events
59
+ - Subscribe to bot events:
60
+ - `app_mention` - Bot is @mentioned
61
+ - `message.channels` - Messages in channels
62
+ - `message.im` - Direct messages
63
+ 6. **OAuth & Permissions** → Add bot token scopes:
64
+ - `app_mentions:read`
65
+ - `chat:write`
66
+ - `channels:read`
67
+ - `channels:history`
68
+ - `im:history`
69
+ - `users:read`
70
+ 7. **Install to workspace**
71
+ 8. Copy the **Bot User OAuth Token** (starts with `xoxb-`)
72
+
73
+ ### 2. Set Environment Variables
74
+
75
+ ```bash
76
+ export SLACK_BOT_TOKEN="xoxb-your-bot-token-here"
77
+ export SLACK_APP_TOKEN="xapp-your-app-token-here" # NEW - For Socket Mode
78
+ export OPENAI_API_KEY="sk-your-key-here" # For LLM agent
79
+ ```
80
+
81
+ ### 3. Load the Guild
82
+
83
+ ```python
84
+ from rustic_ai.core.guild.builders import GuildBuilder
85
+
86
+ # Build guild from JSON spec - Socket Mode connects automatically!
87
+ guild = GuildBuilder.from_json_file("slack/slack_guild.json").launch("my_org")
88
+
89
+ # Check logs for:
90
+ # INFO - SlackSocketModeAgent ready, starting Socket Mode listener...
91
+ # INFO - Connecting to Slack via Socket Mode...
92
+ # INFO - Socket Mode connected and listening for events!
93
+ ```
94
+
95
+ ### 4. Interact with Bot
96
+
97
+ #### In Slack (Socket Mode - Inbound)
98
+
99
+ ```
100
+ # In any Slack channel where bot is a member:
101
+ @mybot what's the weather?
102
+
103
+ # Bot automatically:
104
+ # 1. Receives event via Socket Mode
105
+ # 2. Processes with LLM
106
+ # 3. Responds in thread
107
+ ```
108
+
109
+ #### From Web App or Code (Outbound)
110
+
111
+ Send messages via guild:
112
+
113
+ #### Option A: Natural Language (via LLM)
114
+
115
+ ```python
116
+ from rustic_ai.core.guild.agent_ext.depends.llm.models import (
117
+ ChatCompletionRequest,
118
+ UserMessage
119
+ )
120
+
121
+ # Send to guild
122
+ request = ChatCompletionRequest(
123
+ messages=[
124
+ UserMessage(content="Send 'Hello team!' to #general")
125
+ ]
126
+ )
127
+ # Publish request to guild...
128
+ ```
129
+
130
+ #### Option B: Direct API Call
131
+
132
+ ```python
133
+ from rustic_ai.slack.models.messages import SlackSendMessageRequest
134
+
135
+ request = SlackSendMessageRequest(
136
+ workspace_id="default_workspace",
137
+ channel="#general",
138
+ text="Hello from Rustic AI!"
139
+ )
140
+ # Publish request to guild...
141
+ ```
142
+
143
+ ## Architecture
144
+
145
+ ### Bidirectional Flow
146
+
147
+ ```
148
+ ┌─────────────────────────────────────────────────────────────────┐
149
+ │ OUTBOUND (Guild → Slack) │
150
+ ├─────────────────────────────────────────────────────────────────┤
151
+ │ │
152
+ │ User Request (Web App) │
153
+ │ │ │
154
+ │ ▼ │
155
+ │ ChatCompletionRequest ("send hello to #general") │
156
+ │ │ │
157
+ │ ▼ │
158
+ │ SlackLLM Agent (LLMAgent with tools) │
159
+ │ │ (converts to API call) │
160
+ │ ▼ │
161
+ │ SlackSendMessageRequest │
162
+ │ │ │
163
+ │ ▼ │
164
+ │ SlackConnector Agent │
165
+ │ │ (uses SLACK_BOT_TOKEN) │
166
+ │ ▼ │
167
+ │ POST https://slack.com/api/chat.postMessage │
168
+ │ │ │
169
+ │ ▼ │
170
+ │ ✅ Message appears in Slack │
171
+ │ │
172
+ └─────────────────────────────────────────────────────────────────┘
173
+
174
+ ┌─────────────────────────────────────────────────────────────────┐
175
+ │ INBOUND (Slack → Guild) │
176
+ ├─────────────────────────────────────────────────────────────────┤
177
+ │ │
178
+ │ User in Slack: "@mybot hello" │
179
+ │ │ │
180
+ │ ▼ │
181
+ │ Slack sends event via Socket Mode WebSocket │
182
+ │ │ │
183
+ │ ▼ │
184
+ │ SlackSocketModeAgent (WebSocket listener) │
185
+ │ │ (receives app_mention event) │
186
+ │ ▼ │
187
+ │ Publishes SlackEventMessage to SLACK_INBOUND topic │
188
+ │ │ │
189
+ │ ▼ │
190
+ │ SlackEventHandlerAgent │
191
+ │ │ (cleans @mention, adds context) │
192
+ │ ▼ │
193
+ │ ChatCompletionRequest("hello") │
194
+ │ │ │
195
+ │ ▼ │
196
+ │ SlackLLM Agent (processes request) │
197
+ │ │ │
198
+ │ ▼ │
199
+ │ SlackSendMessageRequest (with thread_ts for threading) │
200
+ │ │ │
201
+ │ ▼ │
202
+ │ SlackConnectorAgent │
203
+ │ │ │
204
+ │ ▼ │
205
+ │ POST https://slack.com/api/chat.postMessage │
206
+ │ │ │
207
+ │ ▼ │
208
+ │ ✅ Bot replies in Slack thread │
209
+ │ │
210
+ └─────────────────────────────────────────────────────────────────┘
211
+ ```
212
+
213
+ ### Agents
214
+
215
+ **SlackSocketMode** (`slack_socket_mode`) - NEW!
216
+ - Connects to Slack via Socket Mode WebSocket
217
+ - Receives real-time events (mentions, messages, reactions)
218
+ - Publishes events to guild message bus
219
+ - No public endpoint required
220
+
221
+ **SlackEventHandler** (`slack_event_handler`) - NEW!
222
+ - Processes incoming Slack events
223
+ - Cleans up @mentions and formats messages
224
+ - Routes events to appropriate handlers (LLM, custom agents)
225
+ - Maintains context for threaded replies
226
+
227
+ **SlackConnector** (`slack_connector`)
228
+ - Handles all Slack API operations (outbound)
229
+ - Reads token from `SLACK_BOT_TOKEN` env var
230
+ - Supports messages, channels, users, files, reactions
231
+ - Uses thread context from session state for replies
232
+
233
+ **SlackLLM** (`slack_llm`)
234
+ - Natural language interface using LLMAgent
235
+ - Configured with Slack tools (send_message, list_channels, create_channel)
236
+ - Converts user requests to API calls
237
+ - Processes both web app requests and Slack events
238
+
239
+ ## API Reference
240
+
241
+ ### Message Operations
242
+
243
+ ```python
244
+ # Send message
245
+ SlackSendMessageRequest(
246
+ workspace_id="default_workspace",
247
+ channel="#general", # or channel ID "C123ABC"
248
+ text="Hello Slack!",
249
+ )
250
+
251
+ # Update message
252
+ SlackUpdateMessageRequest(
253
+ workspace_id="default_workspace",
254
+ channel="C123ABC",
255
+ ts="1234567890.123456",
256
+ text="Updated message",
257
+ )
258
+
259
+ # Delete message
260
+ SlackDeleteMessageRequest(
261
+ workspace_id="default_workspace",
262
+ channel="C123ABC",
263
+ ts="1234567890.123456",
264
+ )
265
+
266
+ # Get message history
267
+ SlackGetMessagesRequest(
268
+ workspace_id="default_workspace",
269
+ channel="C123ABC",
270
+ limit=100,
271
+ )
272
+ ```
273
+
274
+ ### Channel Operations
275
+
276
+ ```python
277
+ # List channels
278
+ SlackListChannelsRequest(
279
+ workspace_id="default_workspace",
280
+ types="public_channel,private_channel", # comma-separated
281
+ exclude_archived=True,
282
+ limit=100,
283
+ )
284
+
285
+ # Create channel
286
+ SlackCreateChannelRequest(
287
+ workspace_id="default_workspace",
288
+ name="new-channel", # lowercase, hyphens only
289
+ is_private=False,
290
+ )
291
+
292
+ # Join channel
293
+ SlackJoinChannelRequest(
294
+ workspace_id="default_workspace",
295
+ channel="C123ABC",
296
+ )
297
+
298
+ # Invite to channel
299
+ SlackInviteToChannelRequest(
300
+ workspace_id="default_workspace",
301
+ channel="C123ABC",
302
+ users=["U123ABC"],
303
+ )
304
+ ```
305
+
306
+ ## Guild Specification
307
+
308
+ The guild is defined in `slack_guild.json` with:
309
+
310
+ - **2 agents**: SlackConnector + SlackLLM
311
+ - **3 tools** for LLM:
312
+ - `send_message` → SlackSendMessageRequest
313
+ - `list_channels` → SlackListChannelsRequest
314
+ - `create_channel` → SlackCreateChannelRequest
315
+ - **Topic-based routing**:
316
+ - `SLACK_CHAT` - LLM conversations
317
+ - `SLACK_API` - Direct API operations
318
+ - `user_message_broadcast` - Responses to user
319
+
320
+ ## Token Management
321
+
322
+ ### Environment Variable (Primary)
323
+
324
+ ```bash
325
+ export SLACK_BOT_TOKEN="xoxb-your-token"
326
+ ```
327
+
328
+ SlackConnector checks `SLACK_BOT_TOKEN` first.
329
+
330
+ ### Guild State (Fallback)
331
+
332
+ ```python
333
+ # Set in guild state
334
+ await guild.set_state("slack_bot_token", "xoxb-new-token")
335
+
336
+ # Clear cache to reload
337
+ slack_connector = guild._agents_by_name["SlackConnector"]
338
+ slack_connector._clients.clear()
339
+ slack_connector._token = None
340
+ ```
341
+
342
+ ## Socket Mode vs HTTP Webhooks
343
+
344
+ This module uses **Socket Mode** instead of HTTP webhooks for receiving events:
345
+
346
+ | Feature | Socket Mode ✅ | HTTP Webhooks |
347
+ |---------|--------------|---------------|
348
+ | **Public endpoint** | Not needed | Required (HTTPS) |
349
+ | **Firewall** | Works behind firewall | Requires inbound access |
350
+ | **Development** | Local testing easy | Need ngrok/public URL |
351
+ | **Connection** | WebSocket (persistent) | HTTP POST (per event) |
352
+ | **Setup complexity** | Simple (2 tokens) | Complex (webhook verification) |
353
+ | **Security** | WSS connection | Signature verification |
354
+ | **Best for** | Internal tools, dev | Public apps, production |
355
+
356
+ **Why Socket Mode?**
357
+ - ✅ No public server needed
358
+ - ✅ Works on localhost
359
+ - ✅ Simpler setup
360
+ - ✅ Faster development
361
+ - ✅ Consistent with WebSocket architecture
362
+
363
+ ## Configuration
364
+
365
+ ### Required Scopes
366
+
367
+ ```
368
+ # Socket Mode
369
+ connections:write # Socket Mode connection (app-level token)
370
+
371
+ # Bot Token Scopes
372
+ app_mentions:read # Receive @bot mentions
373
+ chat:write # Post messages
374
+ channels:read # View channel info
375
+ channels:history # View channel messages
376
+ im:history # View DM messages
377
+ users:read # View user info
378
+ ```
379
+
380
+ ### Environment Variables
381
+
382
+ ```bash
383
+ SLACK_BOT_TOKEN=xoxb-your-token # Required
384
+ SLACK_APP_TOKEN=xapp-your-token # Required (Socket Mode)
385
+ OPENAI_API_KEY=sk-your-key # Required for LLM agent
386
+ ```
387
+
388
+ ## Testing
389
+
390
+ ```bash
391
+ # Run tests
392
+ cd slack
393
+ poetry run pytest
394
+
395
+ # Run specific test
396
+ poetry run pytest tests/test_connector_agent.py
397
+ ```
398
+
399
+ ## Examples
400
+
401
+ ### Example 1: Send Notification
402
+
403
+ ```python
404
+ from rustic_ai.slack.models.messages import SlackSendMessageRequest
405
+
406
+ request = SlackSendMessageRequest(
407
+ workspace_id="default_workspace",
408
+ channel="#notifications",
409
+ text="Deployment completed successfully!"
410
+ )
411
+ ```
412
+
413
+ ### Example 2: List Channels
414
+
415
+ ```python
416
+ from rustic_ai.slack.models.channels import SlackListChannelsRequest
417
+
418
+ request = SlackListChannelsRequest(
419
+ workspace_id="default_workspace",
420
+ types="public_channel",
421
+ limit=50
422
+ )
423
+ ```
424
+
425
+ ### Example 3: Natural Language
426
+
427
+ ```python
428
+ from rustic_ai.core.guild.agent_ext.depends.llm.models import (
429
+ ChatCompletionRequest,
430
+ UserMessage
431
+ )
432
+
433
+ request = ChatCompletionRequest(
434
+ messages=[
435
+ UserMessage(content="Create a new channel called project-alpha")
436
+ ]
437
+ )
438
+ ```
439
+
440
+ ## Troubleshooting
441
+
442
+ ### "No Slack bot token found"
443
+
444
+ ```bash
445
+ # Check if set
446
+ echo $SLACK_BOT_TOKEN
447
+
448
+ # Set it
449
+ export SLACK_BOT_TOKEN="xoxb-your-token"
450
+ ```
451
+
452
+ ### "Channel not found"
453
+
454
+ Invite the bot to the channel first:
455
+ ```
456
+ # In Slack:
457
+ /invite @YourBot
458
+ ```
459
+
460
+ ### "Missing scope"
461
+
462
+ 1. Go to https://api.slack.com/apps
463
+ 2. Your App → OAuth & Permissions
464
+ 3. Add missing scope under "Bot Token Scopes"
465
+ 4. Reinstall to workspace
466
+ 5. Update `SLACK_BOT_TOKEN`
467
+
468
+ ## Module Structure
469
+
470
+ ```
471
+ slack/
472
+ ├── src/rustic_ai/slack/
473
+ │ ├── agents/
474
+ │ │ └── connector_agent.py # SlackConnectorAgent
475
+ │ ├── client/
476
+ │ │ └── slack_client.py # SlackAPIClient
477
+ │ ├── models/
478
+ │ │ ├── messages.py # Message models
479
+ │ │ ├── channels.py # Channel models
480
+ │ │ └── users.py # User models
481
+ │ └── __init__.py
482
+ ├── tests/
483
+ │ ├── test_connector_agent.py
484
+ │ └── ...
485
+ ├── slack_guild.json # Guild specification
486
+ ├── pyproject.toml
487
+ └── README.md
488
+ ```
489
+
490
+ ## License
491
+
492
+ Apache-2.0
493
+
494
+ ---
495
+
496
+ **Built with ❤️ for Rustic AI**
497
+