Skip to content

Instantly share code, notes, and snippets.

@nifuramu
Created May 16, 2014 01:53
Show Gist options
  • Save nifuramu/d949445da35ab1d77794 to your computer and use it in GitHub Desktop.
Save nifuramu/d949445da35ab1d77794 to your computer and use it in GitHub Desktop.
require 'json'
require 'uri'
require 'net/https'
class Slack
def initialize(settings)
@host = settings["host"]
@endpoint = settings["endpoint"]
@token = settings["token"]
end
def send_message(message, username="kussan-bot", icon_emoji=":kussan:", channel="#develop")
data = {
type: "message",
subtype: "bot_message",
text: message,
username: username,
icon_emoji: icon_emoji,
channel: channel,
}
https = Net::HTTP.new(@host, 443)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
https.start do |https|
https.post(sprintf("%s%s", @endpoint, @token), "payload=#{data.to_json}")
end
end
end
# yaml sample
# slack:
# token: "TOKEN"
# host: "YOUR.slack.com"
# endpoint: "/services/hooks/incoming-webhook?token="
settings = YAML.load_file('config/settings.yml')
slack = Slack.new(settings["slack"])
slack.send_message('チンチンチンチンチンチンチンチンチンチンチンチンチンチンチンチン', 'おじさんbot', ':ojisan:', '#general')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment