如何 post 所有直接消息频道中的临时消息?
How to post ephemeral messages in all direct messages channels?
我创建了一个 Slack 应用程序,在用户执行操作后,它应该 post 一个带有此操作结果的临时消息。
但是,当尝试将消息发送到安装应用程序的用户不属于的直接消息通道时,我收到 channel_not_found
错误。
使用旧版工作区应用程序,这个问题很容易解决,因为可以邀请 slack 应用程序加入直接消息频道,从而获得 post 加入该频道的权限。然而,由于 Slack 强制采用仅使用机器人和用户令牌的新方法,解决起来似乎并不容易。机器人无法加入直接消息频道,因此无法在这些频道中获得 post 的权限。
有什么直接的方法可以解决这个问题吗?
如果你想 post 一个短暂的消息来响应一个动作,你只需要使用来自 slack 请求的 response_url
并向这个 [=23] 发送一个 POST 请求=] 与包含临时消息的 JSON 有效负载。
有关详细信息,请参阅 https://api.slack.com/actions#Responding to Action。
这是我在 Golang 中的代码(我使用的是 nlopes 的 slack 包:https://godoc.org/github.com/nlopes/slack)
msg := slack.Msg{
Attachments: []slack.Attachment{
attach,
},
ResponseType: "ephemeral",
}
b, err := json.Marshal(msg)
if err != nil {
err = errors.Wrap(err, "Post failed")
logger.LogError(err)
return nil
}
reader := bytes.NewReader(b)
_, err = http.Post(
c.ResponseURL, // the response URL from slack request
"application/json",
reader,
)
我创建了一个 Slack 应用程序,在用户执行操作后,它应该 post 一个带有此操作结果的临时消息。
但是,当尝试将消息发送到安装应用程序的用户不属于的直接消息通道时,我收到 channel_not_found
错误。
使用旧版工作区应用程序,这个问题很容易解决,因为可以邀请 slack 应用程序加入直接消息频道,从而获得 post 加入该频道的权限。然而,由于 Slack 强制采用仅使用机器人和用户令牌的新方法,解决起来似乎并不容易。机器人无法加入直接消息频道,因此无法在这些频道中获得 post 的权限。
有什么直接的方法可以解决这个问题吗?
如果你想 post 一个短暂的消息来响应一个动作,你只需要使用来自 slack 请求的 response_url
并向这个 [=23] 发送一个 POST 请求=] 与包含临时消息的 JSON 有效负载。
有关详细信息,请参阅 https://api.slack.com/actions#Responding to Action。
这是我在 Golang 中的代码(我使用的是 nlopes 的 slack 包:https://godoc.org/github.com/nlopes/slack)
msg := slack.Msg{
Attachments: []slack.Attachment{
attach,
},
ResponseType: "ephemeral",
}
b, err := json.Marshal(msg)
if err != nil {
err = errors.Wrap(err, "Post failed")
logger.LogError(err)
return nil
}
reader := bytes.NewReader(b)
_, err = http.Post(
c.ResponseURL, // the response URL from slack request
"application/json",
reader,
)