Steps to send the message to slack using API

  • Create a workspace in slack
  • Go to Slack API document Slack API: Applications | Slack
  • Create an app by using the button above. Choose From Scratch and give your app a name and a workspace to live on.
  • Get Client ID and Secret for later
    On the Basic Information page, scroll down and note the Client ID and Client Secret, which you can find under the App Credentials section. We’ll need these values for later so keep them in a safe place.
  • Add the Postman OAuth Callback URL to your Redirect URLs.
    On the left navigation, click OAuth & Permissions and head down to Redirect URLs. Here, add the following URL to your list of Redirect URLs:
    https://oauth.pstmn.io/v1/callback
    This is the URL that Slack will redirect back to as part of the OAuth flow with a temporary value of code. Postman will do the heavy lifting and exchange it for a token for us.
  • Add at least one scope to your app
    In order to have a bot user added to your app, you must specify at least one bot scope within your app’s settings page. If you’re requesting for more scopes, you’re welcome to add them as well but you will need at least one. To do this, scroll down within the OAuth & Permissions page to Scopes and add the scopes that you want to request. As an example here, we’re adding the chat:write scope.
  • Install the APP
  • Go to slack
  • Go to channel details:
  • Click on Add Apps under the integration tab
  • Add app to Slack
  • Configure the OAuth settings
    we have a Slack App to authorize against, we will setup an OAuth 2.0 client. In Postman’s Authorization menu, select OAuth 2.0 for the type. It’s best if you’re using a Collection as then the token details will be reused for all methods found within that Collection, but you can also do this per method as well. Setup the OAuth client by filling out the form as below:
ItemValueComments
Add auth data toRequest HeadersThis means that the token will be added to the headers of your requests. This is a requirement of the Slack API as you cannot send your token within the body.
Token NameA name of your choiceChoose a name that you can use to keep track of your token.
Grant TypeAuthorization CodeThe Slack API relies on issuing you a code value to exchange for your token. This tells Postman that we are using this paradigm.
Callback URLhttps://oauth.pstmn.io/v1/callbackIf you toggle the Authorize Using Browser checkbox, this’ll automatically populate.
Auth URLhttps://slack.com/oauth/v2/authorizeThe first step of the OAuth flow requires that you direct users to this URL. Setting this here, tells Postman to do this for you.
Access Token URLhttps://slack.com/api/oauth.v2.accessThe last step requires you to provide the code from the previous step and call this method. Setting this here tells Postman to call this URL with the proper values.
Client IDThe value of Client ID from step 2Used in conjunction with the value of code to send to the oauth.v2.access method.
Client SecretThe value of Client Secret from step 2Used in conjunction with the value of code to send to the oauth.v2.access method.
Scopee.g. chat:writeA comma-separated list of OAuth scopes. For a complete list of scopes see here.
Client AuthenticationSend client credentials in the body 
  • Click the Get New Access Token Button and Click the Get New Access Token button and a new browser window should open up asking you for permissions on your workspace. If you accept the scopes requested, you should be automatically redirected back to Postman and your token will have been issued. It will start with xoxb- followed by a series of letters and numbers.

Curl:
curl –location –request GET ”
–header ‘Authorization: Bearer xoxb-4510995298352-4473298820503-OKOt0lH24K5mT6hkPrCyJdj3’

  • Curl to send message in a channel
    curl –location –request POST ‘https://api.slack.com/api/chat.postMessage’
    –header ‘Content-Type: application/x-www-form-urlencoded’
    –header ‘Authorization: Bearer xoxb-4510995298352-4473298820503-OKOt0lH24K5mT6hkPrCyJdj3’
    –header ‘Cookie: b=6c3291d3068a2a597d7f92f1f58b9aac’
    –data-urlencode ‘channel=C04E57V7Y3G’
    –data-urlencode ‘text=Hello, how are you?’
    channel : Get channel id from channel details
  • Response:
  • Slack:

Leave a comment

Your email address will not be published. Required fields are marked *