{"id":258238,"date":"2021-05-24T17:00:00","date_gmt":"2021-05-24T14:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-send-a-message-to-slack-from-a-bash-script-cloudsavvy-it\/"},"modified":"2021-05-24T17:00:00","modified_gmt":"2021-05-24T14:00:00","slug":"how-to-send-a-message-to-slack-from-a-bash-script-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-send-a-message-to-slack-from-a-bash-script-cloudsavvy-it\/","title":{"rendered":"#How to Send a Message to Slack From a Bash Script \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3ccb8e52fb1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3ccb8e52fb1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-a-message-to-slack-from-a-bash-script-cloudsavvy-it\/#Use_a_Slack_Bot_With_Webhooks\" >Use a Slack Bot With Webhooks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-a-message-to-slack-from-a-bash-script-cloudsavvy-it\/#Set_Up_A_Slack_App_to_Accept_Webhooks\" >Set Up A Slack App to Accept Webhooks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-a-message-to-slack-from-a-bash-script-cloudsavvy-it\/#Using_Formatted_Messages_and_Attachments\" >Using Formatted Messages and Attachments<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Send a Message to Slack From a Bash <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a> \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-307\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/ef33aa83.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/>With the help of a few webhooks, you can turn Slack into a notification center for your command line. There\u2019s a lot you can do with this, from simple message feeds to custom Slack integrations for your app.<\/p>\n<p>This kind of integration works particularly well for notification-based events, like when something updates, or new content goes live somewhere, similar to how RSS feeds operate. You can also make custom integrations for your own apps that work over the same API, even including <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/messaging\/attachments-to-blocks\">interactive dialogs and menus<\/a> to obtain user input.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Use_a_Slack_Bot_With_Webhooks\"><\/span>Use a Slack Bot With Webhooks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Slack has an API that allows you to automate a lot of interactions with your workspace. You can access this API from anywhere that allows you to make\u00a0HTTP requests like <code>POST<\/code>\u00a0and <code>GET<\/code>, which includes bash scripts using the <code>curl<\/code>\u00a0utility, as well as most scripting languages. The <code>curl<\/code>\u00a0utility is installed by default on nearly all Unix distros, and it\u2019s available for download\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/curl.haxx.se\/windows\/\">on Windows<\/a>.<\/p>\n<p>Part of this API includes webhooks, a way for two applications to send each other messages. You\u2019ll be given a webhook URL which you can make a <code>POST<\/code>\u00a0request to, with some data in the form of a JSON object. For example, you could send a quick message with:<\/p>\n<pre id=\"curl-example\" class=\"small_padding very_large_right_padding special_formatting plastic_pre_wrap\">{\"text\":\"Hello, World!\"}<\/pre>\n<p>Your bash script will send this bit of data off to Slack, which will read it and send \u201cHello, World!\u201d to the channel you specify when you set up the webhook.<\/p>\n<p>You\u2019ll need to use your own\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/apps?new_app=1\">Slack App<\/a>, so the message will come from a bot (which you can customize). This is actually a good thing; webhooks are primarily used for notifications, and since a message sent via a webhook won\u2019t be sent from your own account, you will get push notifications for messages sent this way.<\/p>\n<p>You can also send automated messages from your own Slack account in a similar manner, except you\u2019ll need to use the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/methods\/chat.postMessage\">chat.postMessage<\/a>\u00a0API with the <code>as_user<\/code>\u00a0argument set to true. This will require an <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/docs\/oauth\">OAuth 2.0 token<\/a>\u00a0with the <code>chat:write:user<\/code>\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/scopes\">permission scope<\/a>, which you will need to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/methods\/oauth.access\">generate yourself<\/a> prior to using the API. We suggest sticking with webhooks, as they are far easier to manage.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Set_Up_A_Slack_App_to_Accept_Webhooks\"><\/span>Set Up A Slack App to Accept Webhooks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Navigate to Slack\u2019s API portal and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/apps?new_app=1\">create a new Slack app<\/a>. Give this app a username, select the workspace it will belong to, and hit \u201cCreate App.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-290\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/d05c02ef.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"400\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019ll be brought to a homepage where you can add features to your bot. There\u2019s a lot of cool stuff here, like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/slash-commands\">custom <code>\/commands<\/code><\/a>\u00a0and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/interactive-messages\">interactive messages<\/a>, but all you need right now is \u201cIncoming Webhooks.\u201d Click this button, and turn it on.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-295\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/104eab18.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019ll need to register a new webhook for your application to send messages to. Click \u201cAdd New Webhook to Workspace\u201d at the bottom of this screen.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-296\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/0a4a563f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"400\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You will be asked to authorize the application, and select the channel for the messages. These can be public channels or direct messages with other users; you both will see the bot in your DMs and receive notifications.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-297\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/63677e63.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can register multiple webhooks for different channels, but each hook will have a different URL.<\/p>\n<p>Copy your webhook\u2019s URL, and open up your terminal. Paste in:<\/p>\n<pre>curl -X POST -H 'Content-type: application\/json' --data '{\"text\":\"Hello, World!\"}' YOUR_WEBHOOK_URL<\/pre>\n<p>Replacing <code>YOUR_WEBHOOK_URL<\/code>\u00a0with the one Slack gave you. Hit enter and you should receive a \u201cHello, World!\u201d notification and see a new message in the channel you chose.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-298\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/f78b677a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"350\" height=\"149\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can use this <code>curl<\/code>\u00a0command from inside a bash script, and you can configure the JSON object to send whatever data you\u2019d like, including from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.compciv.org\/topics\/bash\/variables-and-substitution\/\">bash variables<\/a>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Formatted_Messages_and_Attachments\"><\/span>Using Formatted Messages and Attachments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-311\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/ba92b9a0.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The message API can do more than just send simple text. If you\u2019d like your messages to be more stylized, you\u2019ll have to add some extra parameters to the JSON object that <code>curl<\/code>\u00a0sends out.<\/p>\n<p>You can use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/messaging\/composing\/formatting#\">markdown<\/a> and other simple formatting in the same way as you would from Slack\u2019s message box. One thing to note though is that for linking to channels and @ing users you will need to specify the channel ID and user ID rather than the plaintext names, or else it will not work.<\/p>\n<p>For everything else, you\u2019ll want to use Slack\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/tools\/block-kit-builder\">block kit builder<\/a>, a WYSIWYG editor for Slack messages that allows you to make interactive and richly embedded content, and even handles creating the JSON for you. Elements like buttons and menus can be configured to send POST requests <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/interactive-messages\">to a URL you specify<\/a>, letting your users talk back to the application sending the message.<\/p>\n<p>The block kit builder isn\u2019t entirely necessary though, since the old methods aren\u2019t being deprecated. If you\u2019d just like to use features like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/docs\/message-attachments\">Attachments<\/a>, you can use the old <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/docs\/messages\/builder\">Message Builder<\/a> to build and preview the JSON.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/289\/how-to-send-a-message-to-slack-from-a-bash-script\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Send a Message to Slack From a Bash Script \u2013 CloudSavvy IT&#8221; With the help of a few webhooks, you can turn Slack into a notification center for your command line. There\u2019s a lot you can do with this, from simple message feeds to custom Slack integrations for your app. This kind of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":258239,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/ef33aa83.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-258238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/258238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=258238"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/258238\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/258239"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=258238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=258238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=258238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}