Class: Telebot::Client

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • chat_id (Integer)

    Unique identifier for the message recipient - User or GroupChat id

  • from_chat_id (Integer)

    Unique identifier for the chat where the original message was sent - User or GroupChat id

  • message_id (Integer)

    Unique message identifier

Returns:



84
85
86
87
# File 'lib/telebot/client.rb', line 84

def forward_message(chat_id:, from_chat_id:, message_id:)
  result = call(:forwardMessage, chat_id: chat_id, from_chat_id: from_chat_id, message_id: message_id)
  Message.new(result)
end

#get_meTelebot::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.

Returns:



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:

  1. This method will not work if an outgoing webhook is set up.

  2. In order to avoid getting duplicate updates, recalculate offset after each server response.

Parameters:

  • offset (Integer) (defaults to: nil)

    Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.

  • limit (Integer) (defaults to: nil)

    Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100

  • timeout (Integer) (defaults to: nil)

    Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling.

Returns:



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.

Parameters:

  • user_id (Integer)
  • offset (Integer) (defaults to: nil)
  • limit (Integer) (defaults to: nil)

Returns:



197
198
199
200
# File 'lib/telebot/client.rb', line 197

def (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)

Parameters:

  • chat_id (Integer)
  • audio (Telebot::InputFile, String)

    Audio file to send (file or file_id)

  • reply_to_message_id (Integer) (defaults to: nil)

    If the message is a reply, ID of the original message

  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

    Additional interface options

Returns:



115
116
117
118
# File 'lib/telebot/client.rb', line 115

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_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).

Parameters:

  • chat_id (Integer)
  • action (Symbol)

    :typing, :upload_photo, etc. See CHAT_ACTIONS.



185
186
187
188
# File 'lib/telebot/client.rb', line 185

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.

Parameters:

  • chat_id (Integer)
  • document (Telebot::InputFile, String)

    document to send (file or file_id)

  • reply_to_message_id (Integer) (defaults to: nil)

    If the message is a reply, ID of the original message

  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

    Additional interface options

Returns:



128
129
130
131
# File 'lib/telebot/client.rb', line 128

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_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.

Parameters:

  • chat_id (Integer)
  • latitude (Integer)
  • longitude (Integer)
  • reply_to_message_id (Integer) (defaults to: nil)
  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

Returns:



168
169
170
171
172
173
174
175
# File 'lib/telebot/client.rb', line 168

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_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, parse_mode: nil) ⇒ Telebot::Message

Send text message.

Parameters:

  • chat_id (Integer)

    Unique identifier for the message recipient - User or GroupChat id

  • text (String)

    Text of the message to be sent

  • disable_web_page_preview (Boolean) (defaults to: false)

    Disables link previews for links in this message

  • reply_to_message_id (Integer) (defaults to: nil)

    If the message is a reply, ID of the original message

  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

    Additional interface options

  • parse_mode (String) (defaults to: nil)

    “Markdown” or “HTML”, Optional

Returns:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/telebot/client.rb', line 65

def send_message(chat_id:, text:, disable_web_page_preview: false, reply_to_message_id: nil, reply_markup: nil, parse_mode: nil)
  result = call(:sendMessage,
    chat_id: chat_id,
    text: text,
    disable_web_page_preview: disable_web_page_preview,
    reply_to_message_id: reply_to_message_id,
    reply_markup: reply_markup,
    parse_mode: parse_mode
  )
  Message.new(result)
end

#send_photo(chat_id:, photo:, caption: nil, reply_to_message_id: nil, reply_markup: nil) ⇒ Telebot::Message

Send a picture.

Parameters:

  • chat_id (Integer)

    Unique identifier for the message recipient - User or GroupChat id

  • photo (InputFile, String)

    Photo to send. You can either pass a file_id as String to resend a photo that is already on the Telegram servers, or upload a new photo using multipart/form-data.

  • caption (String) (defaults to: nil)

    Photo caption (may also be used when resending photos by file_id)

  • reply_to_message_id (Integer) (defaults to: nil)

    If the message is a reply, ID of the original message

  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

    Additional interface options

Returns:



100
101
102
103
# File 'lib/telebot/client.rb', line 100

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_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.

Parameters:

  • chat_id (Integer)
  • sticker (Telebot::InputFile, String)

    sticker to send (file or file_id)

  • reply_to_message_id (Integer) (defaults to: nil)

    If the message is a reply, ID of the original message

  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

    Additional interface options

Returns:



141
142
143
144
# File 'lib/telebot/client.rb', line 141

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_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).

Parameters:

  • chat_id (Integer)
  • video (InputFile, String)

    file or file_id

  • reply_to_message_id (Integer) (defaults to: nil)
  • reply_markup (ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply) (defaults to: nil)

Returns:



154
155
156
157
# File 'lib/telebot/client.rb', line 154

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_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.

Parameters:

  • url (String)

    HTTPS url to send updates to. Use an empty string to remove webhook integration



207
208
209
# File 'lib/telebot/client.rb', line 207

def set_web_hook(url:)
  call(:setWebhook, url: url)
end