huginn_slackbot_agent 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 97e9d37fd5e6946b661d620cab4ec5c1c344974a
4
+ data.tar.gz: 03ddc4cfcf4bf29ef0997ee1d093de6f4982f9d3
5
+ SHA512:
6
+ metadata.gz: b2a7a8cc8e18de2925a8ea26626b1744d5e39a383607f1142176e0f7a3517831c5949dd8b90619e1f7b631e0a5b3e7b03a24e5cbcbb41eabf13987a662112f29
7
+ data.tar.gz: 7a2f99ad23f064905d9a8c4b76dd040942ef63ba37f2ebfb99655215f036b6b8c13b1a45379df8e028f345a59b6f9d95365677a480bd00cddd83a158fe22cda5
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_slackbot_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_slackbot_agent/slackbot_agent'
@@ -0,0 +1,65 @@
1
+ require 'slack'
2
+
3
+ module Agents
4
+ class SlackbotAgent < Agent
5
+ gem_dependency_check { defined?(Slack) }
6
+
7
+ cannot_be_scheduled!
8
+
9
+ description <<-MD
10
+ This agent instanciate a slack bot and send messages.
11
+
12
+ Generate a slack bot token [here](https://my.slack.com/services/new/bot), and put into a credential called `slack_bot_token`
13
+
14
+ To send messages, just fill the options as a documented [here](https://api.slack.com/methods/chat.postMessage)
15
+
16
+ The bot needs to be in channel to send messages in it.
17
+ MD
18
+
19
+ def default_options
20
+ {
21
+ 'channel' => '#general',
22
+ 'text' => 'It works',
23
+ 'as_user' => true,
24
+ 'attachments' => []
25
+ }
26
+ end
27
+
28
+ def validate_options
29
+ errors.add(:base, 'you need to specify the channel or user name') unless options['channel'].present?
30
+ errors.add(:base, 'you need to specify the text or attachments list') unless options['text'].present? || options['attachments'].any?
31
+ end
32
+
33
+ def working?
34
+ received_event_without_error?
35
+ end
36
+
37
+ def receive(incoming_events)
38
+ incoming_events.each do |event|
39
+ interpolate_with(event) do
40
+ options = {
41
+ channel: interpolated['channel'],
42
+ as_user: interpolated['as_user']
43
+ }
44
+
45
+ options[:text] = interpolated['text'] if interpolated['text'].present?
46
+ options[:attachments] = interpolated['attachments'] if interpolated['attachments'].present? && interpolated['attachments'].any?
47
+
48
+ client.chat_postMessage(options)
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def client
56
+ return @client if @client.present?
57
+
58
+ Slack.configure do |config|
59
+ config.token = credential('slack_bot_token')
60
+ end
61
+
62
+ @client = Slack::Web::Client.new
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,66 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::SlackbotAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::SlackbotAgent.new.default_options
7
+ @checker = Agents::SlackbotAgent.new(:name => "SlackbotAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+
11
+ @event = Event.new
12
+ @event.agent = agents(:bob_weather_agent)
13
+ @event.payload = {
14
+ 'somekey' => 'Somevalue',
15
+ 'some_date' => 'May 23'
16
+ }
17
+
18
+ @expected_token = 'abc123'
19
+ @credential = UserCredential.create(
20
+ credential_name: 'slack_bot_token',
21
+ user: users(:bob),
22
+ credential_value: @expected_token,
23
+ mode: 'text'
24
+ )
25
+
26
+ @sent_requests = Array.new
27
+
28
+ stub_request(:post, 'https://slack.com/api/chat.postMessage').to_return do |request|
29
+ response_body = {
30
+ ok: true,
31
+ channel: 'G694SPM35',
32
+ ts: '1507236820.000421',
33
+ message: {
34
+ type: 'message',
35
+ user: 'U0CEL68F3',
36
+ text: 'teste',
37
+ bot_id: 'B349SNYN9',
38
+ ts: '1507236820.000421'
39
+ }
40
+ }
41
+
42
+ @sent_requests << request
43
+
44
+ { status: 200, body: ActiveSupport::JSON.encode(response_body), headers: { "Content-type" => "application/json" } }
45
+ end
46
+ end
47
+
48
+ describe '#working?' do
49
+ it 'checks if events have error' do
50
+ @checker.error 'something got wrong'
51
+ expect(@checker.reload).not_to be_working # There is a recent error
52
+ end
53
+ end
54
+
55
+ describe '#receive' do
56
+ it 'call slack api when receive an event' do
57
+ @checker.receive([@event])
58
+ expect(@sent_requests.length).to eq(1)
59
+ end
60
+
61
+ it 'should include configured token into request' do
62
+ @checker.receive([@event])
63
+ expect(@sent_requests.first.body.include? "token=#{@expected_token}").to be_truthy
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_slackbot_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Renato Menegasso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: slack-ruby-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - renato.menegasso@bidu.com.br
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/huginn_slackbot_agent.rb
77
+ - lib/huginn_slackbot_agent/slackbot_agent.rb
78
+ - spec/slackbot_agent_spec.rb
79
+ homepage: https://github.com/Bidu/huginn_slackbot_agent
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Huginn agent to send messages into slack as a bot.
102
+ test_files:
103
+ - spec/slackbot_agent_spec.rb