boty 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cc2c0485b34d41c484c6226afcd775177ae9ff4
4
- data.tar.gz: 41dc976a9443dc170427775a5db302f67c64a4fd
3
+ metadata.gz: 6c95fa76730f4a41c13d06ff314d53c0a9ee1f1c
4
+ data.tar.gz: 22567a320540d0fb327cdf9a0c863a10da787b82
5
5
  SHA512:
6
- metadata.gz: e36b9176d3ddd5f8cffa05457637d2a98c1897f38f5d485f960b2306b2ab88be1606f6069bfabc9c8540f596a30249b8891c0f79fce80ea430f8b6ad7a00ddd0
7
- data.tar.gz: b8f8b998064f4aabab2ca72abb91be20ee513c4f841eec7201d90d1e240106f2ff837af1d9faee3691366589d485cf21e8399daaa205a9cafc69651843ec1c88
6
+ metadata.gz: a44d246026394e6d097e83d8405d1fdfcd73bed586790dd807fa975da6a330b548b8b85a3840b2d27c0da2b166e673d623b27524db02ee89504c4ec4d7fdfd2a
7
+ data.tar.gz: e82866d26ad78cc4de3cd4f875d4a52fa59208f745823a0172142a3e8e4cb4e63931c67dd33cd4d340495d349d518c145e1f513f6e86d694d08561a18699696f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boty (0.0.4)
4
+ boty (0.0.6)
5
5
  eventmachine
6
6
  faye-websocket
7
7
  thor
