tars 0.1.0 → 0.1.1

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: 4415bd1f9dad1a5098a8c955b2de1ca1c35c0751
4
- data.tar.gz: 48a9bbe84eda7225d1aeea31e8f9995d833f2627
3
+ metadata.gz: 9f5fe67b91d5d033d41f2c7123050a323a782ec8
4
+ data.tar.gz: 3af01b0411e8767080be738b85def34a2b609907
5
5
  SHA512:
6
- metadata.gz: cd216d5161e618153bd580b866b817e4ef0186c105d3c8674af62faa0b1874dfe55c4fb02bcc247092108cd25348471805383185212dfbc6e48d6a1742d7369b
7
- data.tar.gz: 416ed95b264aa835ff58190406a543f5cc9c87fe32811a57ce4a09e2ceb9773a2fc4d1a4538e6b800d8f42bc7e4f294f908578ca109c12a24682bb1de94adc6c
6
+ metadata.gz: aff30a532f45666f69df07c54d3175e5abb15006648320babcc05268e9cad62c036c908792bd93da0334c77b96b1eabaca1efee9bc4debb39907f179b975d86e
7
+ data.tar.gz: 6f908bef3686853813d73f3f107803416d69ff7396593d8c9209a6a7a885747b154ccfdcf78fc2c21be64e6d0e16d9e78858e8659ed6011a1bfb5eff7c5b8340
data/README.md CHANGED
@@ -43,6 +43,32 @@ end
43
43
 
44
44
  TARS.bootstrap
45
45
  ```
46
+ ## API
47
+
48
+ `TARS.configure`:
49
+ This configuration block allow you to specify your bot settings. Options are:
50
+
51
+ - `token`: This is your bot token, keep it private. To obtain your bot token you should talk to the @botFather and register your bot.
52
+
53
+ - `webhook`: The URL that telegram will use to send you update each time your bot receive a message. This URL should point to this bot server where you run TARS. It's should be in HTTPS with a valid SSL certificate. See *How to run TARS behind Nginx* below for more information about configuring SSL.
54
+
55
+ - `server.port`: The port of the local TARS server. This can be anything if you don't want to use TARS behind a reverse proxy. However it should be a valid port handled by Telegram Bot API (Currently 80, 443, 8443)
56
+
57
+ - `server.path`: The prefix of the URL to access TARS server. This could be anything
58
+
59
+ *Note:* The server Hash can contain any WEBrick params so you can set SSL directly with TARS. See *How to run TARS with WEBrick and SSL* for more informations.
60
+
61
+ ## Todo
62
+
63
+ - [x] Create a check list
64
+ - [ ] Write documentation
65
+ - [ ] Add unit and fonctionnal test
66
+ - [ ] Add option to deamonize/fork in backgrounds
67
+ - [ ] Handle telegram API errors
68
+ - [ ] Allow to pass a RegEx to `on` as first parameter
69
+ - [ ] Implement a Logger
70
+ - [ ] Clean WEBrick output to stdout
71
+ - [ ] Colored output
46
72
 
47
73
  ## Contributing
48
74
 
data/lib/tars.rb CHANGED
@@ -3,6 +3,8 @@ require 'tars/api'
3
3
  require 'tars/update'
4
4
  require 'tars/bot'
5
5
 
6
+ require 'logger'
7
+
6
8
  module TARS
7
9
  class << self
8
10
  attr_accessor :config, :bot
@@ -19,13 +21,11 @@ module TARS
19
21
  end
20
22
 
21
23
  def self.bootstrap
22
- me = TARS::API.me
23
-
24
- puts "Setting webhook for Bot to #{TARS.config.webhook} ..."
24
+ puts "Setting webhook for Bot to #{TARS.config.webhook}"
25
25
  TARS::API.webhook
26
26
 
27
- puts 'Running local server..'
28
-
29
- TARS::Server.new
27
+ puts 'Launch TARS server'
28
+ server = TARS::Server.new
29
+ server.run!
30
30
  end
31
31
  end
data/lib/tars/api.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  require 'json'
2
2
  require 'httparty'
3
+ require 'rest-client'
4
+ require 'pp'
5
+ require 'open-uri'
6
+
7
+ BASE_URI = 'api.telegram.org'
3
8
 
4
9
  module TARS
5
10
  class API
6
11
  include HTTParty
7
- base_uri 'api.telegram.org'
12
+ base_uri BASE_URI
8
13
 
9
14
  def self.me
10
15
  request 'getMe'
@@ -22,6 +27,15 @@ module TARS
22
27
  request 'sendMessage', chat_id: chat_id, text: text
23
28
  end
24
29
 
30
+ def self.reply_with_photo(chat_id, photo)
31
+ uri = "https://#{BASE_URI}/bot#{TARS.config.token}/sendPhoto"
32
+ random = "/tmp/#{Digest::MD5.hexdigest(photo)}#{File.extname(photo)}"
33
+
34
+ IO.copy_stream(open(photo), random)
35
+ RestClient.post(uri, chat_id: chat_id, photo: open(random))
36
+ File.delete(random)
37
+ end
38
+
25
39
  def self.request(endpoint, options = {})
26
40
  post("/bot#{TARS.config.token}/#{endpoint}", query: options)
27
41
  end
data/lib/tars/bot.rb CHANGED
@@ -15,10 +15,17 @@ module TARS
15
15
  end
16
16
 
17
17
  class Server
18
+ class << self
19
+ attr_reader :server
20
+ end
21
+
18
22
  def initialize
19
- server = WEBrick::HTTPServer.new Port: TARS.config.server[:port]
20
- server.mount TARS.config.server[:path], TARS::PostHandler
21
- server.start
23
+ @server = WEBrick::HTTPServer.new Port: TARS.config.server[:port]
24
+ @server.mount TARS.config.server[:path], TARS::PostHandler
25
+ end
26
+
27
+ def run!
28
+ @server.start
22
29
  end
23
30
  end
24
31
 
@@ -31,13 +38,14 @@ module TARS
31
38
  @cmds = {}
32
39
  end
33
40
 
34
- def on(message, &block)
35
- @cmds[message] = block
41
+ def on(command, &block)
42
+ @cmds[command] = block
36
43
  end
37
44
 
38
- def execute(command, message)
45
+ def execute(command, update)
39
46
  return unless @cmds.key?(command)
40
- @cmds[command].call(message)
47
+ message = update.instance_variable_get('@message')
48
+ @cmds[command].call(message, message['chat']['id'])
41
49
  end
42
50
  end
43
51
  end
data/lib/tars/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TARS
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
data/tars.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency "json", "~> 1.8"
21
21
  spec.add_dependency "httparty", "~> 0.13.5"
22
+ spec.add_dependency "rest-client", "~> 1.8.0"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.8"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Yadomi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-26 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.13.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -75,11 +89,9 @@ extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
77
91
  - ".travis.yml"
78
- - CODE_OF_CONDUCT.md
79
92
  - Gemfile
80
93
  - README.md
81
94
  - Rakefile
82
- - bin/console
83
95
  - bin/setup
84
96
  - lib/tars.rb
85
97
  - lib/tars/api.rb
data/CODE_OF_CONDUCT.md DELETED
@@ -1,13 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
-
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
-
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
-
9
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
-
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
-
13
- This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "TARS"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start