slack-api – Slack Incoming Web Hooks – 发送消息

参考:https://api.slack.com/messaging/webhooks

1. Create a Slack app

相当于创建一个虚拟的用户,slack消息的发布者显示为该slack app的名字。

2. Enable Incoming Webhooks

3. Create an Incoming Webhook

通过Add New Webhook to Workspace可以绑定一个Channel并得到对应的HOOK_URL,可以绑定多个Channel并得到多个对应的HOOK_URL。

HOOK_URL 类似 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

4. Use your Incoming Webhook URL to post a message

通过以下命令可以往HOOK_URL对应的Channel发送消息。

bash:

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' HOOK_URL

发送@channel消息的正确语法是<!channel>. 

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hey, Its time for meeting! ","link_names" : 1}' HOOK_URL

c++:

  std::string command = "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"Hello, World!\"}' https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX";
  int status = std::system(command.c_str());
  if (status) {
    std::cout << "Fail to send message !" << std::endl;
  }

 

你可能感兴趣的:(Linux)