【问题标题】:How to send direct slack messages to users without a webhook?如何在没有 webhook 的情况下向用户发送直接松弛消息?
【发布时间】:2019-07-09 22:53:52
【问题描述】:

我正在将一些代码从 hipchat 迁移到 slack。我曾经使用一个 hipchat curl 命令将 dm 发送给我想要转换为 slack 的用户:

msg='hello world'

curl --fail -d "$(jq -c -n --arg msg "${msg}" '{"message_format": "text", "message": $msg}')" \
  -H "Content-Type: application/json"  \
  -X POST "https://my.hipchat.server.com/v2/user/$USERS_EMAIL/message?auth_token=$HIPCHAT_TOKEN"

假设我只有一个bot token 和我想向其发送消息的用户帐户的电子邮件(没有设置 webhook)。如何向该用户发送消息?我将使用的确切 curl 结构是什么?

【问题讨论】:

  • 这看起来像您要求我们为您完成所有工作。因此,我投票结束。但是让您开始:您想使用 API 端点 chat.postMessage 发送 DM。用户 ID 作为频道。这是文档:api.slack.com/methods/chat.postMessage

标签: curl slack slack-api hipchat


【解决方案1】:

@Savageman 基本上是正确的。唯一的区别是您需要使用im.open。这是我的工作代码:

msg='the text yall want to send'
user_id="$(curl -X GET -H "Authorization: Bearer $SLACK_TOKEN" \
  -H 'Content-type: application/x-www-form-urlencoded' \
  "https://slack.com/api/users.lookupByEmail?email=$EMAIL" | jq -r .user.id)"

channel_id="$(curl -X POST -H "Authorization: Bearer $SLACK_TOKEN" \
  -H 'Content-type: application/x-www-form-urlencoded' \
  "https://slack.com/api/im.open?user=$user_id" | jq -r .channel.id)"

curl -X POST -H "Authorization: Bearer $SLACK_TOKEN" \
  -H 'Content-type: application/json' \
  --data "$(jq -c -n --arg msg "${msg}" --arg channel "${channel_id}" '{"channel":$channel,"text": $msg}')" \
  https://slack.com/api/chat.postMessage

【讨论】:

    【解决方案2】:

    你不能在一个命令中做到这一点。

    1. 使用users.lookupByEmail 获取用户ID
    2. 确保使用 dm.open 打开 DM(这也会为您提供与该用户直接消息的频道 ID)
    3. chat.postMessage发送消息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多