telebot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dac629c038645d92ad08c056c49abdb6af84445b
4
+ data.tar.gz: a5ce13f553e827434d788b749cb4ef9ba71cc9ea
5
+ SHA512:
6
+ metadata.gz: d7ba8b3de11ada50ff7c0484cc4b305ef972ca24ea3994f578f43eaa65f00b45ec9f8ee00270ab3ca0d85de839d7f5cdc29b09cfcd3684dfb32b0752044a92f7
7
+ data.tar.gz: 60863c58f0cc56969e03ae3edbce2a4e35da5b3682d87a4d6ed363f671d8da42d75f896eec4e341d95f284118bec3e692a5e891f41d018cdd0c0713084f7c2e6
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # To use benefits of vim-rails plugin
12
+ /config/environment.rb
13
+
14
+ # Do not public token of the demo bot
15
+ examples/DEMO_TOKEN
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ telebot
@@ -0,0 +1 @@
1
+ 2.1.5
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.4
@@ -0,0 +1,13 @@
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, ethnicity, 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/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in telebot.gemspec
4
+ gemspec
5
+
6
+
7
+ group :development, :test do
8
+ gem 'pry'
9
+ gem 'guard'
10
+ gem 'guard-rspec'
11
+ gem 'libnotify'
12
+ end
@@ -0,0 +1,43 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # RSpec files
32
+ rspec = dsl.rspec
33
+ watch(rspec.spec_helper) { rspec.spec_dir }
34
+ watch(rspec.spec_support) { rspec.spec_dir }
35
+ watch(rspec.spec_files)
36
+
37
+ # Ruby files
38
+ ruby = dsl.ruby
39
+ dsl.watch_spec_files_for(ruby.lib_files)
40
+
41
+ end
42
+
43
+ notification :libnotify, timeout: 1, transient: true, append: true, urgency: :critical
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Potapov Sergey
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.
@@ -0,0 +1,73 @@
1
+ # Telebot
2
+
3
+ Ruby client for [Telegram's Bot API](https://core.telegram.org/bots).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'telebot'
11
+ ```
12
+
13
+ Run
14
+
15
+ ```
16
+ $ gem install telebot
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Minimal example, that repeats a message, would look like this:
22
+
23
+ ```ruby
24
+ bot = Telebot::Bot.new(TOKEN)
25
+
26
+ bot.run do |client, message|
27
+ reply = "You've said: #{message.text}"
28
+ client.send_message(chat_id: message.chat.id, text: reply)
29
+ end
30
+ ```
31
+
32
+ Please check out full featured example as well: [demo.rb](https://github.com/greyblake/telebot/blob/master/examples/demo.rb).
33
+
34
+
35
+ ### Using pure client
36
+
37
+ ```ruby
38
+ client = Telebot::Client.new(TOKEN)
39
+
40
+ # Perform getMe call
41
+ client.get_me
42
+ # =>
43
+ # #<Telebot::User:0x0000000111f688
44
+ # @first_name="Telebot Demo",
45
+ # @id=97445105,
46
+ # @last_name=nil,
47
+ # @username="demoTelebot">
48
+
49
+ # Send message to chat with id=12345
50
+ client.send_message(chat_id: 12345, text: "Hello there!")
51
+ ```
52
+ The client supports all [Telegram's Bot API methods](https://core.telegram.org/bots/api#available-methods).
53
+
54
+ For more information check out [client.rb](https://github.com/greyblake/telebot/blob/master/lib/telebot/client.rb)
55
+ and [demo.rb](https://github.com/greyblake/telebot/blob/master/examples/demo.rb).
56
+
57
+
58
+ ## Running demo
59
+
60
+ Clone the repo and run demo example to see how you can use [Telegram's Bot API](https://core.telegram.org/bots).
61
+
62
+ ```ruby
63
+ git clone /github.com/greyblake/telebot
64
+ cd telebot
65
+ bundle install
66
+
67
+ TOKEN=<YOUR_TOKEN> ruby ./examples/demo.rb
68
+ ```
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "telebot"
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
@@ -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,94 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+
5
+ require 'telebot'
6
+ require 'pp'
7
+
8
+ TOKEN_FILE = File.expand_path("../DEMO_TOKEN", __FILE__)
9
+
10
+ def fixture(file_name)
11
+ File.expand_path("../fixtures/#{file_name}", __FILE__)
12
+ end
13
+
14
+ token =
15
+ if File.exist?(TOKEN_FILE)
16
+ File.read(TOKEN_FILE).strip
17
+ elsif ENV['TOKEN']
18
+ ENV['TOKEN']
19
+ else
20
+ abort "Please set token as TOKEN env variable"
21
+ end
22
+
23
+ bot = Telebot::Bot.new(token)
24
+
25
+ bot.run do |client, message|
26
+ case message.text
27
+ when /get_me/
28
+ user = client.get_me
29
+ msg = "ID: #{user.id}\n"
30
+ msg << "First name: #{user.first_name}\n"
31
+ msg << "Last name: #{user.last_name}\n"
32
+ msg << "Username: #{user.username}"
33
+ client.send_message(chat_id: message.chat.id, text: msg)
34
+
35
+ when /send_message/
36
+ client.send_message(chat_id: message.chat.id, text: "Hello, man! What's going on?")
37
+
38
+ when /forward_message/
39
+ client.forward_message(chat_id: message.chat.id, from_chat_id: message.chat.id, message_id: message.message_id)
40
+
41
+ when /send_photo/
42
+ file = Telebot::InputFile.new(fixture("bender_pic.jpg"), 'image/jpeg')
43
+ client.send_photo(chat_id: message.chat.id, photo: file)
44
+
45
+ when /send_audio/
46
+ client.send_message(chat_id: message.chat.id, text: "Let me say 'Hi' in Esperanto.")
47
+ file = Telebot::InputFile.new(fixture("saluton_amiko.ogg"))
48
+ client.send_audio(chat_id: message.chat.id, audio: file)
49
+
50
+ when /send_document/
51
+ file = Telebot::InputFile.new(__FILE__)
52
+ client.send_document(chat_id: message.chat.id, document: file)
53
+
54
+ when /send_sticker/
55
+ file = Telebot::InputFile.new(fixture("zamenhof_sticker.webp"))
56
+ client.send_sticker(chat_id: message.chat.id, sticker: file)
57
+
58
+ when /send_video/
59
+ file_id = "BAADAgADOQADqwcsBcskdD5ZfXZyAg"
60
+ client.send_video(chat_id: message.chat.id, video: file_id)
61
+
62
+ when /send_location/
63
+ client.send_location(chat_id: message.chat.id, latitude: 53.131684, longitude: 23.169556)
64
+
65
+ when /send_chat_action/
66
+ client.send_message(chat_id: message.chat.id, text: "Check my status, I am typing")
67
+ client.send_chat_action(chat_id: message.chat.id, action: :typing)
68
+ sleep 3
69
+ client.send_message(chat_id: message.chat.id, text: "Done")
70
+
71
+ when /get_user_profile_photos/
72
+ photos = client.get_user_profile_photos(user_id: message.from.id)
73
+ if photos.total_count > 0
74
+ client.send_message(chat_id: message.chat.id, text: "Look at yourself!")
75
+ photo = photos.photos.first
76
+ photo_size = photo.first
77
+ client.send_photo(chat_id: message.chat.id, photo: photo_size.file_id)
78
+ else
79
+ client.send_message(chat_id: message.chat.id, text: "Your profile has no photos")
80
+ end
81
+
82
+ else
83
+ markup = Telebot::ReplyKeyboardMarkup.new(
84
+ keyboard: [
85
+ [ "get_me", "send_message", "forward_message" ],
86
+ [ "send_photo", "send_audio", "send_document" ],
87
+ [ "send_sticker", "send_video", "send_location"],
88
+ [ "send_chat_action", "get_user_profile_photos"]
89
+ ]
90
+ )
91
+
92
+ client.send_message(chat_id: message.chat.id, text: 'Unknown command', reply_markup: markup)
93
+ end
94
+ end
@@ -0,0 +1,49 @@
1
+ require 'virtus'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+
5
+ require "telebot/objects/user"
6
+ require "telebot/objects/group_chat"
7
+ require "telebot/objects/photo_size"
8
+ require "telebot/objects/audio"
9
+ require "telebot/objects/contact"
10
+ require "telebot/objects/location"
11
+ require "telebot/objects/document"
12
+ require "telebot/objects/sticker"
13
+ require "telebot/objects/video"
14
+ require "telebot/objects/message"
15
+ require "telebot/objects/update"
16
+ require "telebot/objects/input_file"
17
+ require "telebot/objects/user_profile_photos"
18
+ require "telebot/objects/reply_keyboard_markup"
19
+
20
+ require "telebot/version"
21
+ require "telebot/client"
22
+ require "telebot/response"
23
+ require "telebot/bot"
24
+ require "telebot/errors"
25
+
26
+
27
+ module Telebot
28
+ end
29
+
30
+
31
+ # Implemented objects:
32
+ # * User
33
+ # * GroupChat
34
+ # * PhotoSize
35
+ # * Audio
36
+ # * Contact
37
+ # * Location
38
+ # * Document
39
+ # * Sticker
40
+ # * Video
41
+ # * Message
42
+ # * Update
43
+ # * InputFile
44
+ # * UserProfilePhotos
45
+ # * ReplyKeyboardMarkup
46
+
47
+ # TODO:
48
+ # * ReplyKeyboardHide
49
+ # * ForceReply
@@ -0,0 +1,32 @@
1
+ module Telebot
2
+ class Bot
3
+ # Default update time
4
+ DEFAULT_UPDATE_TIME = 2
5
+
6
+ attr_reader :client, :processed_update_ids, :update_time
7
+
8
+ def initialize(token, update_time: DEFAULT_UPDATE_TIME)
9
+ @client = Client.new(token)
10
+ @processed_update_ids = []
11
+ @update_time = update_time
12
+ end
13
+
14
+ def run(&block)
15
+ loop do
16
+ updates = @client.get_updates(offset: @processed_update_ids.last)
17
+
18
+ updates.each do |update|
19
+ next if @processed_update_ids.include?(update.update_id)
20
+ process_update(update, block)
21
+ @processed_update_ids << update.update_id
22
+ end
23
+
24
+ sleep(@update_time)
25
+ end
26
+ end
27
+
28
+ private def process_update(update, block)
29
+ block.call(client, update.message)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,228 @@
1
+ module Telebot
2
+ class Client
3
+ API_URL = "https://api.telegram.org".freeze
4
+
5
+ # Available chat actions
6
+ CHAT_ACTIONS = [:typing, :upload_photo, :record_video, :upload_video, :record_audio, :upload_audio, :upload_document, :find_location].freeze
7
+
8
+ def initialize(token, adapter: :net_http)
9
+ fail(ArgumentError, "token can't be empty") if token.nil? || token.empty?
10
+ @token = token
11
+
12
+ @faraday = Faraday.new(API_URL) do |conn|
13
+ conn.request :multipart
14
+ conn.request :url_encoded
15
+ conn.response :json, :content_type => /\bjson$/
16
+ conn.adapter adapter
17
+ end
18
+ end
19
+
20
+ # A simple method for testing your bot's auth token. Requires no parameters.
21
+ # Returns basic information about the bot in form of a User object.
22
+ #
23
+ # @return [Telebot::User]
24
+ def get_me
25
+ result = call(:getMe)
26
+ User.new(result)
27
+ end
28
+
29
+ # Use this method to receive incoming updates using long polling.
30
+ # An Array of Update objects is returned.
31
+ #
32
+ # Note:
33
+ # 1. This method will not work if an outgoing webhook is set up.
34
+ # 2. In order to avoid getting duplicate updates, recalculate offset after each server response.
35
+ #
36
+ # @param offset [Integer]
37
+ # Identifier of the first update to be returned.
38
+ # Must be greater by one than the highest among the identifiers of
39
+ # previously received updates. By default, updates starting with the
40
+ # earliest unconfirmed update are returned. An update is considered
41
+ # confirmed as soon as getUpdates is called with an offset higher
42
+ # than its update_id.
43
+ # @param limit [Integer]
44
+ # Limits the number of updates to be retrieved. Values between 1—100 are accepted.
45
+ # Defaults to 100
46
+ # @param timeout [Integer]
47
+ # Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling.
48
+ #
49
+ # @return [Array<Telebot::Update>]
50
+ def get_updates(offset: nil, limit: nil, timeout: nil)
51
+ result = call(:getUpdates, offset: offset, limit: limit, timeout: timeout)
52
+ result.map { |update_hash| Update.new(update_hash) }
53
+ end
54
+
55
+ # Send text message.
56
+ #
57
+ # @param chat_id [Integer] Unique identifier for the message recipient - User or GroupChat id
58
+ # @param text [String] Text of the message to be sent
59
+ # @param disable_web_page_preview [Boolean] Disables link previews for links in this message
60
+ # @param reply_to_message_id [Integer] If the message is a reply, ID of the original message
61
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply] Additional interface options
62
+ #
63
+ # @return [Telebot::Message]
64
+ def send_message(chat_id:, text:, disable_web_page_preview: false, reply_to_message_id: nil, reply_markup: nil)
65
+ result = call(:sendMessage,
66
+ chat_id: chat_id,
67
+ text: text,
68
+ disable_web_page_preview: disable_web_page_preview,
69
+ reply_to_message_id: reply_to_message_id,
70
+ reply_markup: reply_markup
71
+ )
72
+ Message.new(result)
73
+ end
74
+
75
+ # Use this method to forward messages of any kind.
76
+ #
77
+ # @param chat_id [Integer] Unique identifier for the message recipient - User or GroupChat id
78
+ # @param from_chat_id [Integer] Unique identifier for the chat where the original message was sent - User or GroupChat id
79
+ # @param message_id [Integer] Unique message identifier
80
+ #
81
+ # @return [Telebot::Message]
82
+ def forward_message(chat_id:, from_chat_id:, message_id:)
83
+ result = call(:forwardMessage, chat_id: chat_id, from_chat_id: from_chat_id, message_id: message_id)
84
+ Message.new(result)
85
+ end
86
+
87
+ # Send a picture.
88
+ #
89
+ # @param chat_id [Integer] Unique identifier for the message recipient - User or GroupChat id
90
+ # @param photo [InputFile, String] Photo to send. You can either pass a
91
+ # file_id as String to resend a photo that is already on the Telegram servers,
92
+ # or upload a new photo using multipart/form-data.
93
+ # @param caption [String] Photo caption (may also be used when resending photos by file_id)
94
+ # @param reply_to_message_id [Integer] If the message is a reply, ID of the original message
95
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply] Additional interface options
96
+ #
97
+ # @return [Telebot::Message]
98
+ def send_photo(chat_id:, photo:, caption: nil, reply_to_message_id: nil, reply_markup: nil)
99
+ result = call(:sendPhoto, chat_id: chat_id, photo: photo, caption: caption, reply_to_message_id: reply_to_message_id, reply_markup: reply_markup)
100
+ Message.new(result)
101
+ end
102
+
103
+ # Use this method to send audio files, if you want Telegram clients to
104
+ # display the file as a playable voice message. For this to work, your
105
+ # audio must be in an .ogg file encoded with OPUS (other formats may be sent as Document)
106
+ #
107
+ # @param chat_id [Integer]
108
+ # @param audio [Telebot::InputFile, String] Audio file to send (file or file_id)
109
+ # @param reply_to_message_id [Integer] If the message is a reply, ID of the original message
110
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply] Additional interface options
111
+ #
112
+ # @return [Telebot::Message]
113
+ def send_audio(chat_id:, audio:, reply_to_message_id: nil, reply_markup: nil)
114
+ result = call(:sendAudio, chat_id: chat_id, audio: audio, reply_to_message_id: reply_to_message_id, reply_markup: reply_markup)
115
+ Message.new(result)
116
+ end
117
+
118
+ # Send general file.
119
+ #
120
+ # @param chat_id [Integer]
121
+ # @param document [Telebot::InputFile, String] document to send (file or file_id)
122
+ # @param reply_to_message_id [Integer] If the message is a reply, ID of the original message
123
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply] Additional interface options
124
+ #
125
+ # @return [Telebot::Message]
126
+ def send_document(chat_id:, document:, reply_to_message_id: nil, reply_markup: nil)
127
+ result = call(:sendDocument, chat_id: chat_id, document: document, reply_to_message_id: reply_to_message_id, reply_markup: reply_markup)
128
+ Message.new(result)
129
+ end
130
+
131
+ # Use this method to send .webp stickers.
132
+ #
133
+ # @param chat_id [Integer]
134
+ # @param sticker [Telebot::InputFile, String] sticker to send (file or file_id)
135
+ # @param reply_to_message_id [Integer] If the message is a reply, ID of the original message
136
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply] Additional interface options
137
+ #
138
+ # @return [Telebot::Message]
139
+ def send_sticker(chat_id:, sticker:, reply_to_message_id: nil, reply_markup: nil)
140
+ result = call(:sendSticker, chat_id: chat_id, sticker: sticker, reply_to_message_id: reply_to_message_id, reply_markup: reply_markup)
141
+ Message.new(result)
142
+ end
143
+
144
+ # Send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
145
+ #
146
+ # @param chat_id [Integer]
147
+ # @param video [InputFile, String] file or file_id
148
+ # @param reply_to_message_id [Integer]
149
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply]
150
+ #
151
+ # @return [Telebot::Message]
152
+ def send_video(chat_id:, video:, reply_to_message_id: nil, reply_markup: nil)
153
+ result = call(:sendVideo, chat_id: chat_id, video: video, reply_to_message_id: reply_to_message_id, reply_markup: reply_markup)
154
+ Message.new(result)
155
+ end
156
+
157
+ # Send a point on the map.
158
+ #
159
+ # @param chat_id [Integer]
160
+ # @param latitude [Integer]
161
+ # @param longitude [Integer]
162
+ # @param reply_to_message_id [Integer]
163
+ # @param reply_markup [ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply]
164
+ #
165
+ # @return [Telebot::Message]
166
+ def send_location(chat_id:, latitude:, longitude:, reply_to_message_id: nil, reply_markup: nil)
167
+ result = call(:sendLocation, chat_id: chat_id,
168
+ latitude: latitude,
169
+ longitude: longitude,
170
+ reply_to_message_id: reply_to_message_id,
171
+ reply_markup: reply_markup)
172
+ Message.new(result)
173
+ end
174
+
175
+ # Use this method when you need to tell the user that something is happening on the bot's side.
176
+ # The status is set for 5 seconds or less (when a message arrives from your bot,
177
+ # Telegram clients clear its typing status).
178
+ #
179
+ # @param chat_id [Integer]
180
+ # @param action [Symbol] :typing, :upload_photo, etc. See {CHAT_ACTIONS}.
181
+ #
182
+ # @return [void]
183
+ def send_chat_action(chat_id:, action:)
184
+ fail(ArgumentError, "Unknown chat action `#{action.inspect}`") unless CHAT_ACTIONS.include?(action)
185
+ call(:sendChatAction, chat_id: chat_id, action: action)
186
+ end
187
+
188
+ # Use this method to get a list of profile pictures for a user.
189
+ #
190
+ # @param user_id [Integer]
191
+ # @param offset [Integer]
192
+ # @param limit [Integer]
193
+ #
194
+ # @return [Telebot::UserProfilePhotos]
195
+ def get_user_profile_photos(user_id:, offset: nil, limit: nil)
196
+ result = call(:getUserProfilePhotos, user_id: user_id, offset: offset, limit: limit)
197
+ UserProfilePhotos.new(result)
198
+ end
199
+
200
+ # Use this method to specify a url and receive incoming updates via an outgoing webhook.
201
+ #
202
+ # @param url [String] HTTPS url to send updates to. Use an empty string to remove webhook integration
203
+ #
204
+ # @return [void]
205
+ def set_web_hook(url:)
206
+ call(:setWebhook, url: url)
207
+ end
208
+
209
+ private def call(method_name, params = {})
210
+ path = "/bot#{@token}/#{method_name}"
211
+ params.reject! { |key, value| value.nil? }
212
+
213
+ if params.has_key?(:reply_markup)
214
+ params[:reply_markup] = params[:reply_markup].to_json
215
+ end
216
+
217
+ faraday_response = @faraday.post(path, params)
218
+ response = Response.new(faraday_response.body)
219
+
220
+ if response.ok
221
+ response.result
222
+ else
223
+ error = Error.new(response.description, response.error_code)
224
+ fail(error)
225
+ end
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,10 @@
1
+ module Telebot
2
+ class Error < StandardError
3
+ attr_reader :error_code
4
+
5
+ def initialize(message, error_code = nil)
6
+ super(message)
7
+ @error_code = error_code
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module Telebot
2
+ # This object represents an audio file (voice note).
3
+ class Audio
4
+ include Virtus.model
5
+
6
+ # Unique identifier for this file
7
+ attribute :file_id, Integer
8
+
9
+ # Duration of the audio in seconds as defined by sender
10
+ attribute :duration, Integer
11
+
12
+ # Optional. MIME type of the file as defined by sender
13
+ attribute :mime_type, String
14
+
15
+ # Optional. File size
16
+ attribute :file_size, Integer
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Telebot
2
+ # This object represents a phone contact.
3
+ class Contact
4
+ include Virtus.model
5
+
6
+ # Contact's phone number
7
+ attribute :phone_number, String
8
+
9
+ # Contact's first name
10
+ attribute :first_name, String
11
+
12
+ # Optional. Contact's last name
13
+ attribute :last_name, String
14
+
15
+ # Optional. Contact's last name
16
+ attribute :user_id, String
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module Telebot
2
+ # This object represents a general file (as opposed to photos and audio files).
3
+ class Document
4
+ include Virtus.model
5
+
6
+ # Unique file identifier
7
+ attribute :file_id, String
8
+
9
+ # Document thumbnail as defined by sender
10
+ attribute :thumb, PhotoSize
11
+
12
+ # Optional. Original filename as defined by sender
13
+ attribute :file_name, String
14
+
15
+ # Optional. MIME type of the file as defined by sender
16
+ attribute :mime_type, String
17
+
18
+ # Optional. File size
19
+ attribute :file_size, Integer
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module Telebot
2
+ # This object represents a group chat.
3
+ class GroupChat
4
+ include Virtus.model
5
+
6
+ # Unique identifier for this group chat
7
+ attribute :id, Integer
8
+
9
+ # Group name
10
+ attribute :title, String
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Telebot
2
+ # Represents file that has to be sent.
3
+ class InputFile < Faraday::UploadIO
4
+ DEFAULT_MIME_TYPE = "application/octet-stream"
5
+
6
+ # @param file_path [String]
7
+ # @param mime_type [String]
8
+ def initialize(file_path, mime_type = DEFAULT_MIME_TYPE)
9
+ super(file_path, mime_type)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Telebot
2
+ # This object represents a point on the map.
3
+ class Location
4
+ include Virtus.model
5
+
6
+ # Longitude as defined by sender
7
+ attribute :longitude, Float
8
+
9
+ # Latitude as defined by sender
10
+ attribute :latitude, Float
11
+ end
12
+ end
@@ -0,0 +1,76 @@
1
+ module Telebot
2
+ # This object represents a message.
3
+ class Message
4
+ include Virtus.model
5
+
6
+ # Unique message identifier
7
+ attribute :message_id, Integer
8
+
9
+ # Sender
10
+ attribute :from, User
11
+
12
+ # Date the message was sent in Unix time
13
+ #
14
+ # TODO: should be Time instead?
15
+ attribute :date, Integer
16
+
17
+ # Conversation the message belongs to — user in case of a private message, GroupChat in case of a group
18
+ # TODO:
19
+ # Handle GroupChat as well.
20
+ # How to distinguish them?
21
+ attribute :chat, User
22
+
23
+ # Optional. For forwarded messages, sender of the original message
24
+ attribute :forward_from, User
25
+
26
+ # Optional. For forwarded messages, date the original message was sent in Unix time
27
+ #
28
+ # TODO: should be Time instead?
29
+ attribute :forward_date, Integer
30
+
31
+ # Optional. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply.
32
+ attribute :reply_to_message, Message
33
+
34
+ # Optional. For text messages, the actual UTF-8 text of the message
35
+ attribute :text, String
36
+
37
+ # Optional. Message is an audio file, information about the file
38
+ attribute :audio, Audio
39
+
40
+ # Optional. Message is a general file, information about the file
41
+ attribute :document, Document
42
+
43
+ # Optional. Message is a photo, available sizes of the photo
44
+ attribute :photo, [PhotoSize]
45
+
46
+ # Optional. Message is a sticker, information about the sticker
47
+ attribute :sticker, Sticker
48
+
49
+ # Optional. Message is a video, information about the video
50
+ attribute :video, Video
51
+
52
+ # Optional. Message is a shared contact, information about the contact
53
+ attribute :contact, Contact
54
+
55
+ # Optional. Message is a shared location, information about the location
56
+ attribute :location, Location
57
+
58
+ # Optional. A new member was added to the group, information about them (this member may be bot itself)
59
+ attribute :new_chat_participant, User
60
+
61
+ # Optional. A member was removed from the group, information about them (this member may be bot itself)
62
+ attribute :left_chat_participant, User
63
+
64
+ # Optional. A group title was changed to this value
65
+ attribute :new_chat_title, String
66
+
67
+ # Optional. A group photo was change to this value
68
+ attribute :new_chat_photo, [PhotoSize]
69
+
70
+ # Optional. Informs that the group photo was deleted
71
+ attribute :delete_chat_photo, Boolean
72
+
73
+ # Optional. Informs that the group has been created
74
+ attribute :group_chat_created, Boolean
75
+ end
76
+ end
@@ -0,0 +1,19 @@
1
+ module Telebot
2
+ # This object represents one size of a photo or a file / sticker thumbnail.
3
+ # A missing thumbnail for a file (or sticker) is presented as an empty object.
4
+ class PhotoSize
5
+ include Virtus.model
6
+
7
+ # Unique identifier for this file
8
+ attribute :file_id, Integer
9
+
10
+ # Photo width
11
+ attribute :width, Integer
12
+
13
+ # Photo height
14
+ attribute :height, Integer
15
+
16
+ # Optional. File size
17
+ attribute :file_size, Integer
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module Telebot
2
+ class ReplyKeyboardMarkup
3
+ include Virtus.model
4
+
5
+ # Array of button rows, each represented by an Array of Strings.
6
+ #
7
+ attribute :keyboard
8
+
9
+ # @return [String] json that repsents keyboard markup
10
+ def to_json
11
+ to_hash.to_json
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module Telebot
2
+ # This object represents a sticker.
3
+ class Sticker
4
+ include Virtus.model
5
+
6
+ # Unique identifier for this file
7
+ attribute :file_id, Integer
8
+
9
+ # Sticker width
10
+ attribute :width, Integer
11
+
12
+ # Sticker height
13
+ attribute :height, Integer
14
+
15
+ # Sticker thumbnail in .webp or .jpg format
16
+ attribute :thumb, PhotoSize
17
+
18
+ # Optional. File size
19
+ attribute :file_size, Integer
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module Telebot
2
+ # This object represents an incoming update.
3
+ class Update
4
+ include Virtus.model
5
+
6
+ # The update‘s unique identifier. Update identifiers start from a certain
7
+ # positive number and increase sequentially. This ID becomes especially
8
+ # handy if you’re using Webhooks, since it allows you to ignore repeated
9
+ # updates or to restore the correct update sequence, should they get out
10
+ # of order.
11
+ attribute :update_id, Integer
12
+
13
+ # Optional.
14
+ # New incoming message of any kind — text, photo, sticker, etc.
15
+ attribute :message, Message
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module Telebot
2
+ # This object represents a Telegram user or bot.
3
+ class User
4
+ include Virtus.model
5
+
6
+ # Unique identifier for this user or bot
7
+ attribute :id, Integer
8
+
9
+ # User‘s or bot’s first name
10
+ attribute :first_name, String
11
+
12
+ # Optional. User‘s or bot’s last name
13
+ attribute :last_name, String
14
+
15
+ # Optional. User‘s or bot’s username
16
+ attribute :username, String
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Telebot
2
+ # This object represent a user's profile pictures.
3
+ class UserProfilePhotos
4
+ include Virtus.model
5
+
6
+ # Unique identifier for this user or bot
7
+ attribute :total_count, Integer
8
+
9
+ # Requested profile pictures (in up to 4 sizes each)
10
+ attr_reader :photos
11
+
12
+ # Assign Array of Array of PhotoSize
13
+ #
14
+ # @param values [Array<Array<PhotoSize>>]
15
+ def photos=(values)
16
+ @photos = values.map do |photo|
17
+ photo.map do |photo_size_attrs|
18
+ PhotoSize.new(photo_size_attrs)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ module Telebot
2
+ # This object represents a video file.
3
+ class Video
4
+ include Virtus.model
5
+
6
+ # Unique identifier for this file
7
+ attribute :file_id, Integer
8
+
9
+ # Video width as defined by sender
10
+ attribute :width, Integer
11
+
12
+ # Video height as defined by sender
13
+ attribute :height, Integer
14
+
15
+ # Duration of the video in seconds as defined by sender
16
+ attribute :duration, Integer
17
+
18
+ # Video thumbnail
19
+ attribute :thumb, PhotoSize
20
+
21
+ # Optional. Mime type of a file as defined by sender
22
+ attribute :mime_type, String
23
+
24
+ # Optional. File size
25
+ attribute :file_size, Integer
26
+
27
+ # Optional. Text description of the video (usually empty)
28
+ attribute :caption, String
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ module Telebot
2
+ class Response
3
+ include Virtus.model
4
+
5
+ # Whether is successful or failed
6
+ attribute :ok, Boolean
7
+
8
+ # Optional. Human-readable description of the result
9
+ attribute :description, String
10
+
11
+ # Result of request
12
+ attr_accessor :result
13
+
14
+ # An Integer ‘error_code’ field is also returned,
15
+ # but its contents are subject to change in the future.
16
+ attribute :error_code, Integer
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Telebot
2
+ VERSION = "0.1.0"
3
+ end
Binary file
@@ -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 'telebot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "telebot"
8
+ spec.version = Telebot::VERSION
9
+ spec.authors = ["Potapov Sergey"]
10
+ spec.email = ["blake131313@gmail.com"]
11
+
12
+ spec.summary = "Ruby client for Telegram bot API"
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/greyblake/telebot"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "faraday"
21
+ spec.add_dependency "faraday_middleware"
22
+ spec.add_dependency "virtus"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.1"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telebot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Potapov Sergey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: virtus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruby client for Telegram bot API
98
+ email:
99
+ - blake131313@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - Guardfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - examples/demo.rb
118
+ - examples/fixtures/bender_pic.jpg
119
+ - examples/fixtures/saluton_amiko.ogg
120
+ - examples/fixtures/zamenhof_sticker.webp
121
+ - lib/telebot.rb
122
+ - lib/telebot/bot.rb
123
+ - lib/telebot/client.rb
124
+ - lib/telebot/errors.rb
125
+ - lib/telebot/objects/audio.rb
126
+ - lib/telebot/objects/contact.rb
127
+ - lib/telebot/objects/document.rb
128
+ - lib/telebot/objects/group_chat.rb
129
+ - lib/telebot/objects/input_file.rb
130
+ - lib/telebot/objects/location.rb
131
+ - lib/telebot/objects/message.rb
132
+ - lib/telebot/objects/photo_size.rb
133
+ - lib/telebot/objects/reply_keyboard_markup.rb
134
+ - lib/telebot/objects/sticker.rb
135
+ - lib/telebot/objects/update.rb
136
+ - lib/telebot/objects/user.rb
137
+ - lib/telebot/objects/user_profile_photos.rb
138
+ - lib/telebot/objects/video.rb
139
+ - lib/telebot/response.rb
140
+ - lib/telebot/version.rb
141
+ - saluton_amiko.wav
142
+ - telebot.gemspec
143
+ homepage: https://github.com/greyblake/telebot
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.4.5
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Ruby client for Telegram bot API
167
+ test_files: []