Skip to content

Instantly share code, notes, and snippets.

@vadv
Created February 8, 2018 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadv/b64c8fca951fd9c85787d1ac41b79856 to your computer and use it in GitHub Desktop.
Save vadv/b64c8fca951fd9c85787d1ac41b79856 to your computer and use it in GitHub Desktop.
require "chef/handler"
require "etc"
require "date"
class TelegramHandler < Chef::Handler
BOT_API_KEY = 'BOTAPI:KEY'
CHAT_ID = -id
PROXY_ADDR = 'http://chef/telegram/'
def send_report
return if Chef::Config[:why_run]
return if node[:fqdn] =~ /kitchen/
# success? ? run_status.updated_resources : run_status.formatted_exception
# не отсылаем не информативные сообщения
return if empty_message
uri = URI("#{PROXY_ADDR}/bot#{BOT_API_KEY}/sendMessage")
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = {'chat_id' => CHAT_ID, 'text' => text}.to_json
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(req)
puts(response.body) if response.code.to_i > 300
end
def report
begin
send_report
rescue Exception => e
puts("Send telegram message error: #{e}\n#{e.backtrace.join("\n")}")
end
end
end
handler = TelegramHandler.new()
Chef::Config.send("report_handlers") << handler
Chef::Config.send("exception_handlers") << handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment