telegram_bot_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bbfe722f4229bb6884ad39ea06885094f6cdbbfa
4
+ data.tar.gz: 7b97cba693680760ab2531b861d8174e47906f59
5
+ SHA512:
6
+ metadata.gz: cc5bd16901d7e50885d700d1262184d733bdd444d455035b7f265e11bbe50bc27744914a96e48bf58c6171a36ab960e6fbda40f81ae71518c08f20a042efa138
7
+ data.tar.gz: 5cc7cbd0d7e905f28d322fe3eb820f13641edf6dac8f6c1bcb7d1cf9e49d8527ab72da3c1c35d16241359fe68c4039ba8cdfbc3b435933884e6fea3134955098
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format=doc
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in telegram_bot_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bernat Rafales
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # TelegramBotApi [![Build Status](https://travis-ci.org/brafales/telegram_bot_api.svg?branch=master)](http://travis-ci.org/brafales/telegram_bot_api)
2
+
3
+ Telegram Bot Api written in Ruby. Still in early stages, supports for now only a couple of methods.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'telegram_bot_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install telegram_bot_api
20
+
21
+ ## Usage
22
+
23
+ Configure the gem using this snippet:
24
+
25
+ ```ruby
26
+ TelegramBotApi.configure do |config|
27
+ config.auth_token = YOUR_BOT_AUTH_TOKEN
28
+ end
29
+ ```
30
+
31
+ Once the auth token has been setup, you can create request objects,
32
+ configure them to your liking and execute them using the `Client` class.
33
+
34
+ Here's how to create a `SendMessage` request:
35
+
36
+ ```ruby
37
+ request = TelegramBotApi::Requests::SendMessage.new(
38
+ {
39
+ chat_id: 1234,
40
+ text: "This is a test message"
41
+ }
42
+ )
43
+
44
+ TelegramBotApi::Client.execute(request)
45
+ ```
46
+
47
+ The gem also provides some simple validation of requests, so you can make sure all the mandatory parameters have been sent:
48
+
49
+ ```ruby
50
+ request = TelegramBotApi::Requests::SendMessage.new(
51
+ {
52
+ text: "This is a test message"
53
+ }
54
+ )
55
+
56
+ request.valid? #false
57
+ request.errors #[:chat_id]
58
+ ```
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/brafales/telegram_bot_api.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "telegram_bot_api"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ module TelegramBotApi
2
+ class Client
3
+ TELEGRAM_API_ENDPOINT = "https://api.telegram.org"
4
+
5
+ def self.execute(request)
6
+ unless request.valid?
7
+ raise(ArgumentError, request.errors)
8
+ end
9
+ HttpClient.make_request(verb: request.verb, url: request_url(request), params: request.to_json)
10
+ end
11
+
12
+ private
13
+
14
+ def self.request_url(request)
15
+ "#{TELEGRAM_API_ENDPOINT}/#{bot_path}/#{request.endpoint_url}"
16
+
17
+ end
18
+
19
+ def self.bot_path
20
+ auth_token = TelegramBotApi.configuration.auth_token
21
+ "bot#{auth_token}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module TelegramBotApi
2
+ class Configuration
3
+ attr_accessor :auth_token
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require 'typhoeus'
2
+
3
+ module TelegramBotApi
4
+ class HttpClient
5
+
6
+ def self.make_request(verb:, url:, params:)
7
+ method = case verb
8
+ when :get
9
+ :get
10
+ when :post
11
+ :post
12
+ end
13
+
14
+ unless method
15
+ raise(ArgumentError, "Invalid verb")
16
+ end
17
+
18
+ self.send(method, url: url, params: params)
19
+ end
20
+
21
+ private
22
+
23
+ def self.get(url:, params: {})
24
+ Typhoeus.get(url, headers: {'Content-Type'=> "application/json"}, params: params)
25
+ end
26
+
27
+ def self.post(url:, params: {})
28
+ Typhoeus.post(url, headers: {'Content-Type'=> "application/json"}, params: params)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module TelegramBotApi
2
+ class Message
3
+
4
+ attr_reader :text, :chat
5
+
6
+ def initialize(payload)
7
+ @text = payload["text"]
8
+ @chat = payload["chat"]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,55 @@
1
+ module TelegramBotApi
2
+ module Requests
3
+ module Base
4
+
5
+ def self.included(base)
6
+ base.include InstanceMethods
7
+ base.extend ClassMethods
8
+ base.class_eval { attr_accessor *self.all_arguments }
9
+ end
10
+
11
+ module InstanceMethods
12
+
13
+ def to_json
14
+ self.class.all_arguments.inject({}) do |memo, argument|
15
+ memo.merge!({ argument => self.public_send(argument) })
16
+ end
17
+ end
18
+
19
+ def valid?
20
+ errors.empty?
21
+ end
22
+
23
+ def errors
24
+ self.class.mandatory_arguments.select do |argument|
25
+ self.public_send(argument).nil?
26
+ end
27
+ end
28
+
29
+ #For convenience
30
+
31
+ def endpoint_url
32
+ self.class.endpoint_url
33
+ end
34
+
35
+ def verb
36
+ self.class.verb
37
+ end
38
+
39
+ private
40
+
41
+ def build_arguments(arguments)
42
+ self.class.all_arguments.each do |argument|
43
+ instance_variable_set("@#{argument}", arguments[argument])
44
+ end
45
+ end
46
+ end
47
+
48
+ module ClassMethods
49
+ def all_arguments
50
+ mandatory_arguments + optional_arguments
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ module TelegramBotApi
2
+ module Requests
3
+ class SendMessage
4
+
5
+ def initialize(arguments = {})
6
+ build_arguments(arguments)
7
+ end
8
+
9
+ class << self
10
+ def mandatory_arguments
11
+ %i(chat_id text)
12
+ end
13
+
14
+ def optional_arguments
15
+ %i(disable_web_page_preview reply_to_message_id reply_markup)
16
+ end
17
+
18
+ def endpoint_url
19
+ 'sendMessage'
20
+ end
21
+
22
+ def verb
23
+ :post
24
+ end
25
+ end
26
+
27
+ include Base
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module TelegramBotApi
2
+ module Requests
3
+ class SetWebhook
4
+
5
+ def initialize(arguments = {})
6
+ build_arguments(arguments)
7
+ end
8
+
9
+ class << self
10
+ def mandatory_arguments
11
+ []
12
+ end
13
+
14
+ def optional_arguments
15
+ %i(url)
16
+ end
17
+
18
+ def endpoint_url
19
+ 'setWebhook'
20
+ end
21
+
22
+ def verb
23
+ :get
24
+ end
25
+ end
26
+
27
+ include Base
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ require_relative "requests/base"
2
+ require_relative "requests/set_webhook"
3
+ require_relative "requests/send_message"
@@ -0,0 +1,3 @@
1
+ module TelegramBotApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ require_relative "telegram_bot_api/version"
2
+ require_relative "telegram_bot_api/configuration"
3
+ require_relative "telegram_bot_api/http_client"
4
+ require_relative "telegram_bot_api/client"
5
+ require_relative "telegram_bot_api/message"
6
+ require_relative "telegram_bot_api/requests"
7
+
8
+ module TelegramBotApi
9
+
10
+ @configuration = Configuration.new
11
+
12
+ class << self
13
+ attr_accessor :configuration
14
+ end
15
+
16
+ def self.configure
17
+ yield(configuration)
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'telegram_bot_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "telegram_bot_api"
8
+ spec.version = TelegramBotApi::VERSION
9
+ spec.authors = ["Bernat Rafales"]
10
+ spec.email = ["brafales@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby client for the Telegram Bot API}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/brafales/telegram_bot_api"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3"
25
+
26
+ spec.add_dependency "typhoeus", "~> 0"
27
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegram_bot_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bernat Rafales
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-26 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: typhoeus
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: Ruby client for the Telegram Bot API
70
+ email:
71
+ - brafales@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/telegram_bot_api.rb
86
+ - lib/telegram_bot_api/client.rb
87
+ - lib/telegram_bot_api/configuration.rb
88
+ - lib/telegram_bot_api/http_client.rb
89
+ - lib/telegram_bot_api/message.rb
90
+ - lib/telegram_bot_api/requests.rb
91
+ - lib/telegram_bot_api/requests/base.rb
92
+ - lib/telegram_bot_api/requests/send_message.rb
93
+ - lib/telegram_bot_api/requests/set_webhook.rb
94
+ - lib/telegram_bot_api/version.rb
95
+ - telegram_bot_api.gemspec
96
+ homepage: https://github.com/brafales/telegram_bot_api
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Ruby client for the Telegram Bot API
120
+ test_files: []