Class: Telegruby::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/telegruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token) ⇒ Bot

Returns a new instance of Bot.



8
9
10
11
12
# File 'lib/telegruby.rb', line 8

def initialize(api_token)
  @token = api_token
  @endpoint = "https://api.telegram.org/bot#{@token}/"
  @id = 0
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/telegruby.rb', line 6

def id
  @id
end

Instance Method Details

#answer_inline_query(id, results, cache_time: 300, is_personal: false, next_offset: nil) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/telegruby.rb', line 271

def answer_inline_query(id, results, cache_time: 300, is_personal: false, next_offset: nil)
  options = {
    :inline_query_id => id,
    :results => results,
    :cache_time => cache_time,
    :is_personal => is_personal,
  }
  if !next_offset.nil?
    options.merge!(:next_offset => next_offset)
  end
  return self.get_request("answerInlineQuery", options)
end

#forward_message(id, from_id, message_id) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/telegruby.rb', line 63

def forward_message(id, from_id, message_id)
  options = {
    :chat_id => id,
    :from_chat_id => from_id,
    :message_id => message_id
  }

  self.get_request("forwardMessage", options)
end

#get_file(file_id) ⇒ Object



264
265
266
267
268
269
# File 'lib/telegruby.rb', line 264

def get_file(file_id)
  options = {
    :file_id => file_id
  }
  return self.get_request("getFile", options)
end

#get_updates(on_error: nil) ⇒ Object

See core.telegram.org/bots/api for the full method list



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/telegruby.rb', line 16

def get_updates(on_error: nil)
  options = {
    :offset => @id
  }

  request = self.get_request("getUpdates", options)
  if request.code != 200
    if on_error.nil?
      puts "Failed to get updates: #{request}"
      return nil
    else
      return on_error(request)
    end
  end
  update = Update.new(JSON.parse(request.body, object_class: OpenStruct))
  
  if !update.result.empty?
    # there are updates, so mark them as 'seen'
    @id = (update.result.last.update_id + 1)
  end
  
  return update.result.map { |m| Message.new(m) }
end

#get_userphotos(id, offset: nil, limit: nil) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'lib/telegruby.rb', line 243

def get_userphotos(id, offset: nil, limit: nil)
  options = {
    :user_id => id,
    :offset => offset,
    :limit => limit
  }
  
  return self.get_request("getUserProfilePhotos", options)
end

#send_action(id, action) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/telegruby.rb', line 211

def send_action(id, action)
  options = {
    :chat_id => id,
    :action => action
  }
  
  return self.post_request("sendChatAction", options)
end

#send_audio(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/telegruby.rb', line 151

def send_audio(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil)
  options = {
    :chat_id => id,
  } 
  if file_id
    options.merge!(:audio => file_id)
  else
    options.merge!(:audio => File.new(filename))
  end

  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end

  self.post_request("sendAudio", options)
end

#send_document(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object

Sends a document by filename or file ID



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/telegruby.rb', line 221

def send_document(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil)
  options = {
    :chat_id => id
  }
  
  if file_id
    options.merge!(:document => file_id)
  else
    options.merge!(:document => File.new(filename))
  end

  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end

  return self.post_request("sendDocument", options)
end

#send_location(id, lat, long, reply: nil, reply_markup: nil) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/telegruby.rb', line 193

def send_location(id, lat, long, reply: nil, reply_markup: nil)
  options = {
    :chat_id => id,
    :latitude => lat,
    :longitude => long
  }
  
  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end

  self.post_request("sendLocation", options)
end

#send_message(id, text, reply: nil, parse_mode: nil, disable_preview: nil, reply_markup: nil) ⇒ Object

Send a plaintext message to a chat id



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/telegruby.rb', line 41

def send_message(id, text, reply: nil, parse_mode: nil, disable_preview: nil, reply_markup: nil)
  options = {
    :chat_id => id,
    :text => text
  }

  if !parse_mode.nil?
    options.merge!(:parse_mode => parse_mode)
  end
  if !disable_preview.nil?
    options.merge!(:disable_preview => disable_preview)
  end
  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end
  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup.to_json)
  end

  self.get_request("sendMessage", options)
end

#send_photo(id, filename: nil, file_id: nil, reply: nil, caption: nil, reply_markup: nil) ⇒ Object

Sends a photo message to a chat id



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/telegruby.rb', line 74

def send_photo(id, filename: nil, file_id: nil, reply: nil, caption: nil, reply_markup: nil)
  options = {
    :chat_id => id,
  }
  if file_id
    options.merge!(:photo => file_id)
  else
    options.merge!(:photo => File.new(filename))
  end
 
  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !caption.nil?
    options.merge!(:caption => caption)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end
  
  self.post_request("sendPhoto", options)
end

#send_photo_bytestring(id, str, reply: nil) ⇒ Object

Sends a photo from a byte string



100
101
102
103
104
105
106
107
# File 'lib/telegruby.rb', line 100

def send_photo_bytestring(id, str, reply: nil)
  Tempfile.open(["img", ".jpg"]) { |f|
    f.binmode
    f.write(str)
   
    self.send_photo(id, filename: f.path, reply: reply)
  }
end

#send_sticker(id, file_id: nil, filename: nil, reply: nil, reply_markup: nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/telegruby.rb', line 130

def send_sticker(id, file_id: nil, filename: nil, reply: nil, reply_markup: nil)
  options = {
    :chat_id => id,
  }
  if file_id
    options.merge!(:sticker => file_id)
  else
    options.merge!(:sticker => File.new(filename))
  end

  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end

  self.post_request("sendSticker", options)
end

#send_video(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/telegruby.rb', line 172

def send_video(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil)
  options = {
    :chat_id => id,
  }
  if file_id
    options.merge!(:video => file_id)
  else
    options.merge!(:video => File.new(filename))
  end

  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end

  self.post_request("sendVideo", options)
end

#send_voice(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/telegruby.rb', line 109

def send_voice(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil)
  options = {
    :chat_id => id,
  }
  if file_id
    options.merge!(:audio => file_id)
  else
    options.merge!(:audio => File.new(filename))
  end
 
  if !reply.nil?
    options.merge!(:reply_to_message_id => reply)
  end

  if !reply_markup.nil?
    options.merge!(:reply_markup => reply_markup)
  end

  self.post_request("sendVoice", options)
end

#set_webhook(url = nil, certificate = nil) ⇒ Object



253
254
255
256
257
258
259
260
261
262
# File 'lib/telegruby.rb', line 253

def set_webhook(url = nil, certificate = nil)
  options = {
    :url => nil,
  }
  if certificate
    options.merge!(:certificate => File.new(certificate))
  end
  
  return self.get_request("setWebhook", options)
end