Class: Telegram::Bot::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/bot/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Api

Returns a new instance of Api.



4
5
6
# File 'lib/telegram/bot/api.rb', line 4

def initialize(token = nil)
  @token = token || Bot.configuration.token
end

Instance Method Details

#addStickerToSet(user_id, name, png_sticker, emojis, options = {}) ⇒ Object



218
219
220
221
222
# File 'lib/telegram/bot/api.rb', line 218

def addStickerToSet(user_id, name, png_sticker, emojis, options = {})
  request('addStickerToSet', {
      user_id: user_id, name: name, png_sticker: png_sticker, emojis: emojis
  }.merge(options))
end

#answerCallbackQuery(callback_query_id, options = {}) ⇒ Object



180
181
182
# File 'lib/telegram/bot/api.rb', line 180

def answerCallbackQuery(callback_query_id, options = {})
  request('answerCallbackQuery', {callback_query_id: callback_query_id}.merge(options))
end

#connectionObject



251
252
253
254
255
256
257
258
# File 'lib/telegram/bot/api.rb', line 251

def connection
  @conn ||= Faraday.new(url: API_URL) do |faraday|
    faraday.response :json, content_type: /\bjson$/
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end
end

#createNewStickerSet(user_id, name, title, png_sticker, emojis, options = {}) ⇒ Object



212
213
214
215
216
# File 'lib/telegram/bot/api.rb', line 212

def createNewStickerSet(user_id, name, title, png_sticker, emojis, options = {})
  request('createNewStickerSet', {
      user_id: user_id, name: name, title: title, png_sticker: png_sticker, emojis: emojis
  }.merge(options))
end

#deleteChatPhoto(chat_id) ⇒ Object



132
133
134
# File 'lib/telegram/bot/api.rb', line 132

def deleteChatPhoto(chat_id)
  request('deleteChatPhoto', {chat_id: chat_id})
end

#deleteChatStickerSet(chat_id) ⇒ Object



176
177
178
# File 'lib/telegram/bot/api.rb', line 176

def deleteChatStickerSet(chat_id)
  request('deleteChatStickerSet', {chat_id: chat_id})
end

#deleteMessage(chat_id, message_id) ⇒ Object



196
197
198
# File 'lib/telegram/bot/api.rb', line 196

def deleteMessage(chat_id, message_id)
  request('deleteMessage', {chat_id: chat_id, message_id: message_id})
end

#deleteStickerFromSet(sticker) ⇒ Object



228
229
230
# File 'lib/telegram/bot/api.rb', line 228

def deleteStickerFromSet(sticker)
  request('deleteStickerFromSet', {sticker: sticker})
end

#deleteWebhookObject



20
21
22
# File 'lib/telegram/bot/api.rb', line 20

def deleteWebhook
  request('deleteWebhook')
end

#editMessageCaption(options = {}) ⇒ Object



188
189
190
# File 'lib/telegram/bot/api.rb', line 188

def editMessageCaption(options = {})
  request('editMessageCaption', options, Types::Message)
end

#editMessageLiveLocation(latitude, longitude, options = {}) ⇒ Object



80
81
82
# File 'lib/telegram/bot/api.rb', line 80

def editMessageLiveLocation(latitude, longitude, options = {})
  request('editMessageLiveLocation', {latitude: latitude, longitude: longitude}.merge(options), Types::Message)
end

#editMessageReplyMarkup(options = {}) ⇒ Object



192
193
194
# File 'lib/telegram/bot/api.rb', line 192

def editMessageReplyMarkup(options = {})
  request('editMessageReplyMarkup', options, Types::Message)
end

#editMessageText(text, options = {}) ⇒ Object



184
185
186
# File 'lib/telegram/bot/api.rb', line 184

def editMessageText(text, options = {})
  request('editMessageText', {text: text}.merge(options), Types::Message)
end


124
125
126
# File 'lib/telegram/bot/api.rb', line 124

def exportChatInviteLink(chat_id)
  request('exportChatInviteLink', {chat_id: chat_id})
end

#forwardMessage(chat_id, from_chat_id, message_id, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/telegram/bot/api.rb', line 40

def forwardMessage(chat_id, from_chat_id, message_id, options = {})
  request('forwardMessage', {
      chat_id: chat_id, from_chat_id: from_chat_id, message_id: message_id
  }.merge(options), Types::Message)
end

#getChat(user_id) ⇒ Object



156
157
158
# File 'lib/telegram/bot/api.rb', line 156

def getChat(user_id)
  request('getChat', {chat_id: user_id}, Types::Chat)
end

#getChatAdministrators(chat_id) ⇒ Object



160
161
162
# File 'lib/telegram/bot/api.rb', line 160

def getChatAdministrators(chat_id)
  request('getChatAdministrators', {chat_id: chat_id}, Types::ChatMember, true)
end

#getChatMember(chat_id, user_id) ⇒ Object



168
169
170
# File 'lib/telegram/bot/api.rb', line 168

def getChatMember(chat_id, user_id)
  request('getChatMember', {chat_id: chat_id, user_id: user_id}, Types::ChatMember)
end

