Skip to content

Commit

Permalink
Add telegram bot hook extension
Browse files Browse the repository at this point in the history
  • Loading branch information
2called-chaos committed Apr 27, 2020
1 parent 7ca739f commit ea615c4
Show file tree
Hide file tree
Showing 5 changed files with 638 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in dle.gemspec
gemspec

# optional dependencies
gem 'telegram-bot-ruby'
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ Watchmonkey is a very simple tool to monitor resources with Ruby without the nee

Before looking any further you might want to know:

* There is no escalation or notification system but you may add it yourself
* I created this for being used with [Platypus](http://sveinbjorn.org/platypus) hence the [Platypus Hook](https://github.com/2called-chaos/watchmonkey_cli/blob/master/lib/watchmonkey_cli/hooks/platypus.rb)
* There is no escalation or notification system (except experimental telegram bot) but you may add it yourself
* I originally created this for being used with [Platypus](http://sveinbjorn.org/platypus) hence the [Platypus Hook](https://github.com/2called-chaos/watchmonkey_cli/blob/master/lib/watchmonkey_cli/hooks/platypus.rb)
* This is how the text output looks like: [Screenshot](http://imgur.com/8yLYnKb)
* This is how the Platypus support looks like: [ProgressBar](http://imgur.com/Vd8ZD7A) [HTML/WebView](http://imgur.com/5FwmWFZ)
* This is how Telegram Bot looks for now: [Telegram Screenshot](http://imgur.com/HBONi51)

---

Expand Down Expand Up @@ -83,6 +84,15 @@ If you want to monitor something that is not covered by the buildin handlers you
By default Watchmonkey will run all tests once and then exit. This addon will enable Watchmonkey to run in a loop and run tests on a periodic interval.
Since this seems like a core feature it might get included directly into Watchmonkey but for now take a look at the [application configuration file](https://github.com/2called-chaos/watchmonkey_cli/blob/master/doc/config_example.rb) and [ReQueue source code](https://github.com/2called-chaos/watchmonkey_cli/blob/master/lib/watchmonkey_cli/hooks/requeue.rb) for integration examples.

### Telegram Bot
Notify via Telegram. Experimental. Refer to [application configuration file](https://github.com/2called-chaos/watchmonkey_cli/blob/master/doc/config_example.rb) and [TelegramBot source code](https://github.com/2called-chaos/watchmonkey_cli/blob/master/lib/watchmonkey_cli/hooks/telegram_bot.rb) for further information.

* works with ReQueue (wouldn't make much sense otherwise huh?)
* optional per-user message throttling via checker uniqid (checker name + host + arguments)
* optional per-user only/except filters based on tags
* planned: robust telegram connection failure handling
* planned: per-user regex exclusion filters

### Platypus support
[Platypus](http://sveinbjorn.org/platypus) is a MacOS software to create dead simple GUI wrappers for scripts. There is buildin support for the interface types ProgressBar and WebView. For integration examples take a look at the [application configuration file](https://github.com/2called-chaos/watchmonkey_cli/blob/master/doc/config_example.rb) and [Platypus hook source code](https://github.com/2called-chaos/watchmonkey_cli/blob/master/lib/watchmonkey_cli/hooks/platypus.rb).

Expand Down
56 changes: 56 additions & 0 deletions doc/config_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,59 @@

@opts[:colorize] = false # doesn't render in platypus
end



# Integrate Telegram notifications
# For options refer to the source code:
# https://github.com/2called-chaos/watchmonkey_cli/blob/master/lib/watchmonkey_cli/hooks/telegram_bot.rb
if @argv.delete("--telegram")
require "watchmonkey_cli/hooks/telegram_bot"
WatchmonkeyCli::TelegramBot.hook!(self, {
# to create a bot refer to https://core.telegram.org/bots#6-botfather
api_key: "123456789:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

# poll timeout, the longer this is (in seconds) the longer it will take to gracefully shut down
timeout: 5,

# optionally log incoming messages
#logger: Logger.new(STDOUT),

# purge old throttle data, default: 30.days
#throttle_retention: 30.days,

# retry sending messages that failed, default: false
# Not recommended since on connection failure a HUGE amount of messages will accumulate
# and spam you (and reach rate limits) upon connection restore.
#retry_on_egress_failure: false,

# configure your notification targets, if not listed you can't interact with the bot
notify: [
[
# your telegram ID, if you try talking to the bot it will tell you your ID
987654321,

# flags
# - :all -- same as :debug, :info, :error (not recommended)
# - :debug -- send all debug messages (not recommended)
# - :info -- send all info messages (not recommended)
# - :error -- send all error messages (RECOMMENDED)
# - :admin_flag -- allows access to some commands (/wm_shutdown /stats)
[:error, :admin_flag],

# options (all optional, you can comment them out but leave the {})
{
# throttle: seconds(int) -- throttle messages by checker uniqid for this long (0/false = no throttle, default)
throttle: 15*60,

# only: Array(string, symbol) -- only notify when tagged with given tags
only: %w[production critical],

# except: Array(string, symbol) -- don't notify when tagged with given tags (runs after only-check)
except: %w[database],
}
],
[123456789, [:error], { throttle: 30.minutes }]
],
})
end
1 change: 1 addition & 0 deletions lib/watchmonkey_cli/hooks/requeue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def self.hook!(app)
# puts " #{@threads.select{|t| t.status == "run" }.length} running"
# puts " #{@threads.select{|t| t.status == "sleep" }.length} sleeping"
puts " Processed: #{@processed}"
puts " Promises: #{@telegram_bot_egress_promises.length}" if @telegram_bot_egress_promises
puts "========== //STATUS =========="
end
end
Expand Down
Loading

0 comments on commit ea615c4

Please sign in to comment.