0

我正在将一些代码从 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"

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

4

2 回答 2

4

@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
于 2019-07-10T17:17:12.013 回答
2

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

  1. 使用获取用户 IDusers.lookupByEmail
  2. 确保 DM 使用打开dm.open(这也将为您提供与该用户直接消息的频道 ID)
  3. 发送消息chat.postMessage
于 2019-07-10T08:45:08.213 回答