#getChatMembersCount(chat_id) ⇒ Object



164
165
166
# File 'lib/telegram/bot/api.rb', line 164

def getChatMembersCount(chat_id)
  request('getChatMembersCount', {chat_id: chat_id})
end

#getFile(file_id) ⇒ Object



32
33
34
# File 'lib/telegram/bot/api.rb', line 32

def getFile(file_id)
  request('getFile', {file_id: file_id}, Types::File)
end

#getMeObject



12
13
14
# File 'lib/telegram/bot/api.rb', line 12

def getMe
  request('getMe', {}, Types::User)
end

#getStickerSet(name) ⇒ Object



204
205
206
# File 'lib/telegram/bot/api.rb', line 204

def getStickerSet(name)
  request('getStickerSet', {name: name}, Types::StickerSet)
end

#getUpdates(options = {}) ⇒ Object



28
29
30
# File 'lib/telegram/bot/api.rb', line 28

def getUpdates(options = {})
  request('getUpdates', options, Types::Update, true)
end

#getUserProfilePhotos(user_id, options = {}) ⇒ Object



104
105
106
# File 'lib/telegram/bot/api.rb', line 104

def getUserProfilePhotos(user_id, options = {})
  request('getUserProfilePhotos', {user_id: user_id}.merge(options), Types::UserProfilePhotos)
end

#getWebhookInfoObject



24
25
26
# File 'lib/telegram/bot/api.rb', line 24

def getWebhookInfo
  request('getWebhookInfo', {}, Types::WebhookInfo)
end

#kickChatMember(chat_id, user_id, options = {}) ⇒ Object



108
109
110
# File 'lib/telegram/bot/api.rb', line 108

