Jump to content
Colin

Slack Notifier

Recommended Posts

 

 

Register at Slack.com Create a channel for your Team Configure a Custom Incoming WebHooks Integration Install the app on your phone (Optional, but it will receive push notifications) ##Replace Your Unique Webhook URL accordingly (In @@uri)! root@mikrodash:/opt/dashing/rmm_hud# cat jobs/SlackNotifier.rb

 require 'net/http' require 'json' require 'uri' class SlackNotifier @@uri = URI.parse("PASTE_URI_HERE") @@header = {'Content-Type' => "text/json"} def notifySlack(notification) # Create the HTTP objects @http = Net::HTTP.new(@@uri.host, @@uri.port) @http.use_ssl = true @request = Net::HTTP::Post.new(@@uri.request_uri, @@header) @request.body = notification.to_json # Send the request @http.request(@request) end end 

##Basic example of Use root@mikrodash:/opt/dashing/rmm_hud# cat jobs/offlineservercount.rb

 require_relative 'my_db.rb' require_relative 'SlackNotifier.rb' current_offlineservercount = 0 firstRun = true SCHEDULER.every '30s', :first_in => 0 do |job| db = MyDb.conn sn = SlackNotifier.new sql = "SUPER SQL!" notif = "New Offline Server!!\n" results = db.query(sql) if results.count > current_offlineservercount results.each do|row| name = row['name'] notif = notif + name + "\n" end tmpNotif = {'text' => notif} if !firstRun then sn.notifySlack(tmpNotif) end end current_offlineservercount = results.count firstRun = false db.close send_event('offlineservercount', { value: current_offlineservercount } ) end

Yay!!

 

 

Share this post


Link to post
Share on other sites

Colin,

 

I know this is about a year old but we started using Slack in January and I would like some integration with Labtech. Where do I need to run the JSON script at? On the LT server itself?

 

Thanks,

 

Jeffery

Share this post


Link to post
Share on other sites

It could be even easier.

i've setup a script that executes a powershell command that will post to a specific channel in slack.

 

first you need to replace all backslashes in your @message@ with double backslashes. otherwise the POST will fail. Store that in @myresult@.

Then LT EXecute c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe with the following arguments.

 

-command "$url = '; $body = 'payload={\"channel\": \"#labtech\", \"username\": \"LT\", \"text\": \"@myresult@\", \"icon_emoji\": \":ghost:\"}'; Invoke-RestMethod -Method Post -Uri $url -Body $body;"

 

You need to ofcourse first create & configure an incoming webhook API URL (with JSON payload). That URL needs to be in the arguments ().

 

You can test it out on the powershell command line and when you're satisfied script it. make sure you use the right quotations. labtech is kinda picky about what goes where.

 

Wouter

Share this post


Link to post
Share on other sites

What a great way to do it. Thanks!

It could be even easier.

i've setup a script that executes a powershell command that will post to a specific channel in slack.

 

first you need to replace all backslashes in your @message@ with double backslashes. otherwise the POST will fail. Store that in @myresult@.

Then LT EXecute c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe with the following arguments.

 

-command "$url = '; $body = 'payload={\"channel\": \"#labtech\", \"username\": \"LT\", \"text\": \"@myresult@\", \"icon_emoji\": \":ghost:\"}'; Invoke-RestMethod -Method Post -Uri $url -Body $body;"

 

You need to ofcourse first create & configure an incoming webhook API URL (with JSON payload). That URL needs to be in the arguments ().

 

You can test it out on the powershell command line and when you're satisfied script it. make sure you use the right quotations. labtech is kinda picky about what goes where.

 

Wouter

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...