如何添加 Slack 消息按钮以打开与用户的直接聊天?
How to add Slack message button to open direct chat with a user?
我有一个 Slack 机器人需要 return 一条消息,其中包括 Slack 用户列表和一个向每个用户直接发送消息的按钮。
我希望用户的参与体验保持一致。如果用户通过桌面 Slack 应用程序与机器人交互,那么我希望用户留在应用程序中(不要被带到 Web 客户端——他们可能没有经过身份验证)。
类似于:
Results:
User A - click link to view User A's profile in Slack
[Message] - click button to open direct message with user A
User B - click link to view User B's profile in Slack
[Message] - click button to open direct message with user B
我遇到了几个问题:
如何 link 到用户的个人资料,因为我不知道他们是在使用 Slack 应用程序还是 Slack 网络(因此如何决定显示 slack://user?team=&id=..
还是domain.slack.com/team/{id}
方法?这是假设我使用 title_link
或 author_link
属性.
如何创建一个消息按钮来打开直接给用户的消息(甚至更好地打开它并 pre-populate 一条消息)?我目前一直在玩 link 动作,但没有任何运气。
我想我可以通过让机器人发送 DM 然后 @mention 原始用户来解决它。从用户体验的角度来看不太好。
更新
感谢 Adil 和 Erik 的评论,我认为我有 最佳 可能的解决方案。我最终为每个结果制作了一个消息附件,如下所示:
{
"fallback": `${user['name']} likes pizza`,
"text": `<@${user['uid']}>`,
"actions": [
{
"type": "button",
"text": "View Pizza Profile",
"url": url(`/directory/${user['id']}`),
"style": "primary"
},
{
"type": "button",
"text": "Order Pizza",
"url": `https://slack.com/app_redirect?channel=${user['uid']}`
}
]
}
此解决方案提供:
- 包含对用户的@mention 的文本字段(单击它会在浏览器或本机中显示配置文件)。它使用文本作为消息 header(并删除作者和标题字段)。
- Link 操作按钮可通过
app_redirect
打开直接消息聊天
有几个严重的限制:
- 对于本机,直接消息 link 会导致浏览器打开,然后(如果用户决定)重定向回应用程序。
- 用户必须使用浏览器 window,该浏览器已通过与本机客户端相同的工作区的身份验证(这对于 UX 来说是一个巨大的问题,因为许多人打开了多个工作区,在这种情况下重定向失败并显示 non-obvious 错误消息。
要为用户个人资料创建 link,请尝试按照 this page 上的步骤操作:
Create the link by combining your Workspace URL with members’ User IDs. Here’s how:
The first part of the link is the Workspace URL. For example, https://acmeco.slack.com.
The second part of the link is the User ID. Multiple members' User IDs can be found using the users.list
API method. Or, an individual's User ID can be found by clicking the More actions icon in a member's profile, then choosing Copy member ID.
Append the User ID to the end of the Workspace URL. Here’s an example: https://acmeco.slack.com/team/U1H63D8SZ.
使用相同的用户 ID,使用 conversations.open
API method to create a DM/multi-person message with the target user:
from slackclient import SlackClient
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
sc.api_call(
"conversations.open",
users=["W1234567890","U2345678901","U3456789012"]
)
添加 Adil 关于 linking 到用户个人资料的精彩回答
以下是如何提供 link 打开当前用户与另一个用户之间的直接消息:
您可以使用 Slack's channel redirect feature 创建一个 link 来为用户打开任何频道。通过提供目标用户的 ID,这也适用于直接消息渠道。
用户 U12345678
的示例:
https://slack.com/app_redirect?channel=U12345678
您可以通过 Link 按钮或文本 link 提供此 link。
请注意,这将始终打开一个新的浏览器 tab/window,即使用户使用的是 Slack 本机客户端(不,无法确定用户是否使用本机客户端)或浏览器)。
另一种方法是仅在您的消息中提供用户提及,然后让用户单击它以获取包含 "Direct Messages" 的菜单。这将适用于每个用户和每个平台/客户端类型:
只需在您的文本中包含一个标准的用户提及,并向用户提供一些说明:
<@U12345678>
我有一个 Slack 机器人需要 return 一条消息,其中包括 Slack 用户列表和一个向每个用户直接发送消息的按钮。
我希望用户的参与体验保持一致。如果用户通过桌面 Slack 应用程序与机器人交互,那么我希望用户留在应用程序中(不要被带到 Web 客户端——他们可能没有经过身份验证)。
类似于:
Results:
User A - click link to view User A's profile in Slack
[Message] - click button to open direct message with user A
User B - click link to view User B's profile in Slack
[Message] - click button to open direct message with user B
我遇到了几个问题:
如何 link 到用户的个人资料,因为我不知道他们是在使用 Slack 应用程序还是 Slack 网络(因此如何决定显示
slack://user?team=&id=..
还是domain.slack.com/team/{id}
方法?这是假设我使用title_link
或author_link
属性.如何创建一个消息按钮来打开直接给用户的消息(甚至更好地打开它并 pre-populate 一条消息)?我目前一直在玩 link 动作,但没有任何运气。
我想我可以通过让机器人发送 DM 然后 @mention 原始用户来解决它。从用户体验的角度来看不太好。
更新
感谢 Adil 和 Erik 的评论,我认为我有 最佳 可能的解决方案。我最终为每个结果制作了一个消息附件,如下所示:
{
"fallback": `${user['name']} likes pizza`,
"text": `<@${user['uid']}>`,
"actions": [
{
"type": "button",
"text": "View Pizza Profile",
"url": url(`/directory/${user['id']}`),
"style": "primary"
},
{
"type": "button",
"text": "Order Pizza",
"url": `https://slack.com/app_redirect?channel=${user['uid']}`
}
]
}
此解决方案提供:
- 包含对用户的@mention 的文本字段(单击它会在浏览器或本机中显示配置文件)。它使用文本作为消息 header(并删除作者和标题字段)。
- Link 操作按钮可通过
app_redirect
打开直接消息聊天
有几个严重的限制:
- 对于本机,直接消息 link 会导致浏览器打开,然后(如果用户决定)重定向回应用程序。
- 用户必须使用浏览器 window,该浏览器已通过与本机客户端相同的工作区的身份验证(这对于 UX 来说是一个巨大的问题,因为许多人打开了多个工作区,在这种情况下重定向失败并显示 non-obvious 错误消息。
要为用户个人资料创建 link,请尝试按照 this page 上的步骤操作:
Create the link by combining your Workspace URL with members’ User IDs. Here’s how:
The first part of the link is the Workspace URL. For example, https://acmeco.slack.com. The second part of the link is the User ID. Multiple members' User IDs can be found using the
users.list
API method. Or, an individual's User ID can be found by clicking the More actions icon in a member's profile, then choosing Copy member ID. Append the User ID to the end of the Workspace URL. Here’s an example: https://acmeco.slack.com/team/U1H63D8SZ.
使用相同的用户 ID,使用 conversations.open
API method to create a DM/multi-person message with the target user:
from slackclient import SlackClient
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
sc.api_call(
"conversations.open",
users=["W1234567890","U2345678901","U3456789012"]
)
添加 Adil 关于 linking 到用户个人资料的精彩回答
以下是如何提供 link 打开当前用户与另一个用户之间的直接消息:
您可以使用 Slack's channel redirect feature 创建一个 link 来为用户打开任何频道。通过提供目标用户的 ID,这也适用于直接消息渠道。
用户 U12345678
的示例:
https://slack.com/app_redirect?channel=U12345678
您可以通过 Link 按钮或文本 link 提供此 link。
请注意,这将始终打开一个新的浏览器 tab/window,即使用户使用的是 Slack 本机客户端(不,无法确定用户是否使用本机客户端)或浏览器)。
另一种方法是仅在您的消息中提供用户提及,然后让用户单击它以获取包含 "Direct Messages" 的菜单。这将适用于每个用户和每个平台/客户端类型:
只需在您的文本中包含一个标准的用户提及,并向用户提供一些说明:
<@U12345678>