def kickChatMember(chat_id, user_id, options = {})
  request('kickChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
end

#leaveChat(chat_id) ⇒ Object



152
153
154
# File 'lib/telegram/bot/api.rb', line 152

def leaveChat(chat_id)
  request('leaveChat', {chat_id: chat_id})
end

#pinChatMessage(chat_id, message_id, options = {}) ⇒ Object



144
145
146
# File 'lib/telegram/bot/api.rb', line 144

def pinChatMessage(chat_id, message_id, options = {})
  request('pinChatMessage', {chat_id: chat_id, message_id: message_id}.merge(options))
end

#promoteChatMember(chat_id, user_id, options = {}) ⇒ Object



116
117
118
# File 'lib/telegram/bot/api.rb', line 116

def promoteChatMember(chat_id, user_id, options = {})
  request('promoteChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
end

#request(method, query = {}, type_obj = nil, as_array = false) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/telegram/bot/api.rb', line 232

def request(method, query = {}, type_obj = nil, as_array = false)
  query[:reply_markup] = query[:reply_markup].to_json if query.has_key?(:reply_markup)

  response  = connection.post("/bot#{@token}/#{method}", query)
  body      = response.body

  if Bot.configuration.raise_exceptions && (response.status != 200 || body['ok'] != true)
    raise Telegram::Bot::ResponseError.new(body['description'])
  end

  if type_obj.nil?
    body['result']
  elsif as_array
    body['result'].map {|r| type_obj.new(r) }
  else
    type_obj.new(body['result'])
  end
end

#restrictChatMember(chat_id, user_id, options = {}) ⇒ Object



120
121
122
# File 'lib/telegram/bot/api.rb', line 120

def restrictChatMember(chat_id, user_id, options = {})
  request('restrictChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
end

#sendAudio(chat_id, audio, options = {}) ⇒ Object



50
51
52
# File 'lib/telegram/bot/api.rb', line 50

def sendAudio(chat_id, audio, options = {})
  request('sendAudio', {chat_id: chat_id, audio: audio}.merge(options), Types::Message)
end

#sendChatAction(chat_id, action, options = {}) ⇒ Object



100
101
102
# File 'lib/telegram/bot/api.rb', line 100

def sendChatAction(chat_id, action, options = {})
  request('sendChatAction', {chat_id: chat_id, action: action}.merge(options))
end

#sendContact(chat_id, phone_number, first_name, options = {}) ⇒ Object



94
95
96
97
98
# File 'lib/telegram/bot/api.rb', line 94

def sendContact(chat_id, phone_number, first_name, options = {})
  request('sendContact', {
      chat_id: chat_id, phone_number: phone_number, first_name: first_name
  }.merge(options), Types::Message)
end

#sendDocument(chat_id, document, options = {}) ⇒ Object



54
55
56
# File 'lib/telegram/bot/api.rb', line 54

def sendDocument(chat_id, document, options = {})
  request('sendDocument', {chat_id: chat_id, document: document}.merge(options), Types::Message)
end

#sendLocation(chat_id, latitude, longitude, options = {}) ⇒ Object



74
75
76
77
78
# File 'lib/telegram/bot/api.rb', line 74

def sendLocation(chat_id, latitude, longitude, options = {})
  request('sendLocation', {
      chat_id: chat_id, latitude: latitude, longitude: longitude
  }.merge(options), Types::Message)
end

#sendMediaGroup(chat_id, video_note, options = {}) ⇒ Object



66
67
68
# File 'lib/telegram/bot/api.rb', line 66

def sendMediaGroup(chat_id, video_note, options = {})
  request('sendMediaGroup', {chat_id: chat_id, video_note: video_note}.merge(options), Types::Message)
end

#sendMessage(chat_id, text, options = {}) ⇒ Object



36
37
38
# File 'lib/telegram/bot/api.rb', line 36

def sendMessage(chat_id, text, options = {})
  request('sendMessage', {chat_id: chat_id, text: text}.merge(options), Types::Message)
end

#sendPhoto(chat_id, photo, options = {}) ⇒ Object



46
47
48
# File 'lib/telegram/bot/api.rb', line 46

def sendPhoto(chat_id, photo, options = {})
  request('sendPhoto', {chat_id: chat_id, photo: photo}.merge(options), Types::Message)
end

#sendSticker(chat_id, sticker, options = {}) ⇒ Object



200
201
202
# File 'lib/telegram/bot/api.rb', line 200

def sendSticker(chat_id, sticker, options = {})
  request('sendSticker', {chat_id: chat_id, sticker: sticker}.merge(options), Types::Message)
end

#sendVenue(chat_id, latitude, longitude, title, address, options = {}) ⇒ Object



88
89
90
91
92
# File 'lib/telegram/bot/api.rb', line 88

def sendVenue(chat_id, latitude, longitude, title, address, options = {})
  request('sendVenue', {
      chat_id: chat_id, latitude: latitude, longitude: longitude, title: title, address: address
  }.merge(options), Types::Message)
end

#sendVideo(chat_id, video, options = {}) ⇒ Object



58
59
60
# File 'lib/telegram/bot/api.rb', line 58

def sendVideo(chat_id, video, options = {})
  request('sendVideo', {chat_id: chat_id, video: video}.merge(options), Types::Message)
end

#sendVideoNote(chat_id, media, options = {}) ⇒ Object



70
71
72
# File 'lib/telegram/bot/api.rb', line 70

def sendVideoNote(chat_id, media, options = {})
  request('sendVideoNote', {chat_id: chat_id, media: media}.merge(options), Types::Message, true)
end

#sendVoice(chat_id, voice, options = {}) ⇒ Object



62
63
64
# File 'lib/telegram/bot/api.rb', line 62

def sendVoice(chat_id, voice, options = {})
  request('sendVoice', {chat_id: chat_id, voice: voice}.merge(options), Types::Message)
end

#setChatDescription(chat_id, options = {}) ⇒ Object



140
141
142
# File 'lib/telegram/bot/api.rb', line 140

def setChatDescription(chat_id, options = {})
  request('setChatDescription', {chat_id: chat_id}.merge(options))
end

#setChatPhoto(chat_id, photo) ⇒ Object



128
129
130
# File 'lib/telegram/bot/api.rb', line 128

def setChatPhoto(chat_id, photo)
  request('setChatPhoto', {chat_id: chat_id, photo: photo})
end

#setChatStickerSet(chat_id, sticker_set_name) ⇒ Object



172
173
174
# File 'lib/telegram/bot/api.rb', line 172

def setChatStickerSet(chat_id, sticker_set_name)
  request('setChatStickerSet', {chat_id: chat_id, sticker_set_name: sticker_set_name})
end

#setChatTitle(chat_id, title) ⇒ Object



136
137
138
# File 'lib/telegram/bot/api.rb', line 136

def setChatTitle(chat_id, title)
  request('setChatTitle', {chat_id: chat_id, title: title})
end

#setStickerPositionInSet(sticker, position) ⇒ Object



224
225
226
# File 'lib/telegram/bot/api.rb', line 224

def setStickerPositionInSet(sticker, position)
  request('setStickerPositionInSet', {sticker: sticker, position: position})
end

#setWebhook(url, options = {}) ⇒ Object



16
17
18
# File 'lib/telegram/bot/api.rb', line 16

def setWebhook(url, options = {})
  request('setWebhook', {url: url}.merge(options))
end

#stopMessageLiveLocation(options = {}) ⇒ Object



84
85
86
# File 'lib/telegram/bot/api.rb', line 84

def stopMessageLiveLocation(options = {})
  request('stopMessageLiveLocation', options, Types::Message)
end

#tokenObject



8
9
10
# File 'lib/telegram/bot/api.rb', line 8

def token
  @token
end

#unbanChatMember(chat_id, user_id, options = {}) ⇒ Object



112
113
114
# File 'lib/telegram/bot/api.rb', line 112

def unbanChatMember(chat_id, user_id, options = {})
  request('unbanChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
end

#unpinChatMessage(chat_id) ⇒ Object



148
149
150
# File 'lib/telegram/bot/api.rb', line 148

def unpinChatMessage(chat_id)
  request('unpinChatMessage', {chat_id: chat_id})
end

#uploadStickerFile(user_id, png_sticker) ⇒ Object



208
209
210
# File 'lib/telegram/bot/api.rb', line 208

def uploadStickerFile(user_id, png_sticker)
  request('uploadStickerFile', {user_id: user_id, png_sticker: png_sticker}, Types::File)
end