telegrammer 0.0.3 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3618d92e021d648b116dc847bc1d92abe78fb07
4
- data.tar.gz: eb1ac18503e71ba969677914c5a55a5f77ff79df
3
+ metadata.gz: 01192345cd31357f67504923fbd3cb1933e99b4e
4
+ data.tar.gz: a06447a0d0bde6d7d0f9cf03a09e0b51faba93a4
5
5
  SHA512:
6
- metadata.gz: 5cb7e077fa9cc0c2ed3822de1ccbfd4685ed014db036fec6d7828363f0678d1cb8b05cde5a9c167c9c103a22f3d540c3a209b149b5d8b5b6ce4b94ebbe9e48fc
7
- data.tar.gz: dc30eebd1849f0daeec50783653d1ddb731885f0fa43081deac9f28580829067f8f9ac06b085ac9f26a2c6d4c71af7d0a4e615ad0212dffc5bd92834e03d062f
6
+ metadata.gz: d8d582a1eb2c96034917e98133a7b8eae71ef67dc6e63582fdaefbd5500cf9b1637fd097760f5b2907b0b0a566f11484decad6f9e801d7710dd5929ac607445a
7
+ data.tar.gz: d3e478e64f1bca7af8eae29785abcb9fe04df2dd8f482e6f1e03e11a20b0b3c74663d63e87f378939a033cc0c3bc50a0155219e1fc1ff3b21d618c67ae53b475
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ v0.1.0
2
+ ======
3
+
4
+ * Fixing coercion in Telegrammer::DataTypes::Channel.
5
+ * Fixed some Telegram data types.
6
+ * Better error handling when Telegram API servers are down.
7
+ * Improved documentation.
8
+
1
9
  v0.0.3
2
10
  ======
