Class: Telegrammer::Bot
- Inherits:
-
Object
- Object
- Telegrammer::Bot
- Defined in:
- lib/telegrammer/bot.rb
Overview
Wrapper for the Telegram’s Bots API
Constant Summary collapse
- API_ENDPOINT =
'https://api.telegram.org'
Instance Attribute Summary collapse
-
#me ⇒ Object
readonly
Returns the value of attribute me.
Instance Method Summary collapse
-
#forward_message(params) ⇒ Telegrammer::DataTypes::Message
Forward message to a user or group chat.
-
#get_file(params) ⇒ String
Get basic info about a file and prepare it for downloading.
-
#get_me ⇒ Telegrammer::DataTypes::User
Returns basic information about the bot in form of a User object.
-
#get_updates(opts = {}, &_block) ⇒ Object
Get incoming updates using long polling.
-
#get_user_profile_photos(params) ⇒ Telegrammer::DataTypes::UserProfilePhotos
Get a list of profile pictures for a user.
-
#initialize(api_token) ⇒ Bot
constructor
Returns a new instance of Telegrammer::Bot.
-
#send_audio(params) ⇒ Telegrammer::DataTypes::Message
Sends audio file to a user or group chat.
-
#send_chat_action(params) ⇒ Telegrammer::ApiResponse
Sends a status action to a user or group chat.
-
#send_document(params) ⇒ Telegrammer::DataTypes::Message
Sends a document to a user or group chat.
-
#send_location(params) ⇒ Telegrammer::DataTypes::Message
Sends point on the map to a user or group chat.
-
#send_message(params) ⇒ Telegrammer::DataTypes::Message
Send text messages to a user or group chat.
-
#send_photo(params) ⇒ Telegrammer::DataTypes::Message
Sends a photo to a user or group chat.
-
#send_sticker(params) ⇒ Telegrammer::DataTypes::Message
Send WebP images as stickers.
-
#send_video(params) ⇒ Telegrammer::DataTypes::Message
Sends a video file to a user or group chat.
-
#send_voice(params) ⇒ Telegrammer::DataTypes::Message
Sends audio files file to a user or group chat that the users will see as a playable voice message.
-
#set_webhook(url) ⇒ Telegrammer::ApiResponse
Set a webhook where Telegram will send the messages received by your bot.
Constructor Details
#initialize(api_token) ⇒ Bot
Returns a new instance of Telegrammer::Bot
17 18 19 20 21 22 23 24 25 |
# File 'lib/telegrammer/bot.rb', line 17 def initialize(api_token) @api_token = api_token @offset = 0 @timeout = 60 @fail_silently = false @connection = HTTPClient.new @me = get_me end |
Instance Attribute Details
#me ⇒ Object (readonly)
Returns the value of attribute me.
6 7 8 |
# File 'lib/telegrammer/bot.rb', line 6 def me @me end |
Instance Method Details
#forward_message(params) ⇒ Telegrammer::DataTypes::Message
Forward message to a user or group chat.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/telegrammer/bot.rb', line 162 def (params) params_validation = { chat_id: { required: true, class: [Fixnum] }, from_chat_id: { required: true, class: [String] }, message_id: { required: true, class: [Fixnum] } } response = api_request('forwardMessage', params, params_validation) Telegrammer::DataTypes::Message.new(response.result) end |
#get_file(params) ⇒ String
Get basic info about a file and prepare it for downloading.
441 442 443 444 445 446 447 448 449 450 |
# File 'lib/telegrammer/bot.rb', line 441 def get_file(params) params_validation = { file_id: { required: true, class: [String] } } response = response = api_request("getFile", params, params_validation) file_object = Telegrammer::DataTypes::File.new(response.result) "#{API_ENDPOINT}/file/bot#{@api_token}/#{file_object.file_path}" end |
#get_me ⇒ Telegrammer::DataTypes::User
Returns basic information about the bot in form of a User object. Used for testing your bot’s auth token.
104 105 106 107 108 |
# File 'lib/telegrammer/bot.rb', line 104 def get_me response = api_request('getMe', nil, nil) Telegrammer::DataTypes::User.new(response.result) end |
#get_updates(opts = {}, &_block) ⇒ Object
Get incoming updates using long polling
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/telegrammer/bot.rb', line 56 def get_updates(opts={}, &_block) loop do if opts[:fail_silently] @fail_silently = true end response = api_request('getUpdates', { offset: opts[:offset] || @offset, timeout: opts[:timeout] || @timeout }, nil) response.result.each do |raw_update| update = Telegrammer::DataTypes::Update.new(raw_update) @offset = update.update_id + 1 yield update. end end end |
#get_user_profile_photos(params) ⇒ Telegrammer::DataTypes::UserProfilePhotos
Get a list of profile pictures for a user.
416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/telegrammer/bot.rb', line 416 def get_user_profile_photos(params) params_validation = { user_id: { required: true, class: [Fixnum] }, offset: { required: false, class: [Fixnum] }, limit: { required: false, class: [Fixnum] } } response = api_request('getUserProfilePhotos', params, params_validation) Telegrammer::DataTypes::UserProfilePhotos.new(response.result).to_h end |
#send_audio(params) ⇒ Telegrammer::DataTypes::Message
Sends audio file to a user or group chat.
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.
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/telegrammer/bot.rb', line 225 def send_audio(params) extra_params_validation = { audio: { required: true, class: [File, String] }, duration: { required: false, class: [Integer] }, performer: { required: false, class: [String] }, title: { required: false, class: [String] } } send_something(:audio, params, extra_params_validation) end |
#send_chat_action(params) ⇒ Telegrammer::ApiResponse
Sends a status action to a user or group chat.
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).
392 393 394 395 396 397 398 399 |
# File 'lib/telegrammer/bot.rb', line 392 def send_chat_action(params) params_validation = { chat_id: { required: true, class: [Fixnum, String] }, action: { required: true, class: [String] } } api_request('sendChatAction', params, params_validation) end |
#send_document(params) ⇒ Telegrammer::DataTypes::Message
Sends a document to a user or group chat.
284 285 286 287 288 289 290 |
# File 'lib/telegrammer/bot.rb', line 284 def send_document(params) extra_params_validation = { document: { required: true, class: [File, String] } } send_something(:document, params, extra_params_validation) end |
#send_location(params) ⇒ Telegrammer::DataTypes::Message
Sends point on the map to a user or group chat.
367 368 369 370 371 372 373 374 |
# File 'lib/telegrammer/bot.rb', line 367 def send_location(params) extra_params_validation = { latitude: { required: true, class: [Float] }, longitude: { required: true, class: [Float] } } send_something(:location, params, extra_params_validation) end |
#send_message(params) ⇒ Telegrammer::DataTypes::Message
Send text messages to a user or group chat.
132 133 134 135 136 137 138 139 140 |
# File 'lib/telegrammer/bot.rb', line 132 def (params) extra_params_validation = { text: { required: true, class: [String] }, parse_mode: { required: false, class: [String] }, disable_web_page_preview: { required: false, class: [TrueClass, FalseClass] } } send_something(:message, params, extra_params_validation) end |
#send_photo(params) ⇒ Telegrammer::DataTypes::Message
Sends a photo to a user or group chat.
193 194 195 196 197 198 199 200 |
# File 'lib/telegrammer/bot.rb', line 193 def send_photo(params) extra_params_validation = { photo: { required: true, class: [File, String] }, caption: { required: false, class: [String] } } send_something(:photo, params, extra_params_validation) end |
#send_sticker(params) ⇒ Telegrammer::DataTypes::Message
Send WebP images as stickers.
310 311 312 313 314 315 316 |
# File 'lib/telegrammer/bot.rb', line 310 def send_sticker(params) extra_params_validation = { sticker: { required: true, class: [File, String] } } send_something(:sticker, params, extra_params_validation) end |
#send_video(params) ⇒ Telegrammer::DataTypes::Message
Sends a video file to a user or group chat.
At this moment, Telegram only support mp4 videos. If you need to send other formats you must use #send_document.
340 341 342 343 344 345 346 347 348 |
# File 'lib/telegrammer/bot.rb', line 340 def send_video(params) extra_params_validation = { video: { required: true, class: [File, String] }, duration: { required: false, class: [Integer] }, caption: { required: false, class: [String] } } send_something(:video, params, extra_params_validation) end |
#send_voice(params) ⇒ Telegrammer::DataTypes::Message
Sends audio files file to a user or group chat that the users will see as a playable voice message.
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.
257 258 259 260 261 262 263 264 |
# File 'lib/telegrammer/bot.rb', line 257 def send_voice(params) extra_params_validation = { voice: { required: true, class: [File, String] }, duration: { required: false, class: [Integer] } } send_something(:audio, params, extra_params_validation) end |
#set_webhook(url) ⇒ Telegrammer::ApiResponse
Set a webhook where Telegram will send the messages received by your bot.
90 91 92 |
# File 'lib/telegrammer/bot.rb', line 90 def set_webhook(url) api_request('setWebhook', { url: url }, nil) end |