Class: MCollective::Util::Playbook::Tasks::SlackTask
- Defined in:
- lib/mcollective/util/playbook/tasks/slack_task.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #attachments ⇒ Object
- #choria ⇒ Object
- #from_hash(data) ⇒ Object
- #run ⇒ Object
- #to_execution_result(results) ⇒ Object
- #validate_configuration! ⇒ Object
Methods inherited from Base
#initialize, #run_task, #startup_hook, #to_s
Methods included from MCollective::Util::Playbook::TemplateUtil
#__template_process_string, #__template_resolve, #t
Constructor Details
This class inherits a constructor from MCollective::Util::Playbook::Tasks::Base
Instance Method Details
#attachments ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mcollective/util/playbook/tasks/slack_task.rb', line 30 def [ "fallback" => @text, "color" => @color, "text" => @text, "pretext" => "Task: %s" % @description, "mrkdwn_in" => ["text"], "footer" => "Choria Playbooks", "fields" => [ { "title" => "user", "value" => PluginManager["security_plugin"].callerid, "short" => true }, { "title" => "playbook", "value" => @playbook.name, "short" => true } ] ] end |
#choria ⇒ Object
26 27 28 |
# File 'lib/mcollective/util/playbook/tasks/slack_task.rb', line 26 def choria @_choria ||= Util::Choria.new(false) end |
#from_hash(data) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mcollective/util/playbook/tasks/slack_task.rb', line 15 def from_hash(data) @channel = data["channel"] @text = data["text"] @token = data["token"] @color = data.fetch("color", "#ffa449") @username = data.fetch("username", "Choria") @icon = "https://choria.io/img/slack-48x48.png" self end |
#run ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mcollective/util/playbook/tasks/slack_task.rb', line 74 def run https = choria.https(:target => "slack.com", :port => 443) path = "/api/chat.postMessage?token=%s&username=%s&channel=%s&icon_url=%s&attachments=%s" % [ CGI.escape(@token), CGI.escape(@username), CGI.escape(@channel), CGI.escape(@icon), CGI.escape(.to_json) ] resp, data = https.request(choria.http_get(path)) data = JSON.parse(data || resp.body) if resp.code == "200" && data["ok"] Log.debug("Successfully sent message to slack channel %s" % [@channel]) [true, "Message submitted to slack channel %s" % [@channel], [data]] else Log.warn("Failed to send message to slack channel %s: %s" % [@channel, data["error"]]) [false, "Failed to send message to slack channel %s: %s" % [@channel, data["error"]], [data]] end rescue msg = "Could not publish slack message to channel %s: %s: %s" % [@channel, $!.class, $!.to_s] Log.debug(msg) Log.debug($!.backtrace.join("\t\n")) [false, msg, []] end |
#to_execution_result(results) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mcollective/util/playbook/tasks/slack_task.rb', line 53 def to_execution_result(results) result = { "value" => results[1], "type" => "slack", "fail_ok" => @fail_ok } unless results[0] result["error"] = { "msg" => results[1], "kind" => "choria.playbook/taskerror", "details" => { "channel" => @channel } } result["value"] = results[2].first end {"slack.com" => result} end |
#validate_configuration! ⇒ Object
9 10 11 12 13 |
# File 'lib/mcollective/util/playbook/tasks/slack_task.rb', line 9 def validate_configuration! raise("A channel is required") unless @channel raise("Message text is required") unless @text raise("A bot token is required") unless @token end |