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



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.



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.



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.



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.



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)



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



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.



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.



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.



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.



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.



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



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.



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

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