data/README.md CHANGED
@@ -1,28 +1,141 @@
1
1
  # Boty
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/boty`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ `Boty` is a utilitary to create bots (at this time, specificaly Slack bots).
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ A bot is this context is a ordinary ruby application, that knows how to receive
6
+ messages. Boty will give you a nice way to bind your own logic to specific
7
+ messages of your own interest.
6
8
 
7
- ## Installation
9
+ ## Slack Bot Integration<a id="integration" />
8
10
 
9
- Add this line to your application's Gemfile:
11
+ The first thing to do is to create a Slack Bot integration.
12
+ Go to `http://[your-company-name].slack.com/services` to add a new one.
13
+ In the process an `API Token` will be generated. That will be used in the [next
14
+ step](#installation).
10
15
 
11
- ```ruby
12
- gem 'boty'
16
+ ## Installation<a id="installation" />
17
+
18
+ Now you can install `Boty`:
19
+
20
+ ```sh
21
+ $ gem install boty
22
+ ```
23
+
24
+ This will give you an executable `boty`, which ships with a command `help` so
25
+ you can know all the stuff that it can do. But the main one is to [create your
26
+ new shiny bot](#usage).
27
+
28
+ ## Usage<a id="usage" />
29
+
30
+ In this section you will learn how to create a custom bot application for your
31
+ needs, how to configure it to allow the bot say stuff in your slack channels and
32
+ finally, will see how to run the bot properly.
33
+
34
+ ### Creating a new bot
35
+
36
+ To create a new bot, execute:
37
+
38
+ ```sh
39
+ $ boty new jeeba
40
+ ```
41
+
42
+ Where `jeeba` will be the nickname you give your bot in the [Slack
43
+ integration](#integration).
44
+
45
+ The command will create a new directory after your bot name (`jeeba` in my
46
+ example). Your _bot application_ will live in this directory. Feel free to check
47
+ the directory, it will have some ruby files, some directories... it's just an
48
+ ordinary ruby application.
49
+
50
+ But first, let's see something about the [configurations](#configuration).
51
+
52
+ ### Configuration<a id="configuration" />
53
+
54
+ When executing the `boty new` command, you will be prompted for the
55
+ *company name* and the *bot api key*.
56
+
57
+ The information that you enter will be used only locally, so you can experiment
58
+ and test (we will see more about the automated ones in this *README*) while
59
+ developing your bot. This will be stored in the `.env.local` file on the
60
+ recently created dir.
61
+
62
+ If you want to understand how this configuration is managed locally, you can
63
+ check de [`Dotenv` gem documentation](https://github.com/bkeepers/dotenv), but
64
+ you don't need to worry about it.
65
+
66
+ ### Running locally<a id="running" />
67
+
68
+ After create the new _bot application_ and give the *bot api key*, you can just
69
+ enter the new dir and run
70
+
71
+ ```sh
72
+ $ ./bot
13
73
  ```
14
74
 
15
- And then execute:
75
+ Done! Your bot will be connected to your company Slack through the nickname that
76
+ you provided in the [integration](#integration) step.
77
+ To see if it's working properly, just go the the slack, in the general channel
78
+ (or any other where the bot was invited) and type:
16
79
 
17
- $ bundle
80
+ ```sh
81
+ @jeeba: ping
82
+ ```
83
+
84
+ It will respond to you: `pong`. IT'S ALIVE.
85
+
86
+ ### Deploy on heroku
87
+
88
+ But probably what you want is to have your bot running in a server, not in your
89
+ machine, right?
90
+
91
+ Your bot is created with an example `Procfile` to make it easy to run it on
92
+ [Heroku](http://heroku.com) (to give an example).
93
+
94
+ Create a new project on Heroku, following their instructions, until they ask you
95
+ to do the "deploy" (the `git push heroku master`).
96
+
97
+ #### configure the api key
98
+
99
+ Now you need to add the configurations that are localy stored on `.env.local`.
100
+ The two environment variables on that file are:
101
+
102
+ SLACK_COMPANY
103
+ SLACK_BOT_API_TOKEN
104
+
105
+ The *Heroku* command line tool offers a way to create environment vars in the
106
+ server, check
107
+ [their documentation](https://devcenter.heroku.com/articles/config-vars).
108
+ Today, when as I'm writing the readme, you can use the following commands to set
109
+ those two environment variables:
18
110
 
19
- Or install it yourself as:
111
+ $ heroku config:set SLACK_COMPANY=your-company-name
112
+ $ heroku config:set SLACK_BOT_API_TOKEN=your-bot-integration-api-token
20
113
 
21
- $ gem install boty
114
+ #### allow process to run
22
115
 
23
- ## Usage
116
+ Heroku will not detect a web application in your bot, so you need to tell them
117
+ to run your application as a, well... as a "normal" application.
118
+ Go to your heroku dashboard (https://dashboard.heroku.com/apps), find the
119
+ application that you have created to your bot.
120
+ On the tab _Resources_, find a line with the information:
24
121
 
25
- TODO: Write usage instructions here
122
+ your-bot-name bundle exec ./bot
123
+
124
+
125
+ Turn on this resource. And done, your bot is up and running!
126
+
127
+
128
+ ## Adding custom scripts
129
+
130
+ **todo: document**
131
+
132
+ For now, check the `script/ping.rb` and follow it's leads.
133
+
134
+ ### Testing your own scripts
135
+
136
+ **todo: document**`
137
+
138
+ For now, check the `spec/script/ping_spec.rb` and follow it's leads.
26
139
 
27
140
  ## Development
28
141
 
@@ -38,4 +151,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ricard
38
151
  ## License
39
152
 
40
153
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
data/bin/bot ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require "./lib/boty"
3
+
4
+ session = Boty::Session.new
5
+ session.verbose = true
6
+ session.start do |bot|
7
+ bot.verbose = true
8
+ bot.message(/are you there\?/i) do |message|
9
+ next if message.from? self
10
+ say "Ohay <@#{message.user}>! I'm here, that's for sure."
11
+ end
12
+ end
data/lib/boty.rb CHANGED
@@ -13,9 +13,10 @@ require "faye/websocket"
13
13
 
14
14
  $:.unshift File.expand_path("../../lib", __FILE__)
15
15
  require "boty/version"
16
+ require "boty/slack"
16
17
  require "boty/session"
17
- require "boty/bot"
18
18
  require "boty/message"
19
+ require "boty/bot"
19
20
 
20
21
  module Boty
21
22
  end
data/lib/boty/bot.rb CHANGED
@@ -43,12 +43,10 @@ module Boty
43
43
  end
44
44
  end
45
45
 
46
- def say(message)
46
+ def say(message, api_parameters = {})
47
47
  channel = (@_message && @_message.channel) || "general"
48
- uri = URI("https://slack.com/api/chat.postMessage?" +
49
- "token=#{ENV['SLACK_BOT_API_TOKEN']}&as_user=true&channel=#{channel}&" +
50
- "text=#{URI.encode message}")
51
- post_response = Net::HTTP.get uri
48
+ options = { channel: channel }.merge api_parameters
49
+ post_response = Slack.chat.post_message message, options
52
50
  debug nil, post_response
53
51
  end
54
52
 
data/lib/boty/cli.rb CHANGED
@@ -3,7 +3,7 @@ require "thor"
3
3
  module Boty
4
4
  class CLI < Thor
5
5
  include Thor::Actions
6
- class_option :verbose, type: :boolean
6
+ class_option :verbose, type: :boolean, default: true
7
7
 
8
8
  attr_reader :bot_name, :company, :api_key
9
9
 
@@ -19,6 +19,7 @@ module Boty
19
19
  # for some reason the example .rspec isn't been copied
20
20
  create_file ".rspec", "--require=spec_helper"
21
21
  run "chmod +x bot"
22
+ run "bundle install"
22
23
  end
23
24
  end
24
25
 
data/lib/boty/rspec.rb CHANGED
@@ -4,10 +4,8 @@ module Boty
4
4
  def self.included(base)
5
5
  base.instance_eval do
6
6
  let(:bot) {
7
- bot = Boty::Bot.new(
8
- {"id" => "1234", "name" => "bot"},
9
- Boty::Session.new(ENV["SLACK_COMPANY"] || "acme")
10
- )
7
+ bot = Boty::Bot.new({"id" => "1234", "name" => "bot"},
8
+ Boty::Session.new)
11
9
 
12
10
  class << bot
13
11
  attr_accessor :_response
@@ -22,12 +20,17 @@ module Boty
22
20
  end
23
21
  end
24
22
 
23
+ def event(options)
24
+ bot.event({ "type" => "message" }.merge options)
25
+ end
26
+
25
27
  def message_event(text)
26
- bot.event "type" => "message", "text" => text
28
+ event "text" => text
27
29
  end
28
30
  end
29
31
 
30
32
  ::RSpec::Matchers.define :have_responded do |expected|
33
+ # TODO: add proper messages for failures.
31
34
  match do |bot|
32
35
  response = bot._response
33
36
  bot._response = nil
data/lib/boty/session.rb CHANGED
@@ -3,17 +3,10 @@ require "net/http"
3
3
 
4
4
  module Boty
5
5
  class Session
6
- SLACK_RTM_START_URL = "https://%{domain}.slack.com/api/rtm.start?"+
7
- "token=%{token}&"+
8
- "simple_latest=true&no_unreads=true"
9
- private_constant :SLACK_RTM_START_URL
10
-
11
6
  attr_reader :bot
12
7
  attr_writer :verbose
13
8
 
14
- def initialize(slack_domain = ENV["SLACK_COMPANY"], verbose: false)
15
- @rtm_start_url = SLACK_RTM_START_URL.sub "%{domain}", slack_domain
16
- @rtm_start_url = @rtm_start_url.sub "%{token}", ENV["SLACK_BOT_API_TOKEN"]
9
+ def initialize(verbose: false)
17
10
  @verbose = verbose
18
11
  end
19
12
 
@@ -43,7 +36,7 @@ module Boty
43
36
 
44
37
  def login
45
38
  debug "logging in against slack right now"
46
- @slack_info = JSON.parse Net::HTTP.get(URI @rtm_start_url)
39
+ @slack_info = Slack.rtm.start
47
40
  debug "yep! logged in!"
48
41
  @session_url = @slack_info["url"]
49
42
  end
data/lib/boty/slack.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "boty/slack/url"
2
+ require "boty/slack/chat"
3
+ require "boty/slack/rtm"
4
+
5
+ module Boty
6
+ module Slack
7
+ class << self
8
+ def chat; @chat ||= Chat.new end
9
+ def rtm; @rtm ||= RTM.new end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Boty
2
+ module Slack
3
+ class Chat
4
+ include Slack::URL
5
+ url "https://slack.com/api/chat.postMessage"
6
+
7
+ def post_message(message, parameters = {})
8
+ defaults = {
9
+ as_user: true,
10
+ channel: "general",
11
+ text: message
12
+ }
13
+ URL.get parameterize(defaults.merge parameters)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module Boty
2
+ module Slack
3
+ class RTM
4
+ include Slack::URL
5
+ url "https://#{ENV["SLACK_COMPANY"]}.slack.com/api/"
6
+
7
+ def start(parameters = {})
8
+ defaults = { simple_latest: true, no_unreads: true }
9
+ url = parameterize defaults.merge(parameters), path: "rtm.start"
10
+ URL.get url
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module Boty
2
+ module Slack
3
+ module URL
4
+ def self.included(base)
5
+ base.instance_eval do
6
+ def url(url); @_url = url end
7
+ def _url; @_url end
8
+ end
9
+ end
10
+
11
+ def self.get(url)
12
+ JSON.parse Net::HTTP.get(URI(url)) || "{}"
13
+ end
14
+
15
+ private
16
+
17
+ def parameterize(user_parameters, path: nil)
18
+ url = path ? self.class._url + path : self.class._url
19
+ parameters = {token: ENV["SLACK_BOT_API_TOKEN"]}.merge user_parameters
20
+ parameters.reduce(url + "?") { |_url, param|
21
+ parameter = param[0]
22
+ value = URI.encode param[1].to_s
23
+ "#{_url}#{parameter}=#{value}&"
24
+ }.gsub(/&$/, "")
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/boty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Boty
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Valeriano
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-18 00:00:00.000000000 Z
11
+ date: 2015-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -168,6 +168,7 @@ files:
168
168
  - Procfile
169
169
  - README.md
170
170
  - Rakefile
171
+ - bin/bot
171
172
  - bin/console
172
173
  - bin/setup
173
174
  - boty.gemspec
@@ -179,6 +180,10 @@ files:
179
180
  - lib/boty/message.rb
180
181
  - lib/boty/rspec.rb
181
182
  - lib/boty/session.rb
183
+ - lib/boty/slack.rb
184
+ - lib/boty/slack/chat.rb
185
+ - lib/boty/slack/rtm.rb
186
+ - lib/boty/slack/url.rb
182
187
  - lib/boty/version.rb
183
188
  - lib/tasks/.keep
184
189
  - log/.keep
@@ -220,4 +225,3 @@ signing_key:
220
225
  specification_version: 4
221
226
  summary: Boty is a pretty bot specially tailored for Slack.
222
227
  test_files: []
223
- has_rdoc: