-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_slack_example.py
More file actions
261 lines (192 loc) · 7.36 KB
/
plugin_slack_example.py
File metadata and controls
261 lines (192 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""Example: Using the plugin system with Slack integration.
This example demonstrates how to use AgentMind's plugin system
to integrate with Slack for agent communication.
"""
import asyncio
import os
from agentmind import Agent, AgentMind
from agentmind.llm import LiteLLMProvider
from agentmind.plugins import PluginManager
async def example_basic_slack():
"""Basic Slack integration example."""
print("=== Basic Slack Integration ===\n")
# Initialize plugin manager
manager = PluginManager()
# Discover and register plugins
manager.discover_and_load()
# Load Slack plugin
slack_config = {"token": os.getenv("SLACK_BOT_TOKEN"), "channel": os.getenv("SLACK_CHANNEL_ID")}
success = await manager.load_plugin("slack", slack_config)
if not success:
print("Failed to load Slack plugin")
return
# Get plugin instance
slack = manager.get_plugin("slack")
# Connect to Slack
await slack.connect()
# Send a message
response = await slack.send_message("Hello from AgentMind!")
print(f"Message sent: {response['ts']}\n")
# Unload plugin
await manager.unload_plugin("slack")
async def example_agent_with_slack():
"""Agent that communicates via Slack."""
print("=== Agent with Slack Integration ===\n")
# Initialize components
llm = LiteLLMProvider(model="gpt - 4")
mind = AgentMind(llm_provider=llm)
# Create agent
assistant = Agent(
name="SlackAssistant",
role="assistant",
system_prompt="You are a helpful assistant that responds via Slack.",
)
mind.add_agent(assistant)
# Initialize plugin manager
manager = PluginManager()
manager.discover_and_load()
slack_config = {"token": os.getenv("SLACK_BOT_TOKEN"), "channel": os.getenv("SLACK_CHANNEL_ID")}
await manager.load_plugin("slack", slack_config)
slack = manager.get_plugin("slack")
await slack.connect()
# Get recent messages from Slack
messages = await slack.get_channel_history(slack_config["channel"], limit=10)
# Process latest message
if messages:
latest = messages[0]
user_message = latest.get("text", "")
if user_message and not latest.get("bot_id"):
print(f"User message: {user_message}")
# Generate response with agent
result = await mind.collaborate(user_message, max_rounds=1)
response = result.final_output
print(f"Agent response: {response}")
# Send response to Slack
await slack.send_message(response)
# Add reaction to original message
await slack.add_reaction(slack_config["channel"], latest["ts"], "white_check_mark")
await manager.unload_plugin("slack")
async def example_slack_bot():
"""Full Slack bot with multiple agents."""
print("=== Slack Bot with Multiple Agents ===\n")
# Initialize LLM and agents
llm = LiteLLMProvider(model="gpt - 4")
mind = AgentMind(llm_provider=llm)
# Create specialized agents
greeter = Agent(
name="Greeter", role="greeting", system_prompt="You greet users warmly and professionally."
)
helper = Agent(
name="Helper",
role="assistance",
system_prompt="You provide helpful information and assistance.",
)
mind.add_agent(greeter)
mind.add_agent(helper)
# Setup Slack plugin
manager = PluginManager()
manager.discover_and_load()
slack_config = {"token": os.getenv("SLACK_BOT_TOKEN"), "channel": os.getenv("SLACK_CHANNEL_ID")}
await manager.load_plugin("slack", slack_config)
slack = manager.get_plugin("slack")
await slack.connect()
# Send greeting
greeting_result = await mind.collaborate(
"Generate a friendly greeting for the team", max_rounds=1
)
await slack.send_message(greeting_result.final_output)
# List available commands
commands_message = """
*Available Commands:*
• `help` - Show this help message
• `ask <question>` - Ask the assistant a question
• `status` - Check bot status
"""
await slack.send_message(commands_message)
await manager.unload_plugin("slack")
async def example_slack_rich_messages():
"""Send rich messages with Slack blocks."""
print("=== Slack Rich Messages ===\n")
manager = PluginManager()
manager.discover_and_load()
slack_config = {"token": os.getenv("SLACK_BOT_TOKEN"), "channel": os.getenv("SLACK_CHANNEL_ID")}
await manager.load_plugin("slack", slack_config)
slack = manager.get_plugin("slack")
await slack.connect()
# Create rich message with blocks
blocks = [
{"type": "header", "text": {"type": "plain_text", "text": "AgentMind Status Report"}},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "*Status:* All systems operational"},
},
{
"type": "section",
"fields": [
{"type": "mrkdwn", "text": "*Active Agents:* 3"},
{"type": "mrkdwn", "text": "*Tasks Completed:* 42"},
],
},
{"type": "divider"},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "Need help? Type `@agentmind help`"},
},
]
await slack.send_rich_message(slack_config["channel"], blocks, text="AgentMind Status Report")
await manager.unload_plugin("slack")
async def example_slack_file_upload():
"""Upload files to Slack."""
print("=== Slack File Upload ===\n")
manager = PluginManager()
manager.discover_and_load()
slack_config = {"token": os.getenv("SLACK_BOT_TOKEN"), "channel": os.getenv("SLACK_CHANNEL_ID")}
await manager.load_plugin("slack", slack_config)
slack = manager.get_plugin("slack")
await slack.connect()
# Upload a file
file_path = "report.txt"
if os.path.exists(file_path):
await slack.upload_file(
file_path,
slack_config["channel"],
title="Agent Report",
comment="Here's the latest report from AgentMind",
)
print(f"Uploaded {file_path} to Slack")
else:
print(f"File not found: {file_path}")
await manager.unload_plugin("slack")
async def example_list_channels():
"""List all Slack channels."""
print("=== List Slack Channels ===\n")
manager = PluginManager()
manager.discover_and_load()
slack_config = {"token": os.getenv("SLACK_BOT_TOKEN")}
await manager.load_plugin("slack", slack_config)
slack = manager.get_plugin("slack")
await slack.connect()
# List channels
channels = await slack.list_channels()
print("Available channels:")
for channel in channels:
print(f" - {channel['name']} (ID: {channel['id']})")
await manager.unload_plugin("slack")
async def main():
"""Run all examples."""
print("Slack Plugin Examples\n")
print("=" * 50 + "\n")
print("Note: Set SLACK_BOT_TOKEN and SLACK_CHANNEL_ID environment variables.\n")
# Check if credentials are available
if not os.getenv("SLACK_BOT_TOKEN"):
print("SLACK_BOT_TOKEN not set. Skipping examples.")
return
# Uncomment to run examples
# await example_basic_slack()
# await example_agent_with_slack()
# await example_slack_bot()
# await example_slack_rich_messages()
# await example_slack_file_upload()
# await example_list_channels()
if __name__ == "__main__":
asyncio.run(main())