Created
May 1, 2017 19:02
-
-
Save droberts-sea/11aa26eaa1f1933cd534ba13e16a7ad8 to your computer and use it in GitHub Desktop.
Revisions
-
Dan Roberts created this gist
May 1, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ require 'httparty' TOKEN = "xoxp-105525597987-111410440576-176854780516-7dcef01f9a30d7b808a9ebce3248439d" BASE_URL = "https://slack.com/api/" class SlackException < StandardError end class SlackChannel attr_reader :name, :raw_data def initialize(data) @name = data["name"] @raw_data = data end def send(message) # Extra data! query_params = { "token" => TOKEN, "channel" => @name, "text" => message, "username" => "Roberts-Robit", "icon_emoji" => ":robot_face:", "as_user" => "false" } url = "#{BASE_URL}chat.postMessage" response = HTTParty.post(url, query: query_params) if response["ok"] puts "Everything went swell" else raise SlackException.new(response["error"]) end end def self.all url = "#{BASE_URL}channels.list?token=#{TOKEN}" response = HTTParty.get(url).parsed_response if response["ok"] channel_list = response["channels"].map do |channel_data| self.new(channel_data) end return channel_list else raise SlackException.new(response["error"]) end end end puts "Welcome to the Slack API Demo" puts "Here are all the channels" begin channels = SlackChannel.all rescue SlackException => exception puts "Got an error: #{exception}" exit end channels.each do |channel| puts " #{channel.name}" end puts "What channel do you want?" channel_name = gets.chomp channel = channels.find do |channel| channel.name == channel_name end if channel puts "What do you want to send?" message = gets.chomp channel.send(message) else puts "No such channel!" end