如何在没有 webhook 的情况下直接向用户发送 slack 消息?

Ale*_*hen 1 curl slack-api hipchat slack

我正在将一些代码从 hipchat 迁移到 Slack。我用一个 hipchat curl 命令向我想要转换为 slack 的用户发送 DM:

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"
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*hen 5

@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
Run Code Online (Sandbox Code Playgroud)