You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Slack Python API在消息中提及用户并触发通知?

用Slack Python API实现@用户并触发通知

直接将用户ID拼接到文本中只会显示ID,无法触发@通知,需要使用Slack官方规定的用户提及格式:

  • 基础提及格式:<@用户ID>,Slack会自动将其解析为@用户名,同时触发用户的通知提醒。
  • 自定义显示文本格式:<@用户ID|自定义名称>,可以指定显示的文本,同样会触发通知。

修改后的基础实现代码

client.api_call(
    api_method='chat.postMessage',
    json={'channel': '#staff', 'text': f'<@{user}> This is a test.'}
)

自定义显示文本示例

如果需要显示自定义名称而非默认用户名,代码可调整为:

# user为目标用户ID,custom_name为你想展示的自定义名称
client.api_call(
    api_method='chat.postMessage',
    json={'channel': '#staff', 'text': f'<@{user}|{custom_name}> This is a test.'}
)

内容的提问来源于stack exchange,提问作者Let'sbecool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:30:53