Created
January 31, 2021 00:45
-
-
Save jotapeluiz/f851896e0ddfcab8449f1caf588e1690 to your computer and use it in GitHub Desktop.
Small ruby script that sends a text message to a Slack channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'uri' | |
| require 'json' | |
| require 'net/http' | |
| # Create a slack app https://api.slack.com/start | |
| # More information in https://api.slack.com/messaging/sending | |
| ACCESS_TOKEN = '<access token of your app>' | |
| SLACK_URL_POST = 'https://slack.com/api/chat.postMessage' | |
| def send_message(channel_id, text) | |
| headers = { 'Content-Type': 'application/json', 'Authorization': "Bearer #{ACCESS_TOKEN}" } | |
| request = Net::HTTP::Post.new(uri.request_uri, headers) | |
| request.body = { channel: channel_id, text: text }.to_json | |
| uri = URI(SLACK_URL_POST) | |
| https = Net::HTTP.new(uri.host, uri.port) | |
| https.use_ssl = true | |
| https.request(request) | |
| end | |
| # Put the channel id where desire send a message | |
| # https://www.wikihow.com/Find-a-Channel-ID-on-Slack-on-PC-or-Mac | |
| response = send_message('<channel id>', 'Hello channel!') | |
| puts 'Response:' | |
| p JSON.parse(response.body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment