Merge branch 'master' of https://github.com/cantino/huginn into twitter_search_agent

This commit is contained in:
Brian Petro 2015-10-13 20:43:50 -04:00
commit baf14f1c41
2 changed files with 29 additions and 7 deletions

View file

@ -1,6 +1,7 @@
module Agents
class SlackAgent < Agent
DEFAULT_USERNAME = 'Huginn'
ALLOWED_PARAMS = ['channel', 'username', 'unfurl_links', 'attachments']
cannot_be_scheduled!
cannot_create_events!
@ -13,7 +14,7 @@ module Agents
#{'## Include `slack-notifier` in your Gemfile to use this Agent!' if dependencies_missing?}
To get started, you will first need to configure an incoming webhook.
- Go to `https://my.slack.com/services/new/incoming-webhook`, choose a default channel and add the integration.
Your webhook URL will look like: `https://hooks.slack.com/services/some/random/characters`
@ -65,14 +66,22 @@ module Agents
@slack_notifier ||= Slack::Notifier.new(webhook_url, username: username)
end
def filter_options(opts)
opts.select { |key, value| ALLOWED_PARAMS.include? key }.symbolize_keys
end
def receive(incoming_events)
incoming_events.each do |event|
opts = interpolated(event)
if /^:/.match(opts[:icon])
slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_emoji: opts[:icon], unfurl_links: opts[:unfurl_links]
else
slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_url: opts[:icon], unfurl_links: opts[:unfurl_links]
slack_opts = filter_options(opts)
if opts[:icon].present?
if /^:/.match(opts[:icon])
slack_opts[:icon_emoji] = opts[:icon]
else
slack_opts[:icon_url] = opts[:icon]
end
end
slack_notifier.ping opts[:message], slack_opts
end
end
end

View file

@ -2,11 +2,14 @@ require 'spec_helper'
describe Agents::SlackAgent do
before(:each) do
@fallback = "Its going to rain"
@attachments = [{'fallback' => "{{fallback}}"}]
@valid_params = {
'webhook_url' => 'https://hooks.slack.com/services/random1/random2/token',
'channel' => '#random',
'username' => "{{username}}",
'message' => "{{message}}"
'message' => "{{message}}",
'attachments' => @attachments
}
@checker = Agents::SlackAgent.new(:name => "slacker", :options => @valid_params)
@ -15,7 +18,7 @@ describe Agents::SlackAgent do
@event = Event.new
@event.agent = agents(:bob_weather_agent)
@event.payload = { :channel => '#random', :message => 'Looks like its going to rain', username: "Huggin user"}
@event.payload = { :channel => '#random', :message => 'Looks like its going to rain', username: "Huggin user", fallback: @fallback}
@event.save!
end
@ -44,12 +47,22 @@ describe Agents::SlackAgent do
@checker.options['icon_emoji'] = "something"
expect(@checker).to be_valid
end
it "should allow attachments" do
@checker.options['attachments'] = nil
expect(@checker).to be_valid
@checker.options['attachments'] = []
expect(@checker).to be_valid
@checker.options['attachments'] = @attachments
expect(@checker).to be_valid
end
end
describe "#receive" do
it "receive an event without errors" do
any_instance_of(Slack::Notifier) do |obj|
mock(obj).ping(@event.payload[:message],
attachments: [{'fallback' => @fallback}],
channel: @event.payload[:channel],
username: @event.payload[:username]
)