agent-messenger 1.2.0 → 1.3.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.
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +1 -1
- package/dist/package.json +3 -2
- package/dist/src/platforms/slackbot/cli.d.ts +5 -0
- package/dist/src/platforms/slackbot/cli.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/cli.js +19 -0
- package/dist/src/platforms/slackbot/cli.js.map +1 -0
- package/dist/src/platforms/slackbot/client.d.ts +43 -0
- package/dist/src/platforms/slackbot/client.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/client.js +347 -0
- package/dist/src/platforms/slackbot/client.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/auth.d.ts +35 -0
- package/dist/src/platforms/slackbot/commands/auth.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/auth.js +185 -0
- package/dist/src/platforms/slackbot/commands/auth.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/channel.d.ts +3 -0
- package/dist/src/platforms/slackbot/commands/channel.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/channel.js +40 -0
- package/dist/src/platforms/slackbot/commands/channel.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/index.d.ts +6 -0
- package/dist/src/platforms/slackbot/commands/index.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/index.js +6 -0
- package/dist/src/platforms/slackbot/commands/index.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/message.d.ts +3 -0
- package/dist/src/platforms/slackbot/commands/message.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/message.js +135 -0
- package/dist/src/platforms/slackbot/commands/message.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/reaction.d.ts +3 -0
- package/dist/src/platforms/slackbot/commands/reaction.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/reaction.js +43 -0
- package/dist/src/platforms/slackbot/commands/reaction.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/shared.d.ts +9 -0
- package/dist/src/platforms/slackbot/commands/shared.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/shared.js +13 -0
- package/dist/src/platforms/slackbot/commands/shared.js.map +1 -0
- package/dist/src/platforms/slackbot/commands/user.d.ts +3 -0
- package/dist/src/platforms/slackbot/commands/user.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/commands/user.js +40 -0
- package/dist/src/platforms/slackbot/commands/user.js.map +1 -0
- package/dist/src/platforms/slackbot/credential-manager.d.ts +18 -0
- package/dist/src/platforms/slackbot/credential-manager.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/credential-manager.js +187 -0
- package/dist/src/platforms/slackbot/credential-manager.js.map +1 -0
- package/dist/src/platforms/slackbot/index.d.ts +4 -0
- package/dist/src/platforms/slackbot/index.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/index.js +4 -0
- package/dist/src/platforms/slackbot/index.js.map +1 -0
- package/dist/src/platforms/slackbot/types.d.ts +460 -0
- package/dist/src/platforms/slackbot/types.d.ts.map +1 -0
- package/dist/src/platforms/slackbot/types.js +114 -0
- package/dist/src/platforms/slackbot/types.js.map +1 -0
- package/docs/content/docs/integrations/meta.json +1 -1
- package/docs/content/docs/integrations/slackbot.mdx +204 -0
- package/docs/src/app/page.tsx +18 -1
- package/e2e/config.ts +26 -0
- package/e2e/helpers.ts +6 -1
- package/e2e/slackbot.e2e.test.ts +306 -0
- package/package.json +3 -2
- package/skills/agent-slackbot/SKILL.md +285 -0
- package/skills/agent-slackbot/references/authentication.md +253 -0
- package/skills/agent-slackbot/references/common-patterns.md +218 -0
- package/skills/agent-slackbot/templates/monitor-channel.sh +98 -0
- package/skills/agent-slackbot/templates/post-message.sh +107 -0
- package/skills/agent-slackbot/templates/workspace-summary.sh +113 -0
- package/src/platforms/slackbot/cli.ts +30 -0
- package/src/platforms/slackbot/client.test.ts +282 -0
- package/src/platforms/slackbot/client.ts +401 -0
- package/src/platforms/slackbot/commands/auth.test.ts +245 -0
- package/src/platforms/slackbot/commands/auth.ts +240 -0
- package/src/platforms/slackbot/commands/channel.ts +46 -0
- package/src/platforms/slackbot/commands/index.ts +5 -0
- package/src/platforms/slackbot/commands/message.ts +182 -0
- package/src/platforms/slackbot/commands/reaction.ts +59 -0
- package/src/platforms/slackbot/commands/shared.ts +23 -0
- package/src/platforms/slackbot/commands/user.ts +46 -0
- package/src/platforms/slackbot/credential-manager.test.ts +264 -0
- package/src/platforms/slackbot/credential-manager.ts +218 -0
- package/src/platforms/slackbot/index.ts +19 -0
- package/src/platforms/slackbot/types.test.ts +90 -0
- package/src/platforms/slackbot/types.ts +222 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Authentication Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
agent-slackbot uses Slack Bot tokens (xoxb-) obtained from the Slack App configuration page. Unlike agent-slack which extracts user tokens from the desktop app, bot tokens are explicitly created and managed through the Slack API portal.
|
|
6
|
+
|
|
7
|
+
## Bot Token Setup
|
|
8
|
+
|
|
9
|
+
### Creating a Slack App
|
|
10
|
+
|
|
11
|
+
1. Go to [api.slack.com/apps](https://api.slack.com/apps)
|
|
12
|
+
2. Click **Create New App** > **From scratch**
|
|
13
|
+
3. Enter app name and select workspace
|
|
14
|
+
4. Go to **OAuth & Permissions**
|
|
15
|
+
|
|
16
|
+
### Required Scopes
|
|
17
|
+
|
|
18
|
+
Add these **Bot Token Scopes**:
|
|
19
|
+
|
|
20
|
+
| Scope | Purpose |
|
|
21
|
+
|-------|---------|
|
|
22
|
+
| `chat:write` | Send and update messages |
|
|
23
|
+
| `channels:history` | Read messages in public channels |
|
|
24
|
+
| `channels:read` | List and get info for public channels |
|
|
25
|
+
| `channels:join` | Join public channels |
|
|
26
|
+
| `groups:history` | Read messages in private channels |
|
|
27
|
+
| `groups:read` | List and get info for private channels |
|
|
28
|
+
| `users:read` | List workspace users |
|
|
29
|
+
| `users:read.email` | Access user email addresses |
|
|
30
|
+
| `reactions:write` | Add and remove emoji reactions |
|
|
31
|
+
| `reactions:read` | List reactions on messages |
|
|
32
|
+
|
|
33
|
+
### Installing the App
|
|
34
|
+
|
|
35
|
+
1. Click **Install to Workspace** on the OAuth & Permissions page
|
|
36
|
+
2. Review and **Allow** the requested permissions
|
|
37
|
+
3. Copy the **Bot User OAuth Token** (starts with `xoxb-`)
|
|
38
|
+
|
|
39
|
+
### Setting the Token
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# Basic setup
|
|
43
|
+
agent-slackbot auth set xoxb-your-bot-token
|
|
44
|
+
|
|
45
|
+
# With a custom bot identifier (for multi-bot setups)
|
|
46
|
+
agent-slackbot auth set xoxb-your-bot-token --bot deploy --name "Deploy Bot"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This command:
|
|
50
|
+
1. Validates the token format (must start with `xoxb-`)
|
|
51
|
+
2. Calls `auth.test` to verify the token against Slack API
|
|
52
|
+
3. Stores the bot under the workspace with its bot ID and name
|
|
53
|
+
4. Sets this bot as the current active bot
|
|
54
|
+
5. Saves credentials to `~/.config/agent-messenger/slackbot-credentials.json`
|
|
55
|
+
|
|
56
|
+
## Credential Storage
|
|
57
|
+
|
|
58
|
+
### Location
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
~/.config/agent-messenger/slackbot-credentials.json
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Format
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{
|
|
68
|
+
"current": {
|
|
69
|
+
"workspace_id": "T123456",
|
|
70
|
+
"bot_id": "deploy"
|
|
71
|
+
},
|
|
72
|
+
"workspaces": {
|
|
73
|
+
"T123456": {
|
|
74
|
+
"workspace_id": "T123456",
|
|
75
|
+
"workspace_name": "My Workspace",
|
|
76
|
+
"bots": {
|
|
77
|
+
"deploy": {
|
|
78
|
+
"bot_id": "deploy",
|
|
79
|
+
"bot_name": "Deploy Bot",
|
|
80
|
+
"token": "xoxb-1234567890-1234567890-abcdef..."
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Security
|
|
89
|
+
|
|
90
|
+
- File permissions: `0600` (owner read/write only)
|
|
91
|
+
- Token stored in plaintext (standard for CLI tools)
|
|
92
|
+
- Keep this file secure - it grants bot-level access to your workspace
|
|
93
|
+
|
|
94
|
+
## Environment Variables (CI/CD)
|
|
95
|
+
|
|
96
|
+
For automated environments, use environment variables instead of file-based credentials:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
export E2E_SLACKBOT_TOKEN=xoxb-your-bot-token
|
|
100
|
+
export E2E_SLACKBOT_WORKSPACE_ID=T123456
|
|
101
|
+
export E2E_SLACKBOT_WORKSPACE_NAME="My Workspace"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Environment variables take precedence over file-based credentials.
|
|
105
|
+
|
|
106
|
+
## Multi-Bot Management
|
|
107
|
+
|
|
108
|
+
Store and switch between multiple bot tokens:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# Add multiple bots
|
|
112
|
+
agent-slackbot auth set xoxb-deploy-token --bot deploy --name "Deploy Bot"
|
|
113
|
+
agent-slackbot auth set xoxb-alert-token --bot alert --name "Alert Bot"
|
|
114
|
+
|
|
115
|
+
# List all stored bots
|
|
116
|
+
agent-slackbot auth list
|
|
117
|
+
|
|
118
|
+
# Switch active bot
|
|
119
|
+
agent-slackbot auth use deploy
|
|
120
|
+
|
|
121
|
+
# Use a specific bot for one command
|
|
122
|
+
agent-slackbot message send C0ACZKTDDC0 "Alert!" --bot alert
|
|
123
|
+
|
|
124
|
+
# Remove a stored bot
|
|
125
|
+
agent-slackbot auth remove deploy
|
|
126
|
+
|
|
127
|
+
# Disambiguate across workspaces
|
|
128
|
+
agent-slackbot auth use T123456/deploy
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Authentication Status
|
|
132
|
+
|
|
133
|
+
Check current authentication state:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
agent-slackbot auth status
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Output when authenticated:
|
|
140
|
+
```json
|
|
141
|
+
{
|
|
142
|
+
"valid": true,
|
|
143
|
+
"workspace_id": "T123456",
|
|
144
|
+
"workspace_name": "My Workspace",
|
|
145
|
+
"bot_id": "deploy",
|
|
146
|
+
"bot_name": "Deploy Bot",
|
|
147
|
+
"user": "mybot",
|
|
148
|
+
"team": "My Workspace"
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Output when not authenticated:
|
|
153
|
+
```json
|
|
154
|
+
{
|
|
155
|
+
"valid": false,
|
|
156
|
+
"error": "No credentials configured. Run \"auth set <token>\" first."
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Clearing Credentials
|
|
161
|
+
|
|
162
|
+
Remove stored credentials:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
agent-slackbot auth clear
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Token Lifecycle
|
|
169
|
+
|
|
170
|
+
### When Tokens Stop Working
|
|
171
|
+
|
|
172
|
+
Bot tokens can be invalidated when:
|
|
173
|
+
- App is uninstalled from workspace
|
|
174
|
+
- App is deleted from api.slack.com
|
|
175
|
+
- Token is manually revoked
|
|
176
|
+
- Required scopes change (need reinstall)
|
|
177
|
+
|
|
178
|
+
### Re-authentication
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# Get a new token from api.slack.com/apps > OAuth & Permissions
|
|
182
|
+
agent-slackbot auth set xoxb-new-bot-token
|
|
183
|
+
|
|
184
|
+
# Verify
|
|
185
|
+
agent-slackbot auth status
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Troubleshooting
|
|
189
|
+
|
|
190
|
+
### "not_in_channel" Error
|
|
191
|
+
|
|
192
|
+
The bot must join a channel before posting messages to it:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
# Bots auto-join public channels when posting via API if they have channels:join scope
|
|
196
|
+
# For private channels, manually invite the bot from Slack UI
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### "invalid_auth" Error
|
|
200
|
+
|
|
201
|
+
Token is expired or revoked:
|
|
202
|
+
1. Go to api.slack.com/apps > your app > OAuth & Permissions
|
|
203
|
+
2. Check if the app is still installed
|
|
204
|
+
3. Reinstall if needed and copy the new token
|
|
205
|
+
4. Run `agent-slackbot auth set xoxb-new-token`
|
|
206
|
+
|
|
207
|
+
### "missing_scope" Error
|
|
208
|
+
|
|
209
|
+
The bot token lacks required permissions:
|
|
210
|
+
1. Go to api.slack.com/apps > your app > OAuth & Permissions
|
|
211
|
+
2. Add the missing scope under Bot Token Scopes
|
|
212
|
+
3. Reinstall the app to workspace
|
|
213
|
+
4. Copy the new token and run `agent-slackbot auth set xoxb-new-token`
|
|
214
|
+
|
|
215
|
+
## App Manifest
|
|
216
|
+
|
|
217
|
+
For quick setup, use this Slack App manifest:
|
|
218
|
+
|
|
219
|
+
```yaml
|
|
220
|
+
display_information:
|
|
221
|
+
name: Agent Messenger Bot
|
|
222
|
+
description: Bot for agent-messenger CLI integration
|
|
223
|
+
background_color: "#1a1a2e"
|
|
224
|
+
|
|
225
|
+
features:
|
|
226
|
+
bot_user:
|
|
227
|
+
display_name: agent-messenger
|
|
228
|
+
always_online: false
|
|
229
|
+
|
|
230
|
+
oauth_config:
|
|
231
|
+
scopes:
|
|
232
|
+
bot:
|
|
233
|
+
- chat:write
|
|
234
|
+
- channels:history
|
|
235
|
+
- channels:read
|
|
236
|
+
- channels:join
|
|
237
|
+
- groups:history
|
|
238
|
+
- groups:read
|
|
239
|
+
- users:read
|
|
240
|
+
- users:read.email
|
|
241
|
+
- reactions:write
|
|
242
|
+
- reactions:read
|
|
243
|
+
|
|
244
|
+
settings:
|
|
245
|
+
org_deploy_enabled: false
|
|
246
|
+
socket_mode_enabled: false
|
|
247
|
+
token_rotation_enabled: false
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
1. Go to [api.slack.com/apps](https://api.slack.com/apps) > **Create New App** > **From an app manifest**
|
|
251
|
+
2. Select workspace, paste YAML, create
|
|
252
|
+
3. Install to workspace
|
|
253
|
+
4. Copy Bot User OAuth Token
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# Common Patterns
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This guide covers typical workflows for AI agents interacting with Slack using agent-slackbot (bot tokens).
|
|
6
|
+
|
|
7
|
+
## Pattern 1: Send a Simple Message
|
|
8
|
+
|
|
9
|
+
**Use case**: Post a notification or update to a channel
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
#!/bin/bash
|
|
13
|
+
|
|
14
|
+
CHANNEL="C0ACZKTDDC0"
|
|
15
|
+
|
|
16
|
+
RESULT=$(agent-slackbot message send "$CHANNEL" "Deployment completed!")
|
|
17
|
+
TS=$(echo "$RESULT" | jq -r '.ts')
|
|
18
|
+
|
|
19
|
+
if [ -n "$TS" ] && [ "$TS" != "null" ]; then
|
|
20
|
+
echo "Message sent: $TS"
|
|
21
|
+
else
|
|
22
|
+
echo "Failed: $(echo "$RESULT" | jq -r '.error')"
|
|
23
|
+
exit 1
|
|
24
|
+
fi
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**When to use**: Simple one-off messages, notifications, alerts.
|
|
28
|
+
|
|
29
|
+
## Pattern 2: Thread Conversation
|
|
30
|
+
|
|
31
|
+
**Use case**: Send progress updates in a thread
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
#!/bin/bash
|
|
35
|
+
|
|
36
|
+
CHANNEL="C0ACZKTDDC0"
|
|
37
|
+
|
|
38
|
+
# Send initial message
|
|
39
|
+
RESULT=$(agent-slackbot message send "$CHANNEL" "Starting deployment...")
|
|
40
|
+
THREAD_TS=$(echo "$RESULT" | jq -r '.ts')
|
|
41
|
+
|
|
42
|
+
# Send updates in thread
|
|
43
|
+
agent-slackbot message send "$CHANNEL" "Building application..." --thread "$THREAD_TS"
|
|
44
|
+
sleep 2
|
|
45
|
+
agent-slackbot message send "$CHANNEL" "Running tests..." --thread "$THREAD_TS"
|
|
46
|
+
sleep 2
|
|
47
|
+
agent-slackbot message send "$CHANNEL" "Deploying to production..." --thread "$THREAD_TS"
|
|
48
|
+
sleep 2
|
|
49
|
+
|
|
50
|
+
# Update parent with final status
|
|
51
|
+
agent-slackbot message update "$CHANNEL" "$THREAD_TS" "Deployment complete!"
|
|
52
|
+
|
|
53
|
+
# Add reaction
|
|
54
|
+
agent-slackbot reaction add "$CHANNEL" "$THREAD_TS" white_check_mark
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**When to use**: Multi-step processes, CI/CD pipelines, progress tracking.
|
|
58
|
+
|
|
59
|
+
## Pattern 3: Monitor Channel for New Messages
|
|
60
|
+
|
|
61
|
+
**Use case**: Poll a channel and respond to new messages
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
#!/bin/bash
|
|
65
|
+
|
|
66
|
+
CHANNEL="C0ACZKTDDC0"
|
|
67
|
+
LAST_TS=""
|
|
68
|
+
|
|
69
|
+
while true; do
|
|
70
|
+
MESSAGES=$(agent-slackbot message list "$CHANNEL" --limit 1)
|
|
71
|
+
LATEST_TS=$(echo "$MESSAGES" | jq -r '.[0].ts // ""')
|
|
72
|
+
|
|
73
|
+
if [ "$LATEST_TS" != "$LAST_TS" ] && [ -n "$LAST_TS" ]; then
|
|
74
|
+
TEXT=$(echo "$MESSAGES" | jq -r '.[0].text // ""')
|
|
75
|
+
echo "New message: $TEXT"
|
|
76
|
+
|
|
77
|
+
# Respond if needed
|
|
78
|
+
if echo "$TEXT" | grep -qi "help"; then
|
|
79
|
+
agent-slackbot message send "$CHANNEL" "How can I help?" --thread "$LATEST_TS"
|
|
80
|
+
fi
|
|
81
|
+
fi
|
|
82
|
+
|
|
83
|
+
LAST_TS="$LATEST_TS"
|
|
84
|
+
sleep 10
|
|
85
|
+
done
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Limitations**: Polling-based, not real-time. For production bots, use Slack's Events API.
|
|
89
|
+
|
|
90
|
+
## Pattern 4: Reaction-Based Workflow
|
|
91
|
+
|
|
92
|
+
**Use case**: Use reactions as status indicators
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
#!/bin/bash
|
|
96
|
+
|
|
97
|
+
CHANNEL="C0ACZKTDDC0"
|
|
98
|
+
|
|
99
|
+
# Send task message
|
|
100
|
+
RESULT=$(agent-slackbot message send "$CHANNEL" "Processing request...")
|
|
101
|
+
MSG_TS=$(echo "$RESULT" | jq -r '.ts')
|
|
102
|
+
|
|
103
|
+
# Mark as in-progress
|
|
104
|
+
agent-slackbot reaction add "$CHANNEL" "$MSG_TS" hourglass_flowing_sand
|
|
105
|
+
|
|
106
|
+
# Do work...
|
|
107
|
+
sleep 5
|
|
108
|
+
|
|
109
|
+
# Remove in-progress, add success
|
|
110
|
+
agent-slackbot reaction remove "$CHANNEL" "$MSG_TS" hourglass_flowing_sand
|
|
111
|
+
agent-slackbot reaction add "$CHANNEL" "$MSG_TS" white_check_mark
|
|
112
|
+
|
|
113
|
+
# Update message with result
|
|
114
|
+
agent-slackbot message update "$CHANNEL" "$MSG_TS" "Request processed successfully!"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**When to use**: Visual status tracking, acknowledgments, workflow states.
|
|
118
|
+
|
|
119
|
+
## Pattern 5: Multi-Channel Broadcast
|
|
120
|
+
|
|
121
|
+
**Use case**: Send the same message to multiple channels
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
#!/bin/bash
|
|
125
|
+
|
|
126
|
+
MESSAGE="System maintenance in 30 minutes"
|
|
127
|
+
CHANNELS=("C0ACZKTDDC0" "C0AC1NCF8NR")
|
|
128
|
+
|
|
129
|
+
for channel in "${CHANNELS[@]}"; do
|
|
130
|
+
RESULT=$(agent-slackbot message send "$channel" "$MESSAGE")
|
|
131
|
+
TS=$(echo "$RESULT" | jq -r '.ts // "failed"')
|
|
132
|
+
echo "Channel $channel: $TS"
|
|
133
|
+
sleep 1
|
|
134
|
+
done
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**When to use**: Announcements, alerts across multiple channels.
|
|
138
|
+
|
|
139
|
+
## Pattern 6: Error Handling and Retry
|
|
140
|
+
|
|
141
|
+
**Use case**: Robust message sending for production
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
#!/bin/bash
|
|
145
|
+
|
|
146
|
+
send_with_retry() {
|
|
147
|
+
local channel=$1
|
|
148
|
+
local message=$2
|
|
149
|
+
local max_attempts=3
|
|
150
|
+
local attempt=1
|
|
151
|
+
|
|
152
|
+
while [ $attempt -le $max_attempts ]; do
|
|
153
|
+
RESULT=$(agent-slackbot message send "$channel" "$message" 2>&1)
|
|
154
|
+
TS=$(echo "$RESULT" | jq -r '.ts // ""')
|
|
155
|
+
|
|
156
|
+
if [ -n "$TS" ] && [ "$TS" != "null" ]; then
|
|
157
|
+
echo "Sent: $TS"
|
|
158
|
+
return 0
|
|
159
|
+
fi
|
|
160
|
+
|
|
161
|
+
ERROR=$(echo "$RESULT" | jq -r '.error // "unknown"')
|
|
162
|
+
echo "Attempt $attempt failed: $ERROR"
|
|
163
|
+
|
|
164
|
+
# Don't retry on permanent errors
|
|
165
|
+
case "$ERROR" in
|
|
166
|
+
*"not_in_channel"*|*"channel_not_found"*)
|
|
167
|
+
return 1 ;;
|
|
168
|
+
esac
|
|
169
|
+
|
|
170
|
+
sleep $((attempt * 2))
|
|
171
|
+
attempt=$((attempt + 1))
|
|
172
|
+
done
|
|
173
|
+
|
|
174
|
+
echo "Failed after $max_attempts attempts"
|
|
175
|
+
return 1
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
send_with_retry "C0ACZKTDDC0" "Important notification!"
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Best Practices
|
|
182
|
+
|
|
183
|
+
### 1. Use Channel IDs, Not Names
|
|
184
|
+
|
|
185
|
+
Bot tokens require channel IDs (e.g., `C0ACZKTDDC0`), not names:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
# Get channel ID first
|
|
189
|
+
CHANNELS=$(agent-slackbot channel list)
|
|
190
|
+
CHANNEL_ID=$(echo "$CHANNELS" | jq -r '.[] | select(.name=="general") | .id')
|
|
191
|
+
agent-slackbot message send "$CHANNEL_ID" "Hello"
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### 2. Rate Limit Your Requests
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
for channel in "${CHANNELS[@]}"; do
|
|
198
|
+
agent-slackbot message send "$channel" "$MESSAGE"
|
|
199
|
+
sleep 1
|
|
200
|
+
done
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### 3. Use Threads for Related Messages
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
PARENT_TS=$(agent-slackbot message send "$CHANNEL" "Task started" | jq -r '.ts')
|
|
207
|
+
agent-slackbot message send "$CHANNEL" "Step 1 done" --thread "$PARENT_TS"
|
|
208
|
+
agent-slackbot message send "$CHANNEL" "Step 2 done" --thread "$PARENT_TS"
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### 4. Handle the "not_in_channel" Error
|
|
212
|
+
|
|
213
|
+
Bots need to join channels before posting. With the `channels:join` scope, posting to a public channel auto-joins. For private channels, invite the bot from Slack UI.
|
|
214
|
+
|
|
215
|
+
## See Also
|
|
216
|
+
|
|
217
|
+
- [Authentication Guide](authentication.md) - Setting up bot tokens
|
|
218
|
+
- [Templates](../templates/) - Runnable example scripts
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
#
|
|
3
|
+
# monitor-channel.sh - Monitor a Slack channel for new messages via bot token
|
|
4
|
+
#
|
|
5
|
+
# Usage:
|
|
6
|
+
# ./monitor-channel.sh <channel-id> [interval]
|
|
7
|
+
#
|
|
8
|
+
# Arguments:
|
|
9
|
+
# channel-id - Channel ID to monitor (e.g., C0ACZKTDDC0)
|
|
10
|
+
# interval - Polling interval in seconds (default: 10)
|
|
11
|
+
#
|
|
12
|
+
# Example:
|
|
13
|
+
# ./monitor-channel.sh C0ACZKTDDC0
|
|
14
|
+
# ./monitor-channel.sh C0ACZKTDDC0 5
|
|
15
|
+
|
|
16
|
+
set -euo pipefail
|
|
17
|
+
|
|
18
|
+
if [ $# -lt 1 ]; then
|
|
19
|
+
echo "Usage: $0 <channel-id> [interval]"
|
|
20
|
+
echo ""
|
|
21
|
+
echo "Examples:"
|
|
22
|
+
echo " $0 C0ACZKTDDC0 # Poll every 10s"
|
|
23
|
+
echo " $0 C0ACZKTDDC0 5 # Poll every 5s"
|
|
24
|
+
exit 1
|
|
25
|
+
fi
|
|
26
|
+
|
|
27
|
+
CHANNEL="$1"
|
|
28
|
+
INTERVAL="${2:-10}"
|
|
29
|
+
|
|
30
|
+
RED='\033[0;31m'
|
|
31
|
+
GREEN='\033[0;32m'
|
|
32
|
+
YELLOW='\033[1;33m'
|
|
33
|
+
BLUE='\033[0;34m'
|
|
34
|
+
NC='\033[0m'
|
|
35
|
+
|
|
36
|
+
LAST_TS=""
|
|
37
|
+
FIRST_RUN=true
|
|
38
|
+
|
|
39
|
+
check_messages() {
|
|
40
|
+
MESSAGES=$(agent-slackbot message list "$CHANNEL" --limit 1 2>&1)
|
|
41
|
+
LATEST_TS=$(echo "$MESSAGES" | jq -r '.[0].ts // ""')
|
|
42
|
+
|
|
43
|
+
if [ -z "$LATEST_TS" ]; then
|
|
44
|
+
if [ "$FIRST_RUN" = true ]; then
|
|
45
|
+
echo -e "${YELLOW}No messages in channel yet${NC}"
|
|
46
|
+
fi
|
|
47
|
+
FIRST_RUN=false
|
|
48
|
+
return 0
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
if [ "$LATEST_TS" != "$LAST_TS" ]; then
|
|
52
|
+
if [ "$FIRST_RUN" = false ] && [ -n "$LAST_TS" ]; then
|
|
53
|
+
TEXT=$(echo "$MESSAGES" | jq -r '.[0].text // ""')
|
|
54
|
+
USER=$(echo "$MESSAGES" | jq -r '.[0].user // "Unknown"')
|
|
55
|
+
|
|
56
|
+
echo ""
|
|
57
|
+
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
58
|
+
echo -e "${BLUE}New message in $CHANNEL${NC}"
|
|
59
|
+
echo -e "From: $USER"
|
|
60
|
+
echo -e "Message: ${TEXT:0:100}"
|
|
61
|
+
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
62
|
+
fi
|
|
63
|
+
|
|
64
|
+
LAST_TS="$LATEST_TS"
|
|
65
|
+
fi
|
|
66
|
+
|
|
67
|
+
FIRST_RUN=false
|
|
68
|
+
return 0
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if ! command -v agent-slackbot &> /dev/null; then
|
|
72
|
+
echo -e "${RED}Error: agent-slackbot not found${NC}"
|
|
73
|
+
echo "Install: npm install -g agent-messenger"
|
|
74
|
+
exit 1
|
|
75
|
+
fi
|
|
76
|
+
|
|
77
|
+
echo "Checking authentication..."
|
|
78
|
+
AUTH_STATUS=$(agent-slackbot auth status 2>&1)
|
|
79
|
+
VALID=$(echo "$AUTH_STATUS" | jq -r '.valid // false')
|
|
80
|
+
|
|
81
|
+
if [ "$VALID" != "true" ]; then
|
|
82
|
+
echo -e "${RED}Not authenticated! Run: agent-slackbot auth set xoxb-your-token${NC}"
|
|
83
|
+
exit 1
|
|
84
|
+
fi
|
|
85
|
+
|
|
86
|
+
WORKSPACE=$(echo "$AUTH_STATUS" | jq -r '.workspace_name // "Unknown"')
|
|
87
|
+
echo -e "${GREEN}Authenticated to: $WORKSPACE${NC}"
|
|
88
|
+
|
|
89
|
+
echo -e "${YELLOW}Monitoring $CHANNEL (polling every ${INTERVAL}s)...${NC}"
|
|
90
|
+
echo -e "${YELLOW}Press Ctrl+C to stop${NC}"
|
|
91
|
+
echo ""
|
|
92
|
+
|
|
93
|
+
trap 'echo -e "\n${YELLOW}Monitoring stopped${NC}"; exit 0' INT
|
|
94
|
+
|
|
95
|
+
while true; do
|
|
96
|
+
check_messages
|
|
97
|
+
sleep "$INTERVAL"
|
|
98
|
+
done
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
#
|
|
3
|
+
# post-message.sh - Send a message to Slack via bot token with error handling
|
|
4
|
+
#
|
|
5
|
+
# Usage:
|
|
6
|
+
# ./post-message.sh <channel-id> <message>
|
|
7
|
+
#
|
|
8
|
+
# Example:
|
|
9
|
+
# ./post-message.sh C0ACZKTDDC0 "Hello from bot!"
|
|
10
|
+
# ./post-message.sh C0ACZKTDDC0 "Deployment completed"
|
|
11
|
+
|
|
12
|
+
set -euo pipefail
|
|
13
|
+
|
|
14
|
+
if [ $# -lt 2 ]; then
|
|
15
|
+
echo "Usage: $0 <channel-id> <message>"
|
|
16
|
+
echo ""
|
|
17
|
+
echo "Examples:"
|
|
18
|
+
echo " $0 C0ACZKTDDC0 'Hello world!'"
|
|
19
|
+
echo " $0 C0ACZKTDDC0 'Build completed'"
|
|
20
|
+
exit 1
|
|
21
|
+
fi
|
|
22
|
+
|
|
23
|
+
CHANNEL="$1"
|
|
24
|
+
MESSAGE="$2"
|
|
25
|
+
|
|
26
|
+
RED='\033[0;31m'
|
|
27
|
+
GREEN='\033[0;32m'
|
|
28
|
+
YELLOW='\033[1;33m'
|
|
29
|
+
NC='\033[0m'
|
|
30
|
+
|
|
31
|
+
send_message() {
|
|
32
|
+
local channel=$1
|
|
33
|
+
local message=$2
|
|
34
|
+
local max_attempts=3
|
|
35
|
+
local attempt=1
|
|
36
|
+
|
|
37
|
+
while [ $attempt -le $max_attempts ]; do
|
|
38
|
+
echo -e "${YELLOW}Attempt $attempt/$max_attempts...${NC}"
|
|
39
|
+
|
|
40
|
+
RESULT=$(agent-slackbot message send "$channel" "$message" 2>&1)
|
|
41
|
+
TS=$(echo "$RESULT" | jq -r '.ts // ""')
|
|
42
|
+
|
|
43
|
+
if [ -n "$TS" ] && [ "$TS" != "null" ]; then
|
|
44
|
+
echo -e "${GREEN}Message sent!${NC}"
|
|
45
|
+
echo ""
|
|
46
|
+
echo " Channel: $channel"
|
|
47
|
+
echo " Timestamp: $TS"
|
|
48
|
+
return 0
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
ERROR=$(echo "$RESULT" | jq -r '.error // "Unknown error"')
|
|
52
|
+
echo -e "${RED}Failed: $ERROR${NC}"
|
|
53
|
+
|
|
54
|
+
case "$ERROR" in
|
|
55
|
+
*"No credentials"*)
|
|
56
|
+
echo ""
|
|
57
|
+
echo "Run: agent-slackbot auth set xoxb-your-token"
|
|
58
|
+
return 1
|
|
59
|
+
;;
|
|
60
|
+
*"not_in_channel"*)
|
|
61
|
+
echo ""
|
|
62
|
+
echo "Bot is not in this channel. Invite the bot from Slack."
|
|
63
|
+
return 1
|
|
64
|
+
;;
|
|
65
|
+
esac
|
|
66
|
+
|
|
67
|
+
if [ $attempt -lt $max_attempts ]; then
|
|
68
|
+
SLEEP_TIME=$((attempt * 2))
|
|
69
|
+
echo "Retrying in ${SLEEP_TIME}s..."
|
|
70
|
+
sleep $SLEEP_TIME
|
|
71
|
+
fi
|
|
72
|
+
|
|
73
|
+
attempt=$((attempt + 1))
|
|
74
|
+
done
|
|
75
|
+
|
|
76
|
+
echo -e "${RED}Failed after $max_attempts attempts${NC}"
|
|
77
|
+
return 1
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if ! command -v agent-slackbot &> /dev/null; then
|
|
81
|
+
echo -e "${RED}Error: agent-slackbot not found${NC}"
|
|
82
|
+
echo ""
|
|
83
|
+
echo "Install it with:"
|
|
84
|
+
echo " npm install -g agent-messenger"
|
|
85
|
+
exit 1
|
|
86
|
+
fi
|
|
87
|
+
|
|
88
|
+
echo "Checking authentication..."
|
|
89
|
+
AUTH_STATUS=$(agent-slackbot auth status 2>&1)
|
|
90
|
+
VALID=$(echo "$AUTH_STATUS" | jq -r '.valid // false')
|
|
91
|
+
|
|
92
|
+
if [ "$VALID" != "true" ]; then
|
|
93
|
+
echo -e "${RED}Not authenticated!${NC}"
|
|
94
|
+
echo ""
|
|
95
|
+
echo "Run: agent-slackbot auth set xoxb-your-token"
|
|
96
|
+
exit 1
|
|
97
|
+
fi
|
|
98
|
+
|
|
99
|
+
WORKSPACE=$(echo "$AUTH_STATUS" | jq -r '.workspace_name // "Unknown"')
|
|
100
|
+
echo -e "${GREEN}Authenticated to: $WORKSPACE${NC}"
|
|
101
|
+
echo ""
|
|
102
|
+
|
|
103
|
+
echo "Sending message to $CHANNEL..."
|
|
104
|
+
echo "Message: $MESSAGE"
|
|
105
|
+
echo ""
|
|
106
|
+
|
|
107
|
+
send_message "$CHANNEL" "$MESSAGE"
|