class Alerty::Plugin::Typetalk

Typetalk sends notifcation to Typetalk

Constants

VERSION

Public Class Methods

new(config) click to toggle source
# File lib/alerty/plugin/typetalk.rb, line 9
def initialize(config)
  @token = config.token || ENV['TYPETALK_TOKEN']
  @topic_id = config.topic_id || ENV['TYPETALK_TOPIC_ID']
  @template = config.template || ''
end

Public Instance Methods

alert(record) click to toggle source
# File lib/alerty/plugin/typetalk.rb, line 15
def alert(record)
  msg = parse_template(@template, record)
  post_message(msg)
end

Private Instance Methods

parse_template(template, record) click to toggle source
# File lib/alerty/plugin/typetalk.rb, line 38
def parse_template(template, record)
  template.dup.tap do |t|
    t.gsub!('${hostname}', record[:hostname])
    t.gsub!('${command}', record[:command])
    t.gsub!('${exitstatus}', record[:exitstatus].to_s)
    t.gsub!('${output}', record[:output])
  end
end
post(uri, body) click to toggle source
# File lib/alerty/plugin/typetalk.rb, line 28
def post(uri, body)
  req = Net::HTTP::Post.new(uri)
  req['X-Typetalk-Token'] = @token
  req['Content-Type'] = 'application/json'
  req.body = body
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    res = http.request(req)
  end
end
post_message(msg) click to toggle source
# File lib/alerty/plugin/typetalk.rb, line 22
def post_message(msg)
  uri = URI("https://typetalk.com/api/v1/topics/#{@topic_id}")
  body = { message: msg }.to_json
  post(uri, body)
end