网淘吧来吧,欢迎您!

返回首页 微信
微信
手机版
手机版

Slack

2026-03-27 新闻来源:网淘吧 围观:21
电脑广告
手机广告

Slack

通过托管的 OAuth 认证访问 Slack API。发送消息、管理频道、列出用户并自动化 Slack 工作流。

快速入门

# Post a message to a channel
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'channel': 'C0123456789', 'text': 'Hello from Maton!'}).encode()
req = urllib.request.Request('https://gateway.maton.ai/slack/api/chat.postMessage', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

基础 URL

https://gateway.maton.ai/slack/{method}

网关将请求代理至slack.com并自动注入您的 OAuth 令牌。

认证

所有请求均需在 Authorization 请求头中包含 Maton API 密钥:

Authorization: Bearer $MATON_API_KEY

环境变量:将您的 API 密钥设置为MATON_API_KEY

export MATON_API_KEY="YOUR_API_KEY"

获取您的 API 密钥

  1. 登录或前往maton.ai
  2. 创建账户前往
  3. maton.ai/settings

复制您的 API 密钥

连接管理.

列出连接

python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections?app=slack&status=ACTIVE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

创建连接

python <<'EOF'
import urllib.request, os, json
data = json.dumps({'app': 'slack'}).encode()
req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

获取连接

python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

响应:

{
  "connection": {
    "connection_id": "21fd90f9-5935-43cd-b6c8-bde9d915ca80",
    "status": "ACTIVE",
    "creation_time": "2025-12-08T07:20:53.488460Z",
    "last_updated_time": "2026-01-31T20:03:32.593153Z",
    "url": "https://connect.maton.ai/?session_token=...",
    "app": "slack",
    "metadata": {}
  }
}

在浏览器中打开返回的url以完成 OAuth 授权。

删除连接

python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}', method='DELETE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

指定连接

如果您有多个 Slack 连接,请使用Maton-Connection请求头指定要使用哪一个:

python <<'EOF'
import urllib.request, os, json
data = json.dumps({'channel': 'C0123456789', 'text': 'Hello!'}).encode()
req = urllib.request.Request('https://gateway.maton.ai/slack/api/chat.postMessage', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Maton-Connection', '21fd90f9-5935-43cd-b6c8-bde9d915ca80')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

如果省略,网关将使用默认(最早创建的)活动连接。


API 参考

认证

认证测试

GET /slack/api/auth.test

返回当前用户和团队信息。


消息

发布消息

POST /slack/api/chat.postMessage
Content-Type: application/json

{
  "channel": "C0123456789",
  "text": "Hello, world!"
}

使用消息块:

POST /slack/api/chat.postMessage
Content-Type: application/json

{
  "channel": "C0123456789",
  "blocks": [
    {"type": "section", "text": {"type": "mrkdwn", "text": "*Bold* and _italic_"}}
  ]
}

发布线程回复

POST /slack/api/chat.postMessage
Content-Type: application/json

{
  "channel": "C0123456789",
  "thread_ts": "1234567890.123456",
  "text": "This is a reply in a thread"
}

更新消息

POST /slack/api/chat.update
Content-Type: application/json

{
  "channel": "C0123456789",
  "ts": "1234567890.123456",
  "text": "Updated message"
}

删除消息

POST /slack/api/chat.delete
Content-Type: application/json

{
  "channel": "C0123456789",
  "ts": "1234567890.123456"
}

定时发送消息

POST /slack/api/chat.scheduleMessage
Content-Type: application/json

{
  "channel": "C0123456789",
  "text": "Scheduled message",
  "post_at": 1734567890
}

查看定时消息列表

GET /slack/api/chat.scheduledMessages.list

删除定时消息

POST /slack/api/chat.deleteScheduledMessage
Content-Type: application/json

{
  "channel": "C0123456789",
  "scheduled_message_id": "Q1234567890"
}

获取永久链接

GET /slack/api/chat.getPermalink?channel=C0123456789&message_ts=1234567890.123456

对话(频道)

列出频道

GET /slack/api/conversations.list?types=public_channel,private_channel&limit=100

类型:公共频道,私有频道,私信,群组私信

获取频道信息

GET /slack/api/conversations.info?channel=C0123456789

获取频道历史记录

GET /slack/api/conversations.history?channel=C0123456789&limit=100

带时间范围:

GET /slack/api/conversations.history?channel=C0123456789&oldest=1234567890&latest=1234567899

获取主题回复

GET /slack/api/conversations.replies?channel=C0123456789&ts=1234567890.123456

获取频道成员

GET /slack/api/conversations.members?channel=C0123456789&limit=100

创建频道

POST /slack/api/conversations.create
Content-Type: application/json

{
  "name": "new-channel-name",
  "is_private": false
}

加入频道

POST /slack/api/conversations.join
Content-Type: application/json

{
  "channel": "C0123456789"
}

离开频道

POST /slack/api/conversations.leave
Content-Type: application/json

{
  "channel": "C0123456789"
}

归档频道

POST /slack/api/conversations.archive
Content-Type: application/json

{
  "channel": "C0123456789"
}

取消归档频道

POST /slack/api/conversations.unarchive
Content-Type: application/json

{
  "channel": "C0123456789"
}

重命名频道

POST /slack/api/conversations.rename
Content-Type: application/json

{
  "channel": "C0123456789",
  "name": "new-name"
}

设置频道主题

POST /slack/api/conversations.setTopic
Content-Type: application/json

{
  "channel": "C0123456789",
  "topic": "Channel topic here"
}

设置频道目的

POST /slack/api/conversations.setPurpose
Content-Type: application/json

{
  "channel": "C0123456789",
  "purpose": "Channel purpose here"
}

邀请加入频道

POST /slack/api/conversations.invite
Content-Type: application/json

{
  "channel": "C0123456789",
  "users": "U0123456789,U9876543210"
}

从频道中踢出

POST /slack/api/conversations.kick
Content-Type: application/json

{
  "channel": "C0123456789",
  "user": "U0123456789"
}

标记频道为已读

POST /slack/api/conversations.mark
Content-Type: application/json

{
  "channel": "C0123456789",
  "ts": "1234567890.123456"
}

私信

打开私信对话

POST /slack/api/conversations.open
Content-Type: application/json

{
  "users": "U0123456789"
}

对于群组私信:

POST /slack/api/conversations.open
Content-Type: application/json

{
  "users": "U0123456789,U9876543210"
}

列出私信频道

GET /slack/api/conversations.list?types=im

列出群组私信频道

GET /slack/api/conversations.list?types=mpim

我的对话

GET /slack/api/users.conversations?limit=100

用户

列出用户

GET /slack/api/users.list?limit=100

获取用户信息

GET /slack/api/users.info?user=U0123456789

获取用户状态

GET /slack/api/users.getPresence?user=U0123456789

设置用户状态

POST /slack/api/users.setPresence
Content-Type: application/json

{
  "presence": "away"
}

通过电子邮件查找用户

GET /slack/api/users.lookupByEmail?email=user@example.com

反应

添加反应

POST /slack/api/reactions.add
Content-Type: application/json

{
  "channel": "C0123456789",
  "name": "thumbsup",
  "timestamp": "1234567890.123456"
}

移除反应

POST /slack/api/reactions.remove
Content-Type: application/json

{
  "channel": "C0123456789",
  "name": "thumbsup",
  "timestamp": "1234567890.123456"
}

获取消息上的反应

GET /slack/api/reactions.get?channel=C0123456789&timestamp=1234567890.123456

列出我的反应

GET /slack/api/reactions.list?limit=100

星标

列出星标

GET /slack/api/stars.list?limit=100

添加星标

POST /slack/api/stars.add
Content-Type: application/json

{
  "channel": "C0123456789",
  "timestamp": "1234567890.123456"
}

移除星标

POST /slack/api/stars.remove
Content-Type: application/json

{
  "channel": "C0123456789",
  "timestamp": "1234567890.123456"
}

机器人

获取机器人信息

GET /slack/api/bots.info?bot=B0123456789

文件

上传文件

POST /slack/api/files.upload
Content-Type: multipart/form-data

channels=C0123456789
content=file content here
filename=example.txt
title=Example File

上传文件 v2(获取上传URL)

GET /slack/api/files.getUploadURLExternal?filename=example.txt&length=1024

完成文件上传

POST /slack/api/files.completeUploadExternal
Content-Type: application/json

{
  "files": [{"id": "F0123456789", "title": "My File"}],
  "channel_id": "C0123456789"
}

删除文件

POST /slack/api/files.delete
Content-Type: application/json

{
  "file": "F0123456789"
}

获取文件信息

GET /slack/api/files.info?file=F0123456789

搜索

搜索消息

GET /slack/api/search.messages?query=keyword

代码示例

JavaScript

const response = await fetch('https://gateway.maton.ai/slack/api/chat.postMessage', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${process.env.MATON_API_KEY}`
  },
  body: JSON.stringify({ channel: 'C0123456', text: 'Hello!' })
});
const result = await response.json();
console.log(result);

Python

import os
import requests

response = requests.post(
    'https://gateway.maton.ai/slack/api/chat.postMessage',
    headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'},
    json={'channel': 'C0123456', 'text': 'Hello!'}
)
print(response.json())

备注

  • 频道ID:C(公开),G(私有/群组),D(私信)
  • 用户ID以U开头,机器人ID以B开头,团队ID以T
  • 消息时间戳 (ts) 是唯一的标识符
  • 使用mrkdwn类型来实现Slack风格的Markdown格式化
  • 线程回复使用thread_ts来引用父消息
  • 基于游标的分页:使用cursorresponse_metadata.next_cursor

Shell 笔记

  • 重要提示:在使用curl命令时,如果URL包含方括号 (fields[]sort[]records[]),请使用curl -g以禁用通配符解析
  • 重要提示:当将curl输出通过管道传递给jq或其他命令时,某些Shell环境中环境变量如$MATON_API_KEY可能无法正确展开。通过管道传输时,您可能会收到"无效的API密钥"错误。

错误处理

状态码含义
400缺少Slack连接
401Maton API密钥无效或缺失
429请求频率受限(每个账户每秒10次请求)
4xx/5xx来自Slack API的透传错误

缺少权限范围错误:如果您遇到missing_scope错误,请联系Maton技术支持为您的连接申请额外的权限范围。

故障排除:API密钥问题

  1. 请检查MATON_API_KEY环境变量已设置:
echo $MATON_API_KEY
  1. 通过列出连接来验证 API 密钥是否有效:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF

故障排除:应用名称无效

  1. 确保您的 URL 路径以slack开头。例如:
  • 正确示例:https://gateway.maton.ai/slack/api/chat.postMessage
  • 错误示例:https://gateway.maton.ai/api/chat.postMessage

资源

免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏
文章底部电脑广告
手机广告位-内容正文底部
上一篇:Image Editing 下一篇:Faster Whisper

相关文章

您是本站第289849名访客 今日有231篇新文章/评论