This blog is about the How to Send Customized Messages to Slack From your App. We will try our best so that you understand this guide . I hope you guys like this blog, How to Send Customized Messages to Slack From your App. If your answer is yes after reading the article, please share this article with your friends and family to support us.
Check How to Send Customized Messages to Slack From your App
Slack is a popular messaging app used by many teams. It comes with many services and an API for developers to integrate with their applications. In today’s post we will see how to use one of their services called Incoming Webhooks, to send data to slack from an external application.
This way we can easily send messages to Slack from any application we already have; we can send reports, updates, news, notifications and more. For this post, I have used JavaScript in the example. To get started, log into your team’s Slack account.
1. Configure the integration
You will first need to set up an inbound webhook integration. Go to yourteam.slack.com/apps/build/custom-integration and click Incoming webhooks, then select a channel or user to which you want to post your messages (this selection can be overridden later in the code).
Once done, you will see the configuration page for the incoming webhook integration.
Scroll down and there will be a Webhook URL in the format https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/token. Save that URL somewhere, we’ll need it later. You can change the icon and name of the integration further on this page, but we’ll do it in code.
2. Create the message
Let’s say you’ve already built a web app that looks up Valentine’s Day sales on popular sites as well as offer codes to use during the sale, and for some reason you want to share this result with your Slack team members. .
All we have to do now is use the webhook URL created in the previous step and send it a request from your application with JSON data, which will craft the sales offer message.
Let’s first put together the JSON string that will become the Slack message. The parameter that carries the JSON data is called the payload, so the JSON string should look like this:
var myJSONStr = “payload = {” username “:” SALE BOT “,” icon_url “:” example.com/img/icon.jpg “,” channel “:” #general “}”
icon_url is the URL of the image that will appear as a profile picture, you can also use icon_emoji to display an emoji as a profile picture, for example, “icon_emoji”: “: gift:”. “Channel” specifies the channel or username that will see your message. For the username, use the syntax “@username”, for the channel “#channelname”.
Now for the actual message; you can add the “text” property and write your message as its value and finish with it, or use the property called “attachment” to add rich-formatted text, which is what we’ll do now.
The “attachment” property of the payload is as follows:
“attached files”: , “Text”: “Just click on the site names and start shopping. Get * extra reduction with offer code *, if provided.”, “Thumb_url”: “http://example.com/ thumbnail.jpg “}]
“Backup” is the alternate text that is displayed when the Slack message is viewed in an application that does not support message attachments (such as mobile notifications).
“Color” is the color of the left border of the message.
“Pretext” is the text that is displayed before the main content.
“Author_link” is the URL hyperlinked in the author’s name (if provided).
“Mrkdwn_in” is an array of property names whose values are displayed formatted in the message, according to markdown syntax like
for bold and (_) for italics. The three possible values for “mrkdwn_in” are “text”, “pretext” and “fields”
“Thumb_url” is the URL of the thumbnail image.
This is what the message will look like so far.
Now let’s add the fields to the attachment matrix, which will display the sites and offer codes in two columns. “fields”:
,
Use n to add a newline and syntax
Underlining is used to format text in italics.
short is set to true if the values should be displayed side by side (as if it were short). Together JSONString will look like this (keep string on single line in actual working code) var myJSONStr = “payload = {” username “:” SALE BOT “,” icon_url “:” example.com/img/icon.jpg”,”attachments “: , “Mrkdwn_in”:
, “Text”: “Just click on the site names and start shopping. Get * extra reduction with offer code *, if provided.”, “Thumb_url”: “http://example.com/ thumbnail.jpg ”}]} ‘;
3. Post the request
Now to make the post request in JavaScript, use the below function:
function postMessageToSlack () {var xmlhttp = new XMLHttpRequest (), webhook_url = url-you-save-from-before, myJSONStr = json-string-from-above; xmlhttp.open (‘POST’, webhook_url, false); xmlhttp. setRequestHeader (‘Content-Type’, ‘application / x-www-form-urlencoded’); xmlhttp.send (myJSONStr);} Add this feature to a button click or page load
to see it working.
The final output will look like this:
Final remarks: How to Send Customized Messages to Slack From your App
I hope you understand this article, How to Send Customized Messages to Slack From your App. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.