3
11
 
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/telegrammer.svg)](http://badge.fury.io/rb/telegrammer)
2
2
  [![Code Climate](https://codeclimate.com/github/mayoral/telegrammer/badges/gpa.svg)](https://codeclimate.com/github/mayoral/telegrammer)
3
+ [![Inline docs](http://inch-ci.org/github/mayoral/telegrammer.svg?branch=master)](http://inch-ci.org/github/mayoral/telegrammer)
3
4
 
4
5
  # Telegrammer
5
6
 
@@ -22,15 +23,13 @@ And then execute:
22
23
  First you'll need to register your bot and get an API token. To do that you have to talk with the [@BotFather](https://telegram.me/botfather).
23
24
  Learn more about this [here](https://core.telegram.org/bots).
24
25
 
25
- All current actions are supported by the gem. Here's an example for each action:
26
+ After getting your token, you can do things like this:
26
27
 
27
28
  ```ruby
28
29
  require 'telegrammer'
29
30
 
30
31
  bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
31
32
 
32
- # GET UPDATES
33
- # https://core.telegram.org/bots/api/#getupdates
34
33
  bot.get_updates do |message|
35
34
  puts "In chat #{message.chat.id}, @#{message.from.username} said: #{message.text}"
36
35
  bot.send_message(chat_id: message.chat.id, text: "You said: #{message.text}")
@@ -38,90 +37,13 @@ bot.get_updates do |message|
38
37
  # Here you can also process message text to detect user commands
39
38
  # To learn more about commands, see https://core.telegram.org/bots#commands
40
39
  end
41
-
42
- # SEND MESSAGES
43
- # https://core.telegram.org/bots/api/#sendmessage
44
- bot.send_message(chat_id: 123456789, text: "This is a text")
45
-
46
- # https://core.telegram.org/bots/api/#replykeyboardmarkup
47
- reply_markup = Telegrammer::DataTypes::ReplyKeyboardMarkup.new(
48
- keyboard: [
49
- ["Option 1.1", "Option 1.2"],
50
- ["Option 2"],
51
- ["Option 3.1", "Option 3.2", "Option 3.3"]
52
- ],
53
- resize_keyboard: false
54
- )
55
-
56
- # This message will activate a custom keyboard...
57
- bot.send_message(
58
- chat_id: 1460713,
59
- text: "Select an option",
60
- reply_markup: reply_markup
61
- )
62
-
63
- # https://core.telegram.org/bots/api/#replykeyboardhide
64
- reply_markup = Telegrammer::DataTypes::ReplyKeyboardHide.new(
65
- hide_keyboard: true
66
- )
67
-
68
- # And this message will disable it
69
- bot.send_message(
70
- chat_id: 1460713,
71
- text: "Thanks",
72
- reply_markup: reply_markup
73
- )
74
-
75
- # FORWARDING A MESSAGE
76
- # https://core.telegram.org/bots/api/#forwardmessage
77
- bot.forward_message(chat_id: 123456789, from_chat_id: 987654321, message_id: 111222333)
78
-
79
- # SENDING PHOTOS
80
- # https://core.telegram.org/bots/api/#sendphoto
81
- image_file = File.open("foo.jpg")
82
- bot.send_document(chat_id: 123456789, document: image_file)
83
-
84
- # SENDING AUDIO FILES
85
- # https://core.telegram.org/bots/api/#sendaudio
86
- audio_file = File.open("foo.ogg")
87
- bot.send_audio(chat_id: 123456789, audio: audio_file)
88
-
89
- # SENDING DOCUMENTS
90
- # https://core.telegram.org/bots/api/#senddocument
91
- my_secret_file = File.open("secret.txt")
92
- bot.send_document(chat_id: 123456789, document: my_secret_file)
93
-
94
- # SENDING STICKERS
95
- # https://core.telegram.org/bots/api/#sendsticker
96
- sticker_image_file = File.open("sticker.jpg")
97
- bot.send_sticker(chat_id: 123456789, sticker: sticker_image_file)
98
-
99
- # SENDING VIDEOS
100
- # https://core.telegram.org/bots/api/#sendvideo
101
- video_file = File.open("foo.mp4")
102
- bot.send_audio(chat_id: 123456789, video: video_file)
103
-
104
- # SENDING DOCUMENTS
105
- # https://core.telegram.org/bots/api/#senddocument
106
- file = File.open("foo.ogg")
107
- bot.send_audio(chat_id: 123456789, audio: file)
108
-
109
- # SENDING LOCATIONS
110
- # https://core.telegram.org/bots/api/#sendlocation
111
- bot.send_location(chat_id: 123456789, latitude: 38.775539, longitude: -4.829988)
112
-
113
- # SENDING CHAT ACTIONS
114
- # https://core.telegram.org/bots/api/#sendchataction
115
- bot.send_chat_action(chat_id: 123456789, action: "typing")
116
-
117
- # GETTING USER PROFILE PHOTOS
118
- # https://core.telegram.org/bots/api/#getuserprofilephotos
119
- bot.get_user_profile_photos(user_id: 123456789)
120
40
  ```
121
41
 
42
+ This gem currently supports all operations exposed by the Telegram's Bot API. All the classes and methods are documented with examples, so please [check the documentation](http://www.rubydoc.info/gems/telegrammer/Telegrammer/Bot). Also you can [check the the wiki](https://github.com/mayoral/telegrammer/wiki), where you'll find more info about topics like [setting webhooks for your bot](https://github.com/mayoral/telegrammer/wiki/Using-webhooks).
43
+
122
44
  ## Contributing
123
45
 
124
- 1. Fork it ( https://github.com/mayoral/telegrammer/fork )
46
+ 1. Fork it: https://github.com/mayoral/telegrammer/fork
125
47
  2. Create your feature branch (`git checkout -b my-new-feature`)
126
48
  3. Commit your changes (`git commit -am 'Add some feature'`)
127
49
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/telegrammer.rb CHANGED
@@ -28,22 +28,32 @@ require "telegrammer/api_response"
28
28
 
29
29
  module Telegrammer
30
30
  module Errors
31
+ # Error returned when a required param is missing
31
32
  class MissingParamsError < StandardError
32
33
  def initialize(parameter, action)
33
34
  super("Missing parameter #{parameter} for action #{action}")
34
35
  end
35
36
  end
36
37
 
38
+ # Error returned when a param type is invalid
37
39
  class InvalidParamTypeError < StandardError
38
40
  def initialize(parameter, current_type, allowed_types)
39
41
  super("Invalid parameter type: #{parameter}: #{current_type}. Allowed types: #{allowed_types.each {|type| type.class.to_s }.join(",")}.")
40
42
  end
41
43
  end
42
44
 
45
+ # Error returned when something goes bad with your request to the Telegram API
43
46
  class BadRequestError < StandardError
44
47
  def initialize(error_code, message)
45
48
  super("Bad request. Error code: #{error_code} - Message: #{message}")
46
49
  end
47
50
  end
51
+
52
+ # Error returned when Telegram API Service is unavailable
53
+ class ServiceUnavailableError < StandardError
54
+ def initialize(status_code)
55
+ super("Telegram API Service unavailable (HTTP error code #{status_code})")
56
+ end
57
+ end
48
58
  end
49
59
  end
@@ -5,15 +5,19 @@ module Telegrammer
5
5
  attr_reader :success
6
6
 
7
7
  def initialize(response)
8
- @body = response.response_body
9
- data = MultiJson.load(@body)
8
+ if response.code < 500
9
+ @body = response.response_body
10
10
 
11
- @success = data["ok"]
11
+ data = MultiJson.load(@body)
12
+ @success = data["ok"]
12
13
 
13
- if @success
14
- @result = data["result"]
14
+ if @success
15
+ @result = data["result"]
16
+ else
17
+ raise Telegrammer::Errors::BadRequestError.new(data["error_code"], data["description"])
18
+ end
15
19
  else
16
- raise Telegrammer::Errors::BadRequestError.new(data["error_code"], data["description"])
20
+ raise Telegrammer::Errors::ServiceUnavailableError.new(response.code)
17
21
  end
18
22
  end
19
23
 
@@ -1,9 +1,19 @@
1
1
  module Telegrammer
2
+ # Wrapper for the Telegram's Bots API
2
3
  class Bot
3
4
  API_ENDPOINT = 'https://api.telegram.org'
4
5
 
5
6
  attr_reader :me
6
7
 
8
+ # Returns a new instance of Telegrammer::Bot
9
+ #
10
+ # @param [String] api_token API Token
11
+ #
12
+ # @example
13
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
14
+ #
15
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong obtaining data about your bot
16
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
7
17
  def initialize(api_token)
8
18
  @api_token = api_token
9
19
  @offset = 0
@@ -12,6 +22,21 @@ module Telegrammer
12
22
  @me = get_me
13
23
  end
14
24
 
25
+ # Get incoming updates using long polling
26
+ #
27
+ # @example
28
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
29
+ #
30
+ # bot.get_updates do |message|
31
+ # puts "In chat #{message.chat.id}, @#{message.from.username} said: #{message.text}"
32
+ # bot.send_message(chat_id: message.chat.id, text: "You said: #{message.text}")
33
+ #
34
+ # # Here you can also process message text to detect user commands
35
+ # # To learn more about commands, see https://core.telegram.org/bots#commands
36
+ # end
37
+ #
38
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
39
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
15
40
  def get_updates(&block)
16
41
  loop do
17
42
  response = api_request("getUpdates", {offset: @offset, timeout: @timeout}, nil)
@@ -24,19 +49,65 @@ module Telegrammer
24
49
  end
25
50
  end
26
51
 
52
+ # Set a webhook where Telegram will send the messages received by your bot.
53
+ #
54
+ # @param [String] url
55
+ #
56
+ # @example
57
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
58
+ #
59
+ # # Set up a webhook
60
+ # bot.set_webhook("http://www.example.com/my/action")
61
+ #
62
+ # # Delete a webhook
63
+ # bot.set_webhook("")
64
+ #
65
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
66
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
67
+ #
68
+ # @see https://github.com/mayoral/telegrammer/wiki/Using-webhooks
69
+ # @return [Telegrammer::ApiResponse] Response from the API.
27
70
  def set_webhook(url)
28
- response = api_request("setWebhook", {url: url}, nil)
29
-
30
- Telegrammer::DataTypes::Update.new(response.result)
71
+ api_request("setWebhook", {url: url}, nil)
31
72
  end
32
73
 
33
-
74
+ # Returns basic information about the bot in form of a User object. Used for testing your bot's auth token.
75
+ #
76
+ # @example
77
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
78
+ # bot_user = bot.get_me
79
+ #
80
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
81
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
82
+ #
83
+ # @return [Telegrammer::DataTypes::User] User object with info about the bot
34
84
  def get_me
35
85
  response = api_request("getMe", nil, nil)
36
86
 
37
87
  Telegrammer::DataTypes::User.new(response.result)
38
88
  end
39
89
 
90
+ # Send text messages to a user or group chat.
91
+ #
92
+ # @param [Hash] params hash of paramers to send to the sendMessage API operation.
93
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
94
+ # @option params [String] :text Required. Text of the message to be sent
95
+ # @option params [Boolean] :disable_web_page_preview Optional. Disables link previews for links in this message
96
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message
97
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
98
+ #
99
+ # @example
100
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
101
+ #
102
+ # bot.send_message(
103
+ # chat_id: 123456789,
104
+ # text: "Hello World!"
105
+ # )
106
+ #
107
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
108
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
109
+ #
110
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
40
111
  def send_message(params)
41
112
  params_validation = {
42
113
  chat_id: { required: true, class: [Fixnum] },
@@ -55,6 +126,26 @@ module Telegrammer
55
126
  Telegrammer::DataTypes::Message.new(response.result)
56
127
  end
57
128
 
129
+ # Forward message to a user or group chat.
130
+ #
131
+ # @param [Hash] params hash of paramers to send to the forwardMessage API operation.
132
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
133
+ # @option params [Integer] :from_chat_id Required. Unique identifier for the chat where the original message was sent.
134
+ # @option params [Integer] :message_id Required. Message id to be forwarded.
135
+ #
136
+ # @example
137
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
138
+ #
139
+ # bot.forward_message(
140
+ # chat_id: 123456789,
141
+ # from_chat_id: 987654321
142
+ # message_id: 111222333
143
+ # )
144
+ #
145
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
146
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
147
+ #
148
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
58
149
  def forward_message(params)
59
150
  params_validation = {
60
151
  chat_id: { required: true, class: [Fixnum] },
@@ -67,6 +158,25 @@ module Telegrammer
67
158
  Telegrammer::DataTypes::Message.new(response.result)
68
159
  end
69
160
 
161
+ # Sends a photo to a user or group chat.
162
+ #
163
+ # @param [Hash] params hash of paramers to send to the sendPhoto API operation.
164
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
165
+ # @option params [File,String] :photo Required.
166
+ # @option params [String] :caption Optional.
167
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message
168
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
169
+ #
170
+ # @example
171
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
172
+ # image_file = File.open("foo.jpg")
173
+ # bot.send_photo(chat_id: 123456789, photo: image_file)
174
+ #
175
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
176
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
177
+ #
178
+ # @see #send_document
179
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
70
180
  def send_photo(params)
71
181
  params_validation = {
72
182
  chat_id: { required: true, class: [Fixnum] },
@@ -85,6 +195,26 @@ module Telegrammer
85
195
  Telegrammer::DataTypes::Message.new(response.result)
86
196
  end
87
197
 
198
+ # Sends audio file to a user or group chat.
199
+ #
200
+ # At this moment, Telegram only allows Ogg files encoded with the OPUS codec. If you need to send another file format, you must use #send_document.
201
+ #
202
+ # @param [Hash] params hash of paramers to send to the sendAudio API operation.
203
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
204
+ # @option params [File,String] audio Required. Audio file to send. You can either pass a file_id as String to resend an audio that is already on the Telegram servers, or upload a new audio file using multipart/form-data
205
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message
206
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
207
+ #
208
+ # @example
209
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
210
+ # audio_file = File.open("foo.ogg")
211
+ # bot.send_audio(chat_id: 123456789, audio: audio_file)
212
+ #
213
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
214
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
215
+ #
216
+ # @see #send_document
217
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
88
218
  def send_audio(params)
89
219
  params_validation = {
90
220
  chat_id: { required: true, class: [Fixnum] },
@@ -102,10 +232,28 @@ module Telegrammer
102
232
  Telegrammer::DataTypes::Message.new(response.result)
103
233
  end
104
234
 
235
+ # Sends a document to a user or group chat.
236
+ #
237
+ # @param [Hash] params hash of paramers to send to the sendDocument API operation.
238
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
239
+ # @option params [File,String] :document Required. File to send. You can either pass a file_id as String to resend a file that is already on the Telegram servers, or upload a new file using multipart/form-data.
240
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message
241
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
242
+ #
243
+ # @example
244
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
245
+ # my_secret_file = File.open("secrets.doc")
246
+ # bot.send_document(chat_id: 123456789, document: my_secret_file)
247
+ #
248
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
249
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
250
+ #
251
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
105
252
  def send_document(params)
106
253
  params_validation = {
107
254
  chat_id: { required: true, class: [Fixnum] },
108
255
  document: { required: false, class: [File, String] },
256
+ reply_to_message_id: { required: false, class: [String] },
109
257
  reply_markup: { required: false, class: [
110
258
  Telegrammer::DataTypes::ReplyKeyboardMarkup,
111
259
  Telegrammer::DataTypes::ReplyKeyboardHide,
@@ -118,10 +266,29 @@ module Telegrammer
118
266
  Telegrammer::DataTypes::Message.new(response.result)
119
267
  end
120
268
 
269
+ # Send WebP images as stickers.
270
+ #
271
+ # @param [Hash] params hash of paramers to send to the sendSticker API operation.
272
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
273
+ # @option params [File,String] :sticker Required. Sticker to send. You can either pass a file_id as String to resend a file that is already on the Telegram servers, or upload a new file using multipart/form-data.
274
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message
275
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
276
+ #
277
+ # @example
278
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
279
+ # sticker_file = File.open("my-sticker.webp")
280
+ # bot.send_sticker(chat_id: 123456789, sticker: sticker_file)
281
+ #
282
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
283
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
284
+ #
285
+ # @see #send_document
286
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
121
287
  def send_sticker(params)
122
288
  params_validation = {
123
289
  chat_id: { required: true, class: [Fixnum] },
124
290
  sticker: { required: true, class: [File, String] },
291
+ reply_to_message_id: { required: false, class: [String] },
125
292
  reply_markup: { required: false, class: [
126
293
  Telegrammer::DataTypes::ReplyKeyboardMarkup,
127
294
  Telegrammer::DataTypes::ReplyKeyboardHide,
@@ -134,10 +301,31 @@ module Telegrammer
134
301
  Telegrammer::DataTypes::Message.new(response.result)
135
302
  end
136
303
 
304
+ # Sends a video file to a user or group chat.
305
+ #
306
+ # At this moment, Telegram only support mp4 videos. If you need to send other formats you must use #send_document.
307
+ #
308
+ # @param [Hash] params hash of paramers to send to the sendVideo API operation.
309
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
310
+ # @option params [File,String] :video Video to send. You can either pass a file_id as String to resend a video that is already on the Telegram servers, or upload a new video file.
311
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message
312
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
313
+ #
314
+ # @example
315
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
316
+ # my_video = File.open("foo.mp4")
317
+ # bot.send_video(chat_id: 123456789, video: my_video)
318
+ #
319
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
320
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
321
+ #
322
+ # @see #send_document
323
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
137
324
  def send_video(params)
138
325
  params_validation = {
139
326
  chat_id: { required: true, class: [Fixnum] },
140
327
  video: { required: true, class: [File, String] },
328
+ reply_to_message_id: { required: false, class: [String] },
141
329
  reply_markup: { required: false, class: [
142
330
  Telegrammer::DataTypes::ReplyKeyboardMarkup,
143
331
  Telegrammer::DataTypes::ReplyKeyboardHide,
@@ -150,11 +338,29 @@ module Telegrammer
150
338
  Telegrammer::DataTypes::Message.new(response.result)
151
339
  end
152
340
 
341
+ # Sends point on the map to a user or group chat.
342
+ #
343
+ # @param [Hash] params hash of paramers to send to the sendAudio API operation.
344
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
345
+ # @option params [Float] :latitude Required. Latitude of location.
346
+ # @option params [Float] :longitude Required. Longitude of location.
347
+ # @option params [Integer] :reply_to_message_id Optional. If the message is a reply, ID of the original message.
348
+ # @option params [Telegrammer::DataTypes::ReplyKeyboardMarkup,Telegrammer::DataTypes::ReplyKeyboardHide,Telegrammer::DataTypes::ForceReply] :reply_markup Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.
349
+ #
350
+ # @example
351
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
352
+ # bot.send_location(chat_id: 123456789, latitude: 38.775539, longitude: -4.829988)
353
+ #
354
+ # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
355
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
356
+ #
357
+ # @return [Telegrammer::DataTypes::Message] Message object sent to the user or group chat
153
358
  def send_location(params)
154
359
  params_validation = {
155
360
  chat_id: { required: true, class: [Fixnum] },
156
361
  latitude: { required: true, class: [Float] },
157
362
  longitude: { required: true, class: [Float] },
363
+ reply_to_message_id: { required: false, class: [String] },
158
364
  reply_markup: { required: false, class: [
159
365
  Telegrammer::DataTypes::ReplyKeyboardMarkup,
160
366
  Telegrammer::DataTypes::ReplyKeyboardHide,
@@ -167,18 +373,46 @@ module Telegrammer
167
373
  Telegrammer::DataTypes::Message.new(response.result)
168
374
  end
169
375
 
170
- # bot.send_chat_action(chat_id: "1460713", action: "typing")
376
+ # Sends a status action to a user or group chat.
377
+ #
378
+ # Used when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
379
+ #
380
+ # @param [Hash] params hash of paramers to send to the sendChatAction API operation.
381
+ # @option params [Integer] :chat_id Required. Unique identifier for the message recipient — User or GroupChat id.
382
+ # @option params [String] :action Required. Type of action to broadcast. Choose one, depending on what the user is about to receive: "typing" for text messages, "upload_photo" for photos, "record_video" or "upload_video" for videos, "record_audio" or "upload_audio" for audio files, "upload_document" for general files, "find_location" for location data.
383
+ #
384
+ # @example
385
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
386
+ # bot.send_chat_action(chat_id: 123456789, action: "typing")
387
+ #
388
+ # @raise [Telegrammer::Errors::BadRequestError]
389
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
390
+ #
391
+ # @return [Telegrammer::ApiResponse] Response from the API.
171
392
  def send_chat_action(params)
172
393
  params_validation = {
173
394
  chat_id: { required: true, class: [Fixnum] },
174
395
  action: { required: true, class: [String] }
175
396
  }
176
397
 
177
- response = api_request("sendChatAction", params, params_validation)
178
-
179
- response.result
398
+ api_request("sendChatAction", params, params_validation)
180
399
  end
181
400
 
401
+ # Get a list of profile pictures for a user.
402
+ #
403
+ # @param [Hash] params hash of paramers to send to the sendChatAction API operation.
404
+ # @option params [Integer] :chat_id Required. Unique identifier of the target user.
405
+ # @option params [Integer] :offset Optional. Sequential number of the first photo to be returned. By default, all photos are returned.
406
+ # @option params [Integer] :limit Optional. Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100.
407
+ #
408
+ # @example
409
+ # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
410
+ # bot.get_user_profile_photos(user_id: 123456789)
411
+ #
412
+ # @raise [Telegrammer::Errors::BadRequestError]
413
+ # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
414
+ #
415
+ # @return [Telegrammer::DataTypes::UserProfilePhotos] Message object sent to the user or group chat
182
416
  def get_user_profile_photos(params)
183
417
  params_validation = {
184
418
  user_id: { required: true, class: [Fixnum] },
@@ -1,6 +1,15 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Audio data type
4
+ #
5
+ # @attr [String] file_id Unique identifier for this file
6
+ # @attr [Integer] duration Duration of the audio in seconds as defined by sender
7
+ # @attr [String] mime_type MIME type of the file as defined by sender
8
+ # @attr [Integer] file_size File size
9
+ #
10
+ # See more at https://core.telegram.org/bots/api#audio
3
11
  class Audio < Telegrammer::DataTypes::Base
12
+ # Unique identifier
4
13
  attribute :file_id, String
5
14
  attribute :duration, Integer
6
15
  attribute :mime_type, String
@@ -1,8 +1,8 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
- class Channel < Telegrammer::DataTypes::Base
4
- attribute :id, Integer
5
-
3
+ # Custom attribute class to handle chat attribute in Telegrammer::DataTypes::Message
4
+ class Channel < Virtus::Attribute
5
+ # Transforms a channel into a User object or GroupChat object
6
6
  def coerce(value)
7
7
  value.respond_to?(:first_name) ? User.new(value) : GroupChat.new(value)
8
8
  end
@@ -1,5 +1,13 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Contact data type
4
+ #
5
+ # @attr [String] phone_number Contact's phone number
6
+ # @attr [String] first_name Contact's first name
7
+ # @attr [String] last_name Optional. Contact's last name
8
+ # @attr [String] user_id Optional. Contact's user identifier in Telegram
9
+ #
10
+ # See more at https://core.telegram.org/bots/api#contact
3
11
  class Contact < Telegrammer::DataTypes::Base
4
12
  attribute :phone_number, String
5
13
  attribute :first_name, String
@@ -1,5 +1,14 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Document data type
4
+ #
5
+ # @attr [String] file_id Unique file identifier
6
+ # @attr [Telegrammer::DataTypes::PhotoSize] thumb Document thumbnail as defined by sender
7
+ # @attr [String] file_name Optional. Original filename as defined by sender
8
+ # @attr [String] mime_type Optional. MIME type of the file as defined by sender
9
+ # @attr [String] file_size Optional. File size
10
+ #
11
+ # See more at https://core.telegram.org/bots/api#document
3
12
  class Document < Telegrammer::DataTypes::Base
4
13
  attribute :file_id, String
5
14
  attribute :thumb, PhotoSize
@@ -1,5 +1,11 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram ForceReply data type
4
+ #
5
+ # @attr [Boolean] force_reply Shows reply interface to the user, as if they manually selected the bot‘s message and tapped ’Reply'
6
+ # @attr [Boolean] selective Optional. Use this parameter if you want to force reply from specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
7
+ #
8
+ # See more at https://core.telegram.org/bots/api#forcereply
3
9
  class ForceReply < Telegrammer::DataTypes::Base
4
10
  attribute :force_reply, Boolean, default: true
5
11
  attribute :selective, Boolean, default: false
@@ -1,5 +1,11 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram GroupChat data type
4
+ #
5
+ # @attr [Integer] id Unique identifier for this group chat
6
+ # @attr [String] title Group name
7
+ #
8
+ # See more at https://core.telegram.org/bots/api#groupchat
3
9
  class GroupChat < Telegrammer::DataTypes::Base
4
10
  attribute :id, Integer
5
11
  attribute :title, String
@@ -1,8 +1,14 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Location data type
4
+ #
5
+ # @attr [Float] latitude Latitude as defined by sender
6
+ # @attr [Float] longitude Longitude as defined by sender
7
+ #
8
+ # See more at https://core.telegram.org/bots/api#location
3
9
  class Location < Telegrammer::DataTypes::Base
4
- attribute :longitude, Float
5
10
  attribute :latitude, Float
11
+ attribute :longitude, Float
6
12
  end
7
13
  end
8
14
  end
@@ -1,12 +1,37 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Message data type
4
+ #
5
+ # @attr [Integer] message_id Unique message identifier
6
+ # @attr [Telegrammer::DataTypes::User] from Sender
7
+ # @attr [DateTime] date Date the message was sent
8
+ # @attr [Telegrammer::DataTypes::Channel] chat Conversation the message belongs to — user in case of a private message, GroupChat in case of a group
9
+ # @attr [Telegrammer::DataTypes::User] forward_from Optional. For forwarded messages, sender of the original message
10
+ # @attr [DateTime] forward_date Optional. For forwarded messages, date the original message was sent
11
+ # @attr [Telegrammer::DataTypes::Message] reply_to_message 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.
12
+ # @attr [String] text Optional. For text messages, the actual UTF-8 text of the message
13
+ # @attr [Telegrammer::DataTypes::Audio] audio Optional. Message is an audio file, information about the file
14
+ # @attr [Telegrammer::DataTypes::Document] document Optional. Message is a general file, information about the file
15
+ # @attr [Telegrammer::DataTypes::PhotoSize] photo Optional. Message is a photo, available sizes of the photo
16
+ # @attr [Telegrammer::DataTypes::Sticker] sticker Optional. Message is a sticker, information about the sticker
17
+ # @attr [Telegrammer::DataTypes::Video] video Optional. Message is a video, information about the video
18
+ # @attr [Telegrammer::DataTypes::Contact] contact Optional. Message is a shared contact, information about the contact
19
+ # @attr [Telegrammer::DataTypes::Location] location Optional. Message is a shared location, information about the location
20
+ # @attr [Telegrammer::DataTypes::User] new_chat_participant Optional. A new member was added to the group, information about them (this member may be bot itself)
21
+ # @attr [Telegrammer::DataTypes::User] left_chat_participant Optional. A member was removed from the group, information about them (this member may be bot itself)
22
+ # @attr [String] new_chat_title Optional. A group title was changed to this value
23
+ # @attr [Telegrammer::DataTypes::PhotoSize] new_chat_photo Optional. A group photo was change to this value
24
+ # @attr [Boolean] delete_chat_photo Optional. Informs that the group photo was deleted
25
+ # @attr [Boolean] group_chat_created Optional. Informs that the group has been created
26
+ #
27
+ # See more at https://core.telegram.org/bots/api#message
3
28
  class Message < Telegrammer::DataTypes::Base
4
29
  attribute :message_id, Integer
5
30
  attribute :from, User
6
31
  attribute :date, DateTime
7
32
  attribute :chat, Channel
8
33
  attribute :forward_from, User
9
- attribute :forward_date, Integer
34
+ attribute :forward_date, DateTime
10
35
  attribute :reply_to_message, Message
11
36
  attribute :text, String
12
37
  attribute :audio, Audio
@@ -19,7 +44,7 @@ module Telegrammer
19
44
  attribute :new_chat_participant, User
20
45
  attribute :left_chat_participant, User
21
46
  attribute :new_chat_title, String
22
- attribute :new_chat_photo, String
47
+ attribute :new_chat_photo, Array[PhotoSize]
23
48
  attribute :delete_chat_photo, Boolean
24
49
  attribute :group_chat_created, Boolean
25
50
  end
@@ -1,5 +1,13 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram PhotoSize data type
4
+ #
5
+ # @attr [String] file_id Unique identifier for this file
6
+ # @attr [Integer] width Photo width
7
+ # @attr [Integer] height Photo height
8
+ # @attr [Integer] file_size Optional. File size
9
+ #
10
+ # See more at https://core.telegram.org/bots/api#photosize
3
11
  class PhotoSize < Telegrammer::DataTypes::Base
4
12
  attribute :file_id, String
5
13
  attribute :width, Integer
@@ -1,5 +1,11 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram ReplyKeyboardHide data type
4
+ #
5
+ # @attr [Boolean] hide_keyboard Requests clients to hide the custom keyboard
6
+ # @attr [Boolean] selective Optional. Use this parameter if you want to hide keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
7
+ #
8
+ # See more at https://core.telegram.org/bots/api#replykeyboardhide
3
9
  class ReplyKeyboardHide < Telegrammer::DataTypes::Base
4
10
  attribute :hide_keyboard, Boolean, default: true
5
11
  attribute :selective, Boolean, default: false
@@ -1,5 +1,15 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram ReplyKeyboardMarkup data type.
4
+ #
5
+ # This object represents a custom keyboard with reply options (see Introduction to bots for details and examples).
6
+ #
7
+ # @attr [Array] keyboard Array of button rows, each represented by an Array of Strings
8
+ # @attr [Boolean] resize_keyboard Optional. Requests clients to resize the keyboard vertically for optimal fit. Defaults to false.
9
+ # @attr [Boolean] one_time_keyboard Optional. Requests clients to hide the keyboard as soon as it's been used. Defaults to false.
10
+ # @attr [Boolean] selective Optional. Use this parameter if you want to hide keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
11
+ #
12
+ # See more at https://core.telegram.org/bots/api#replykeyboardmarkup
3
13
  class ReplyKeyboardMarkup < Telegrammer::DataTypes::Base
4
14
  attribute :keyboard, Array[Array[String]]
5
15
  attribute :resize_keyboard, Boolean, default: false
@@ -1,5 +1,14 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Sticker data type.
4
+ #
5
+ # @attr [String] file_id Unique identifier for this file
6
+ # @attr [Integer] width Sticker width
7
+ # @attr [Integer] height Sticker height
8
+ # @attr [Telegrammer::DataTypes::PhotoSize] thumb Sticker thumbnail in .webp or .jpg format
9
+ # @attr [Integer] file_id Optional. File size
10
+ #
11
+ # See more at https://core.telegram.org/bots/api#sticker
3
12
  class Sticker < Telegrammer::DataTypes::Base
4
13
  attribute :file_id, String
5
14
  attribute :width, Integer
@@ -1,5 +1,11 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Update data type.
4
+ #
5
+ # @attr [Integer] update_id The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially.
6
+ # @attr [Telegrammer::DataTypes::Message] message Optional. New incoming message of any kind - text, photo, sticker, etc.
7
+ #
8
+ # See more at https://core.telegram.org/bots/api#update
3
9
  class Update < Telegrammer::DataTypes::Base
4
10
  attribute :update_id, Integer
5
11
  attribute :message, Message
@@ -1,5 +1,13 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram User data type.
4
+ #
5
+ # @attr [Integer] id Unique identifier for this user or bot
6
+ # @attr [String] first_name User‘s or bot’s first name
7
+ # @attr [String] last_name Optional. User‘s or bot’s last name
8
+ # @attr [String] username Optional. User‘s or bot’s username
9
+ #
10
+ # See more at https://core.telegram.org/bots/api#user
3
11
  class User < Telegrammer::DataTypes::Base
4
12
  attribute :id, Integer
5
13
  attribute :first_name, String
@@ -1,9 +1,13 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram UserProfilePhotos data type.
4
+ #
5
+ # @attr [Integer] total_count Total number of profile pictures the target user has
6
+ # @attr [Telegrammer::DataTypes::PhotoSize] photos Requested profile pictures (in up to 4 sizes each)
7
+ #
8
+ # See more at https://core.telegram.org/bots/api#userprofilephotos
3
9
  class UserProfilePhotos < Telegrammer::DataTypes::Base
4
- attribute :id, Integer
5
- attribute :first_name, String
6
- attribute :last_name, String
10
+ attribute :total_count, Integer
7
11
  attribute :photos, Array[PhotoSize]
8
12
  end
9
13
  end
@@ -1,5 +1,17 @@
1
1
  module Telegrammer
2
2
  module DataTypes
3
+ # Telegram Video data type
4
+ #
5
+ # @attr [String] file_id Unique file identifier
6
+ # @attr [Integer] width Video width as defined by sender
7
+ # @attr [Integer] height Video height as defined by sender
8
+ # @attr [Integer] duration Duration of the video in seconds as defined by sender
9
+ # @attr [Telegrammer::DataTypes::PhotoSize] thumb Video thumbnail
10
+ # @attr [String] mime_type Optional. Mime type of a file as defined by sender
11
+ # @attr [String] file_size Optional. File size
12
+ # @attr [String] caption Optional. Text description of the video (usually empty)
13
+ #
14
+ # See more at https://core.telegram.org/bots/api#video
3
15
  class Video < Telegrammer::DataTypes::Base
4
16
  attribute :file_id, String
5
17
  attribute :width, Integer
@@ -1,3 +1,3 @@
1
1
  module Telegrammer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegrammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luis Mayoral
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-26 00:00:00.000000000 Z
11
+ date: 2015-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus