Skip to content

Instantly share code, notes, and snippets.

@kossoff
Created November 8, 2016 09:55
Show Gist options
  • Save kossoff/b7e8df2937a00d456a5c4938442c7ae7 to your computer and use it in GitHub Desktop.
Save kossoff/b7e8df2937a00d456a5c4938442c7ae7 to your computer and use it in GitHub Desktop.
Service for watch Dropbox folder on linux & send notifications to Slack if new files added
#!/home/RUBYUSER/.rvm/rubies/ruby-2.3.0/bin/ruby
require 'rubygems'
require 'rb-inotify'
require 'slack-notifier'
watcher = INotify::Notifier.new
notifier = Slack::Notifier.new "https://hooks.slack.com/services/SLACKWEBHOOKURL"
notifier.channel = '#CHANNEL_NAME'
watcher.watch("/home/RUBYUSER/Dropbox/", :create, :delete, :recursive) do |event|
event.flags.each do |flag|
flag = flag.to_s
case flag
when 'create'
if File.extname(event.name) == '.JPG' || File.extname(event.name) == '.JPEG' || File.extname(event.name) == '.jpg' || File.extname(event.name) == '.jpeg'
message = "New file http://SERVERNAME/#{event.absolute_name.sub( '/home/RUBYUSER/Dropbox/', '')}"
notifier.ping message
puts message
end
when 'delete'
puts "Deleted file #{event.absolute_name}"
else
end
end
end
## loop, wait for events from inotify
watcher.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment