Last active
August 29, 2015 13:57
-
-
Save wata/9612659 to your computer and use it in GitHub Desktop.
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 characters
| module Fluent | |
| class ChatworkOutput < BufferedOutput | |
| Fluent::Plugin.register_output('chatwork', self) | |
| config_param :api_token, :string | |
| config_param :room, :string, :default => nil | |
| config_param :proxy, :string, :default => nil | |
| config_param :title, :string, :default => nil | |
| config_param :flush_interval, :time, :default => 1 | |
| def initialize | |
| super | |
| require 'faraday' | |
| end | |
| def configure(conf) | |
| super | |
| @api_token = conf['api_token'] | |
| @room = conf['room'] | |
| @title = conf['title'] | |
| @conn = Faraday.new( | |
| :url => 'https://api.chatwork.com', | |
| :proxy => conf['proxy'] || nil | |
| ) do |builder| | |
| builder.use Faraday::Request::UrlEncoded | |
| builder.use Faraday::Response::Logger | |
| builder.use Faraday::Adapter::NetHttp | |
| end | |
| end | |
| def format(tag, time, record) | |
| [tag, time, record].to_msgpack | |
| end | |
| def write(chunk) | |
| chunk.msgpack_each do |(tag,time,record)| | |
| begin | |
| send_message(record) | |
| rescue => e | |
| $log.error("ChatWork Error:", :error_class => e.class, :error => e.message) | |
| end | |
| end | |
| end | |
| def send_message(record) | |
| room = @room | |
| header = "[info][title]" + `hostname`.chop + " " + @title + "[/title]" | |
| message = record['message'] | |
| footer = "[/info]" | |
| response = @conn.post do |request| | |
| request.url "/v1/rooms/#{room}/messages" | |
| request.headers = { | |
| 'X-ChatWorkToken' => @api_token | |
| } | |
| request.params[:body] = header + message + footer | |
| end | |
| # todo: error handle | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment