Last active
December 27, 2021 07:26
-
-
Save Varun-garg/effdc054d43a152cf47d227277e8893e to your computer and use it in GitHub Desktop.
slack status updater
This file contains 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
#!/usr/bin/ruby | |
require 'net/http' | |
require 'optparse' | |
require 'json' | |
require 'date' | |
API_TOKEN = ENV['SLACK_TOKEN'] | |
OPTS = Hash[ ARGV.join(' ').scan(/--?([^=\s]+)(?: (\S+))?/) ] # regex to arrange args in hash | |
ARGS = ARGV - OPTS.keys.map{|k| "--#{k}"} | |
URL = 'https://slack.com/api/users.profile.set' | |
HEADERS = { | |
'Authorization'=> "Bearer #{API_TOKEN}", | |
'Content-Type' =>'application/json; charset=ISO-8859-1', | |
'Accept'=>'application/json' | |
} | |
SIGNED_OFF = { | |
profile: { | |
status_text: "Signed off, call if urgent.", | |
status_emoji: ":mobile_phone_off:", | |
status_expiration: DateTime.new( DateTime.now.year, DateTime.now.month, DateTime.now.day, 23, 59, 59, DateTime.now.zone).to_time.to_i | |
}, | |
} | |
LUNCH = { | |
profile: { | |
status_text: "Lunch", | |
status_emoji: ":pizza:", | |
status_expiration: DateTime.new( DateTime.now.year, DateTime.now.month, DateTime.now.day, 15, 00, 00, DateTime.now.zone).to_time.to_i | |
}, | |
} | |
uri = URI(URL) | |
STATUS_MAPS = { | |
signed_off: SIGNED_OFF, | |
lunch: LUNCH | |
} | |
unless ARGS[0] | |
STDERR.puts "Require additional status parameter from - [#{STATUS_MAPS.keys.join(', ')}]" | |
Kernel.exit(1) | |
end | |
status_to_update = STATUS_MAPS[ARGS[0].to_sym] | |
puts status_to_update | |
req = Net::HTTP::Post.new(uri, HEADERS) | |
req.body = status_to_update.to_json | |
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http| | |
http.request(req) | |
end | |
puts JSON.parse(res.body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obtaining token (source: https://github.com/yuya373/emacs-slack#how-to-get-token):
window.prompt("your api token is: ", TS.boot_data.api_token)
SLACK_TOKEN
and set it to the tokenExample usage (crontab)