botmetrics-rb 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +65 -14
- data/bin/console +1 -1
- data/botmetrics-rb.gemspec +3 -3
- data/lib/botmetrics-rb/version.rb +3 -0
- data/lib/botmetrics.rb +76 -28
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d52b84166c8dc99b99d90eca6b723ea10f562a5d
|
4
|
+
data.tar.gz: cd8db70b02fa23b83af2cec3659601c1de625eb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24bd20b4b6522fa31b662ffb298b17cb527b2f5fd5b9a1ff4095d15d1cdeafa10a2baf8596767cd13a7b76105457c4401e3fea7ab28c3ff9da6e6340ac5311e6
|
7
|
+
data.tar.gz: 359eb18be44c0d2d6a6357ffeb47a930d2b3267e31aa53d4c775966bda98fbd47d873e534d50550b637751238c27246c0b05058d0b8d1531bca80d442906e555
|
data/README.md
CHANGED
@@ -23,37 +23,89 @@ Or install it yourself as:
|
|
23
23
|
## Usage
|
24
24
|
|
25
25
|
Log in to your [BotMetrics](https://getbotmetrics.com) account, navigate
|
26
|
-
to "Bot Settings" and find out your
|
26
|
+
to "Bot Settings" and find out your Bot ID and API Key.
|
27
27
|
|
28
|
-
|
29
|
-
Key respectively.
|
28
|
+
With that, you can initialize a `BotMetrics::Client`:
|
30
29
|
|
30
|
+
```ruby
|
31
|
+
client = BotMetrics::Client.new(api_key: '123', bot_id: '123')
|
31
32
|
```
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
|
34
|
+
Alternatively, you can set the following ENV variables
|
35
|
+
|
36
|
+
- `ENV['BOTMETRICS_API_KEY']`
|
37
|
+
- `ENV['BOTMETRICS_BOT_ID']`
|
38
|
+
- `ENV['BOTMETRICS_API_HOST']`
|
39
|
+
|
40
|
+
and initialize a `BotMetrics::Client` with the default ENV variables:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
client = BotMetrics::Client.new
|
35
44
|
```
|
36
45
|
|
37
|
-
|
38
|
-
|
39
|
-
|
46
|
+
### `register_bot!`
|
47
|
+
|
48
|
+
With a `BotMetrics::Client` instance,
|
49
|
+
every time you create a new Slack Bot (in the OAuth2 callback),
|
50
|
+
and assuming the bot token you received as part of the OAuth2 callback is `bot-token`,
|
51
|
+
you can make the following call:
|
40
52
|
|
41
53
|
```ruby
|
42
|
-
BotMetrics.
|
54
|
+
client = BotMetrics::Client.new(api_key: '123', bot_id: '123')
|
55
|
+
client.register_bot!('bot-token')
|
43
56
|
```
|
44
57
|
|
45
|
-
|
58
|
+
#### Retroactive Registration
|
46
59
|
|
47
60
|
If you created your bot in the past, you can pass in `created_at` with
|
48
61
|
the UNIX timestamp of when your bot was created, like so:
|
49
62
|
|
50
63
|
```ruby
|
51
|
-
BotMetrics.
|
64
|
+
client = BotMetrics::Client.new(api_key: '123', bot_id: '123')
|
65
|
+
client.register_bot!('bot-token', created_at: 1462318092)
|
66
|
+
```
|
67
|
+
|
68
|
+
### `message`
|
69
|
+
|
70
|
+
You can send messages to a Slack channel or user using the `#message` method:
|
71
|
+
|
72
|
+
#### Sending a Message to a Channel
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
client = BotMetrics::Client.new(api_key: '123', bot_id: '123')
|
76
|
+
client.message(team_id: 'T123', channel: 'C123', text: 'Hello!')
|
77
|
+
```
|
78
|
+
|
79
|
+
#### Sending an Attachment to a User
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
client = BotMetrics::Client.new(api_key: '123', bot_id: '123')
|
83
|
+
client.message(
|
84
|
+
team_id: 'T123',
|
85
|
+
user: 'U123',
|
86
|
+
attachments:
|
87
|
+
[
|
88
|
+
{
|
89
|
+
"title": "Title",
|
90
|
+
"pretext": "Pretext _supports_ mrkdwn",
|
91
|
+
"text": "Testing *right now!*",
|
92
|
+
"mrkdwn_in": ["text", "pretext"]
|
93
|
+
}
|
94
|
+
]
|
95
|
+
)
|
52
96
|
```
|
53
97
|
|
98
|
+
Accepted parameters include:
|
99
|
+
|
100
|
+
- `team_id`: This is the Slack ID
|
101
|
+
- `channel`: This is the Slack channel's UID
|
102
|
+
- `user`: This is the Slack user's UID
|
103
|
+
- `text`: A string
|
104
|
+
- `attachments`: Attachments that follows the Slack's [message attachment structure](https://api.slack.com/docs/attachments)
|
105
|
+
|
54
106
|
## Development
|
55
107
|
|
56
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
108
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment with the gem.
|
57
109
|
|
58
110
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
111
|
|
@@ -65,4 +117,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
65
117
|
## License
|
66
118
|
|
67
119
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
68
|
-
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
|
-
|
4
|
+
require_relative "../lib/botmetrics"
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
data/botmetrics-rb.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'botmetrics'
|
4
|
+
require 'botmetrics-rb/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "botmetrics-rb"
|
@@ -27,8 +27,8 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = []
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_dependency "json",
|
31
|
-
spec.add_dependency "
|
30
|
+
spec.add_dependency "json", "~> 1.8.3"
|
31
|
+
spec.add_dependency "http", "~> 2.0.0"
|
32
32
|
|
33
33
|
spec.add_development_dependency "bundler", "~> 1.11"
|
34
34
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/botmetrics.rb
CHANGED
@@ -1,42 +1,90 @@
|
|
1
1
|
require "json"
|
2
|
-
require "
|
2
|
+
require "http"
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
module BotMetrics
|
5
|
+
class Client
|
6
|
+
DEFAULT_API_HOST = "https://www.getbotmetrics.com".freeze
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def initialize(api_key: nil, bot_id: nil, api_host: nil)
|
9
|
+
@api_key = api_key || ENV['BOTMETRICS_API_KEY']
|
10
|
+
@bot_id = bot_id || ENV['BOTMETRICS_BOT_ID']
|
11
|
+
@api_host = api_host || ENV['BOTMETRICS_API_HOST'] || DEFAULT_API_HOST
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
if blank?(@api_key)
|
14
|
+
raise ArgumentError.new("Missing argument api_key. Please pass api_key in as an argument.")
|
15
|
+
end
|
16
|
+
|
17
|
+
if blank?(@bot_id)
|
18
|
+
raise ArgumentError.new("Missing argument bot_id. Please pass bot_id in as an argument.")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def register_bot!(token, opts = {})
|
23
|
+
params = { "format" => "json", "instance[token]" => token }
|
24
|
+
|
25
|
+
created_at = opts[:created_at] || opts["created_at"]
|
26
|
+
params["instance[created_at]"] = created_at.to_i if created_at.to_i != 0
|
27
|
+
|
28
|
+
response = HTTP.auth(api_key).post("#{api_url}/instances", params: params)
|
29
|
+
|
30
|
+
response.code == 201
|
13
31
|
end
|
14
|
-
|
15
|
-
|
32
|
+
|
33
|
+
def message(team_id:, channel: nil, user: nil, text: nil, attachments: nil)
|
34
|
+
if blank?(channel) && blank?(user)
|
35
|
+
raise ArgumentError.new("Missing argument channel and user. Please provide at least one.")
|
36
|
+
end
|
37
|
+
|
38
|
+
if blank?(text) && blank?(attachments)
|
39
|
+
raise ArgumentError.new("Missing argument text and attachments. Please provide at least one.")
|
40
|
+
end
|
41
|
+
|
42
|
+
params = {
|
43
|
+
"message[team_id]" => team_id,
|
44
|
+
"message[channel]" => channel,
|
45
|
+
"message[user]" => user,
|
46
|
+
"message[text]" => text,
|
47
|
+
"message[attachments]" => message_attachments(attachments)
|
48
|
+
}.delete_if { |_, v| v.nil? }
|
49
|
+
|
50
|
+
response = HTTP.auth(api_key).post("#{api_url}/messages", params: params)
|
51
|
+
|
52
|
+
response.code == 202
|
16
53
|
end
|
17
54
|
|
18
|
-
|
19
|
-
host = ENV['BOTMETRICS_API_HOST'] || 'https://www.getbotmetrics.com'
|
55
|
+
private
|
20
56
|
|
21
|
-
|
22
|
-
omit_default_port: true,
|
23
|
-
idempotent: true,
|
24
|
-
retry_limit: 6,
|
25
|
-
read_timeout: 360,
|
26
|
-
connect_timeout: 360
|
27
|
-
}
|
57
|
+
attr_accessor :api_key, :bot_id, :api_host
|
28
58
|
|
29
|
-
|
30
|
-
|
31
|
-
|
59
|
+
def blank?(attr)
|
60
|
+
attr.nil? || attr == ''
|
61
|
+
end
|
32
62
|
|
33
|
-
|
34
|
-
|
35
|
-
|
63
|
+
def api_url
|
64
|
+
"#{api_host}/bots/#{bot_id}"
|
65
|
+
end
|
36
66
|
|
37
|
-
|
38
|
-
|
67
|
+
def options(extra_params)
|
68
|
+
{
|
69
|
+
headers: { "Authorization" => api_key },
|
70
|
+
omit_default_port: true,
|
71
|
+
idempotent: true,
|
72
|
+
retry_limit: 6,
|
73
|
+
read_timeout: 360,
|
74
|
+
connect_timeout: 360
|
75
|
+
}.merge(extra_params)
|
76
|
+
end
|
39
77
|
|
40
|
-
|
78
|
+
def message_attachments(attachments)
|
79
|
+
if attachments.nil?
|
80
|
+
nil
|
81
|
+
else
|
82
|
+
if attachments.is_a? String
|
83
|
+
attachments
|
84
|
+
else
|
85
|
+
attachments.to_json
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
41
89
|
end
|
42
90
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: botmetrics-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- arunthampi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -25,19 +25,19 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.8.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: http
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 2.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 2.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- bin/console
|
114
114
|
- bin/setup
|
115
115
|
- botmetrics-rb.gemspec
|
116
|
+
- lib/botmetrics-rb/version.rb
|
116
117
|
- lib/botmetrics.rb
|
117
118
|
homepage: https://www.getbotmetrics.com
|
118
119
|
licenses:
|