Class: Telebot::Client
- Inherits:
-
Object
- Object
- Telebot::Client
- Defined in:
- lib/telebot/client.rb
Constant Summary collapse
- API_URL =
"https://api.telegram.org".freeze
- CHAT_ACTIONS =
Available chat actions
[:typing, :upload_photo, :record_video, :upload_video, :record_audio, :upload_audio, :upload_document, :find_location].freeze
Instance Method Summary collapse
-
#forward_message(chat_id:, from_chat_id:, message_id:) ⇒ Telebot::Message
Use this method to forward messages of any kind.
-
#get_me ⇒ Telebot::User
A simple method for testing your bot’s auth token.
-
#get_updates(offset: nil, limit: nil, timeout: nil) ⇒ Array<Telebot::Update>
Use this method to receive incoming updates using long polling.
-
#get_user_profile_photos(user_id:, offset: nil, limit: nil) ⇒ Telebot::UserProfilePhotos
Use this method to get a list of profile pictures for a user.
-
#initialize(token, adapter: :net_http) ⇒ Client
constructor
A new instance of Client.
-
#send_audio(chat_id:, audio:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.
-
#send_chat_action(chat_id:, action:) ⇒ void
Use this method when you need to tell the user that something is happening on the bot’s side.
-
#send_document(chat_id:, document:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send general file.
-
#send_location(chat_id:, latitude:, longitude:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send a point on the map.
-
#send_message(chat_id:, text:, disable_web_page_preview: false, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send text message.
-
#send_photo(chat_id:, photo:, caption: nil, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send a picture.
-
#send_sticker(chat_id:, sticker:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Use this method to send .webp stickers.
-
#send_video(chat_id:, video:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
-
#set_web_hook(url:) ⇒ void
Use this method to specify a url and receive incoming updates via an outgoing webhook.
Constructor Details
#initialize(token, adapter: :net_http) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/telebot/client.rb', line 8 def initialize(token, adapter: :net_http) fail(ArgumentError, "token can't be empty") if token.nil? || token.empty? @token = token @faraday = Faraday.new(API_URL) do |conn| conn.request :multipart conn.request :url_encoded conn.response :json, :content_type => /\bjson$/ conn.adapter adapter end end |
Instance Method Details
#forward_message(chat_id:, from_chat_id:, message_id:) ⇒ Telebot::Message
Use this method to forward messages of any kind.
82 83 84 85 |
# File 'lib/telebot/client.rb', line 82 def (chat_id:, from_chat_id:, message_id:) result = call(:forwardMessage, chat_id: chat_id, from_chat_id: from_chat_id, message_id: ) Message.new(result) end |
#get_me ⇒ Telebot::User
A simple method for testing your bot’s auth token. Requires no parameters. Returns basic information about the bot in form of a User object.
24 25 26 27 |
# File 'lib/telebot/client.rb', line 24 def get_me result = call(:getMe) User.new(result) end |
#get_updates(offset: nil, limit: nil, timeout: nil) ⇒ Array<Telebot::Update>
Use this method to receive incoming updates using long polling. An Array of Update objects is returned.
Note:
-
This method will not work if an outgoing webhook is set up.
-
In order to avoid getting duplicate updates, recalculate offset after each server response.
50 51 52 53 |
# File 'lib/telebot/client.rb', line 50 def get_updates(offset: nil, limit: nil, timeout: nil) result = call(:getUpdates, offset: offset, limit: limit, timeout: timeout) result.map { |update_hash| Update.new(update_hash) } end |
#get_user_profile_photos(user_id:, offset: nil, limit: nil) ⇒ Telebot::UserProfilePhotos
Use this method to get a list of profile pictures for a user.
195 196 197 198 |
# File 'lib/telebot/client.rb', line 195 def get_user_profile_photos(user_id:, offset: nil, limit: nil) result = call(:getUserProfilePhotos, user_id: user_id, offset: offset, limit: limit) UserProfilePhotos.new(result) end |
#send_audio(chat_id:, audio:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Document)
113 114 115 116 |
# File 'lib/telebot/client.rb', line 113 def send_audio(chat_id:, audio:, reply_to_message_id: nil, reply_markup: nil) result = call(:sendAudio, chat_id: chat_id, audio: audio, reply_to_message_id: , reply_markup: reply_markup) Message.new(result) end |
#send_chat_action(chat_id:, action:) ⇒ void
This method returns an undefined value.
Use this method 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).
183 184 185 186 |
# File 'lib/telebot/client.rb', line 183 def send_chat_action(chat_id:, action:) fail(ArgumentError, "Unknown chat action `#{action.inspect}`") unless CHAT_ACTIONS.include?(action) call(:sendChatAction, chat_id: chat_id, action: action) end |
#send_document(chat_id:, document:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send general file.
126 127 128 129 |
# File 'lib/telebot/client.rb', line 126 def send_document(chat_id:, document:, reply_to_message_id: nil, reply_markup: nil) result = call(:sendDocument, chat_id: chat_id, document: document, reply_to_message_id: , reply_markup: reply_markup) Message.new(result) end |
#send_location(chat_id:, latitude:, longitude:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send a point on the map.
166 167 168 169 170 171 172 173 |
# File 'lib/telebot/client.rb', line 166 def send_location(chat_id:, latitude:, longitude:, reply_to_message_id: nil, reply_markup: nil) result = call(:sendLocation, chat_id: chat_id, latitude: latitude, longitude: longitude, reply_to_message_id: , reply_markup: reply_markup) Message.new(result) end |
#send_message(chat_id:, text:, disable_web_page_preview: false, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send text message.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/telebot/client.rb', line 64 def (chat_id:, text:, disable_web_page_preview: false, reply_to_message_id: nil, reply_markup: nil) result = call(:sendMessage, chat_id: chat_id, text: text, disable_web_page_preview: disable_web_page_preview, reply_to_message_id: , reply_markup: reply_markup ) Message.new(result) end |
#send_photo(chat_id:, photo:, caption: nil, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send a picture.
98 99 100 101 |
# File 'lib/telebot/client.rb', line 98 def send_photo(chat_id:, photo:, caption: nil, reply_to_message_id: nil, reply_markup: nil) result = call(:sendPhoto, chat_id: chat_id, photo: photo, caption: caption, reply_to_message_id: , reply_markup: reply_markup) Message.new(result) end |
#send_sticker(chat_id:, sticker:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Use this method to send .webp stickers.
139 140 141 142 |
# File 'lib/telebot/client.rb', line 139 def send_sticker(chat_id:, sticker:, reply_to_message_id: nil, reply_markup: nil) result = call(:sendSticker, chat_id: chat_id, sticker: sticker, reply_to_message_id: , reply_markup: reply_markup) Message.new(result) end |
#send_video(chat_id:, video:, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message
Send video files, Telegram clients support mp4 videos (other formats may be sent as Document).
152 153 154 155 |
# File 'lib/telebot/client.rb', line 152 def send_video(chat_id:, video:, reply_to_message_id: nil, reply_markup: nil) result = call(:sendVideo, chat_id: chat_id, video: video, reply_to_message_id: , reply_markup: reply_markup) Message.new(result) end |
#set_web_hook(url:) ⇒ void
This method returns an undefined value.
Use this method to specify a url and receive incoming updates via an outgoing webhook.
205 206 207 |
# File 'lib/telebot/client.rb', line 205 def set_web_hook(url:) call(:setWebhook, url: url) end |