Gradio 代理与 MCP 黑客马拉松
获奖者Gradio 代理与 MCP 黑客马拉松
获奖者你可以将 Gradio 应用作为一个 Slack 机器人,让你的 Slack 工作区用户直接与它互动。
Slack 机器人将监听频道中提及它的消息。当它收到一条消息(可以包含文本和文件)时,它将通过 Gradio 的内置 API 将其发送到你的 Gradio 应用。你的机器人将回复从 API 接收到的响应。
由于 Gradio 的 API 非常灵活,你可以非常轻松地创建支持文本、图像、音频、流媒体、聊天历史记录以及各种其他功能的 Slack 机器人。
gradio
和 slack-bolt
库pip install --upgrade gradio slack-bolt~=1.0
现在,我们准备开始!
app_mentions:read
chat:write
files:read
files:write
xoxb-
开头),我们稍后会用到它。socket-token
,并复制生成的令牌(以 xapp-
开头),我们稍后会用到它。app_mention
机器人事件。让我们从编写一个非常简单的 Slack 机器人开始,以确保一切正常。将以下 Python 代码写入名为 bot.py
的文件中,并粘贴上一节中第 6 步和第 8 步的两个令牌。
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
SLACK_BOT_TOKEN = # PASTE YOUR SLACK BOT TOKEN HERE
SLACK_APP_TOKEN = # PASTE YOUR SLACK APP TOKEN HERE
app = App(token=SLACK_BOT_TOKEN)
@app.event("app_mention")
def handle_app_mention_events(body, say):
user_id = body["event"]["user"]
say(f"Hi <@{user_id}>! You mentioned me and said: {body['event']['text']}")
if __name__ == "__main__":
handler = SocketModeHandler(app, SLACK_APP_TOKEN)
handler.start()
如果这部分工作正常,我们就可以添加 Gradio 特定的代码了。我们将使用 Gradio Python 客户端 来查询上面提到的 Gradio Playground Space。这是更新后的 bot.py
文件:
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
SLACK_BOT_TOKEN = # PASTE YOUR SLACK BOT TOKEN HERE
SLACK_APP_TOKEN = # PASTE YOUR SLACK APP TOKEN HERE
app = App(token=SLACK_BOT_TOKEN)
gradio_client = Client("abidlabs/gradio-playground-bot")
def download_image(url, filename):
headers = {"Authorization": f"Bearer {SLACK_BOT_TOKEN}"}
response = httpx.get(url, headers=headers)
image_path = f"./images/{filename}"
os.makedirs("./images", exist_ok=True)
with open(image_path, "wb") as f:
f.write(response.content)
return image_path
def slackify_message(message):
# Replace markdown links with slack format and remove code language specifier after triple backticks
pattern = r'\[(.*?)\]\((.*?)\)'
cleaned = re.sub(pattern, r'<\2|\1>', message)
cleaned = re.sub(r'```\w+\n', '```', cleaned)
return cleaned.strip()
@app.event("app_mention")
def handle_app_mention_events(body, say):
# Extract the message content without the bot mention
text = body["event"]["text"]
bot_user_id = body["authorizations"][0]["user_id"]
clean_message = text.replace(f"<@{bot_user_id}>", "").strip()
# Handle images if present
files = []
if "files" in body["event"]:
for file in body["event"]["files"]:
if file["filetype"] in ["png", "jpg", "jpeg", "gif", "webp"]:
image_path = download_image(file["url_private_download"], file["name"])
files.append(handle_file(image_path))
break
# Submit to Gradio and send responses back to Slack
for response in gradio_client.submit(
message={"text": clean_message, "files": files},
):
cleaned_response = slackify_message(response[-1])
say(cleaned_response)
if __name__ == "__main__":
handler = SocketModeHandler(app, SLACK_APP_TOKEN)
handler.start()
现在,在你的 Slack 工作区中创建一个新频道或导航到现有频道,你希望在那里使用机器人。点击 Slack 侧边栏中“Channels”(频道)旁边的“+”按钮,然后按照提示创建一个新频道。
最后,邀请你的机器人加入频道
/invite @YourBotName
现在你可以在机器人所在的任何频道中提及它,选择性地附加一张图片,它将用生成的 Gradio 应用代码进行响应!
机器人将:
这只是一个基本示例——你可以扩展它来处理更多类型的文件、添加错误处理,或与不同的 Gradio 应用集成!
如果你从 Gradio 应用构建了 Slack 机器人,请随时在 X 上分享并@ Gradio 账号,我们很乐意帮助你进行推广!