telegram_bot 0.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: 9a92c64ad73728e0f777bf7d403e6bb62f13d698
4
+ data.tar.gz: 96e0b8b4ca3d224c6c263ea9ad1b056e9c01c683
5
+ SHA512:
6
+ metadata.gz: 317077a37c2a9fbb4fe2fb7e8cddb0dcf4005563e4648b882b0f6e06f04756794b5fe480f0bed7540b4d2f665d26d8e6bdf17d402d3defa519c0b1ecd9656597
7
+ data.tar.gz: 24581019d494cfc0ac94754b77b6efc2d0bd910e749f16b0aacba6c09fedbaad8595aa17b7d897612599d4b86d33459f8d053eeebc8a9aec200893d7879a0306
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in telegram_bot.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 José Tomás Albornoz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # TelegramBot
2
+
3
+ A charismatic client for [Telegram's Bot API](https://core.telegram.org/bots).
4
+
5
+ Write your own Telegram Bot!
6
+
7
+ Currently under heavy development.
8
+ Contributions are always welcome.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile (currently under development):
13
+
14
+ ```ruby
15
+ gem 'telegram_bot', github: 'eljojo/telegram_bot'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install telegram_bot
25
+
26
+ ## Usage
27
+
28
+ Here's an example:
29
+
30
+ ```ruby
31
+ require 'telegram_bot'
32
+ require 'pp'
33
+
34
+ bot = TelegramBot.new('[YOUR TELEGRAM BOT TOKEN GOES HERE]')
35
+ bot.get_updates do |message|
36
+ pp(message)
37
+ command = message.get_command_for(bot)
38
+
39
+ message.reply do |reply|
40
+ reply.text = "i think that #{command.inspect} is false"
41
+ pp(reply)
42
+ reply.send_with(bot)
43
+ end
44
+ end
45
+ ```
46
+
47
+ ## How do I get a Bot Token
48
+
49
+ Talk to the [@BotFather](https://telegram.me/botfather).
50
+ You can find more info [here](https://core.telegram.org/bots).
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/eljojo/telegram_bot/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,20 @@
1
+ require 'excon'
2
+ require 'virtus'
3
+ require 'json'
4
+
5
+ require "telegram_bot/version"
6
+ require "telegram_bot/user"
7
+ require "telegram_bot/group_chat"
8
+ require "telegram_bot/channel"
9
+ require "telegram_bot/message"
10
+ require "telegram_bot/out_message"
11
+ require "telegram_bot/update"
12
+ require "telegram_bot/api_response"
13
+ require "telegram_bot/bot"
14
+
15
+
16
+ module TelegramBot
17
+ def self.new(*args)
18
+ Bot.new(*args)
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module TelegramBot
2
+ class ApiResponse
3
+ attr_reader :body, :ok, :result
4
+
5
+ def initialize(res)
6
+ @body = res.body
7
+ if res.status == 200
8
+ data = JSON.parse(body)
9
+ @ok = data["ok"]
10
+ @result = data["result"]
11
+ else
12
+ @ok = false
13
+ end
14
+ end
15
+
16
+ alias_method :ok?, :ok
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ module TelegramBot
2
+ class Bot
3
+ ENDPOINT = 'https://api.telegram.org/'
4
+
5
+ attr_reader :me
6
+ alias_method :identity, :me
7
+
8
+ def initialize(token)
9
+ @token = token
10
+ @timeout = 50
11
+ @offset = 0
12
+ @connection = Excon.new(ENDPOINT, persistent: true)
13
+ @me = get_me
14
+ end
15
+
16
+ def get_me
17
+ response = request(:getMe)
18
+ User.new(response.result)
19
+ end
20
+
21
+ def get_updates(&block)
22
+ loop do
23
+ response = request(:getUpdates, offset: @offset, timeout: @timeout)
24
+ response.result.each do |raw_update|
25
+ update = Update.new(raw_update)
26
+ @offset = update.id + 1
27
+ yield update.message
28
+ end
29
+ end
30
+ end
31
+
32
+ def send_message(out_message)
33
+ response = request(:sendMessage, out_message.to_h)
34
+ Message.new(response.result)
35
+ end
36
+
37
+
38
+ private
39
+ attr_reader :token
40
+ def request(action, query = {})
41
+ path = "/bot#{@token}/#{action}"
42
+ res = @connection.post(path: path, query: query)
43
+ ApiResponse.new(res)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ module TelegramBot
2
+ class Channel
3
+ include Virtus.model
4
+ attribute :id, Integer
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module TelegramBot
2
+ class GroupChat
3
+ include Virtus.model
4
+ attribute :id, Integer
5
+ alias_method :to_i, :id
6
+ attribute :title, String
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module TelegramBot
2
+ class Message
3
+ include Virtus.model
4
+ attribute :message_id, Integer
5
+ alias_method :id, :message_id
6
+ alias_method :to_i, :id
7
+ attribute :from, User
8
+ attribute :text, String
9
+ attribute :date, DateTime
10
+ attribute :chat, Channel
11
+ attribute :reply_to_message, Message
12
+
13
+ def reply(&block)
14
+ reply = OutMessage.new(chat_id: chat.id)
15
+ yield reply if block_given?
16
+ reply
17
+ end
18
+
19
+ def get_command_for(bot)
20
+ text && text.sub(Regexp.new("@#{bot.identity.username}($|\s|\.|,)", Regexp::IGNORECASE), '').strip
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module TelegramBot
2
+ class OutMessage
3
+ include Virtus.model
4
+ attribute :chat_id, Integer
5
+ attribute :text, String
6
+
7
+ def send_with(bot)
8
+ bot.send_message(self)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module TelegramBot
2
+ class Update
3
+ include Virtus.model
4
+ attribute :update_id, Integer
5
+ alias_method :id, :update_id
6
+ alias_method :to_i, :id
7
+ attribute :message, Message
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module TelegramBot
2
+ class User
3
+ include Virtus.model
4
+ attribute :id, Integer
5
+ alias_method :to_i, :id
6
+ attribute :first_name, String
7
+ attribute :last_name, String
8
+ attribute :username, String
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module TelegramBot
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
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/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "telegram_bot"
8
+ spec.version = TelegramBot::VERSION
9
+ spec.authors = ["José Tomás Albornoz"]
10
+ spec.email = ["jojo@eljojo.net"]
11
+ spec.summary = %q{Simple client for Telegram's Bot API}
12
+ spec.description = %q{Still in development. Simple client for Telegram's Bot API}
13
+ spec.homepage = "https://github.com/eljojo/telegram_bot"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "excon", ">= 0.30.0"
22
+ spec.add_dependency "virtus", ">= 1.0.0"
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegram_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - José Tomás Albornoz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.30.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.30.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: virtus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Still in development. Simple client for Telegram's Bot API
70
+ email:
71
+ - jojo@eljojo.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/telegram_bot.rb
82
+ - lib/telegram_bot/api_response.rb
83
+ - lib/telegram_bot/bot.rb
84
+ - lib/telegram_bot/channel.rb
85
+ - lib/telegram_bot/group_chat.rb
86
+ - lib/telegram_bot/message.rb
87
+ - lib/telegram_bot/out_message.rb
88
+ - lib/telegram_bot/update.rb
89
+ - lib/telegram_bot/user.rb
90
+ - lib/telegram_bot/version.rb
91
+ - telegram_bot.gemspec
92
+ homepage: https://github.com/eljojo/telegram_bot
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Simple client for Telegram's Bot API
116
+ test_files: []