Post a message to slack using https://slack.com/api/chat.postMessage

18,960

Solution 1

Add your parameters to the end of Slack's chat.postMessage endpoint like this:

http://slack.com/api/chat.postMessage?token=XXX&channel=XXX&text=XXX

Then make a GET request to that URL to post your message. Personally I'd suggest doing this as a Node application and using the request package obtained via npm. Makes it very easy.

Post message to Slack in a Node App

  1. Create a new node project and then change to that folder on the command line

  2. On the command line type npm install -g request to install the request module for your project

  3. Inside the index.js file (or wherever you plan on calling the API) do as follows:

    //Import request module
    var request = require('request');
    
    //Replace your token, channelID and text here
    var path_to_call = 'http://slack.com/api/chat.postMessage?token=XXX&channel=XXX&text=XXX';
    
    request(path_to_call, function(error, response, body) {
         if (!error && response.statusCode == 200) { 
             console.log('Success');
         } else { 
             console.log(error);
         }
    });
    

Solution 2

var url = "https://slack.com/api/chat.postMessage";
var auth_token = auth_token; //Your Bot's auth token
var headers = {
   "Authorization": "Bearer " + auth_token,
   "Content-Type" : "application/json"
}
var body = {
    channel: userSlackId, // Slack user or channel, where you want to send the message
    text: "Your text goes here."
}
request.post({
   "url": url,
   "headers": headers,
   "body": JSON.stringify(body)
}, (err, response, body) => {
   if (err) {
       reject(err);
   }
   console.log("response: ", JSON.stringify(response));
   console.log("body: ",body);
});

You have to set headers as Authorization, and add Bearer before your token as it is mentioned in slack docs. Also, send user/channel in body. Here I'm providing the link for the same for your reference https://api.slack.com/methods/chat.postMessage#channels . Hope this helps.

Solution 3

If you just want to post messages I would recommend to use an Incoming Webhook. They are specifically designed for that purpose and easier to use than API calls.

An Incoming webhook is a custom URL that you can create for your Slack team and then use to send messages into any channel. For sending a message you only need to submit your message in JSON format along with some parameters as POST request to your webhook URL.

If you are using PHP scripting on your website then you best use CURL for the call.

Check out the documentation for details on how to use it.

Solution 4

Not sure which language you're using, but if using Postman to test, you can try the following format.

raw Postman request

POST /api/chat.postMessage HTTP/1.1
Host: slack.com
Content-Type: application/json
Cache-Control: no-cache

{
    "text": "This is a line of text.\nAnd this is another one.",
    "token": "XXXX",
    "channel": "XXXX",
}
Share:
18,960
PronobiS
Author by

PronobiS

Updated on June 27, 2022

Comments

  • PronobiS
    PronobiS almost 2 years

    I want to post a message to slack on x channel I need to send the following x parameters how do I send the following parameters to a website

    "channel": "XXXXX", "token": "token", "text": "text"

  • Akshay Mahajan
    Akshay Mahajan almost 6 years
    This seems to work only in Postman and not in actual code. it gives error Failed to load https://slack.com/api/chat.postMessage: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
  • Ashish kushwaha
    Ashish kushwaha about 3 years
    As OP has not mentioned any specific language, given solution is with Node.js and request library.