Class: Telegruby::Bot
- Inherits:
-
Object
- Object
- Telegruby::Bot
- Defined in:
- lib/telegruby.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Instance Method Summary collapse
- #answer_inline_query(id, results, cache_time: 300, is_personal: false, next_offset: nil) ⇒ Object
- #forward_message(id, from_id, message_id) ⇒ Object
- #get_file(file_id) ⇒ Object
-
#get_updates(on_error: nil) ⇒ Object
See core.telegram.org/bots/api for the full method list.
- #get_userphotos(id, offset: nil, limit: nil) ⇒ Object
-
#initialize(api_token) ⇒ Bot
constructor
A new instance of Bot.
- #send_action(id, action) ⇒ Object
- #send_audio(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object
-
#send_document(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object
Sends a document by filename or file ID.
- #send_location(id, lat, long, reply: nil, reply_markup: nil) ⇒ Object
-
#send_message(id, text, reply: nil, parse_mode: nil, disable_preview: nil, reply_markup: nil) ⇒ Object
Send a plaintext message to a chat id.
-
#send_photo(id, filename: nil, file_id: nil, reply: nil, caption: nil, reply_markup: nil) ⇒ Object
Sends a photo message to a chat id.
-
#send_photo_bytestring(id, str, reply: nil) ⇒ Object
Sends a photo from a byte string.
- #send_sticker(id, file_id: nil, filename: nil, reply: nil, reply_markup: nil) ⇒ Object
- #send_video(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object
- #send_voice(id, filename: nil, file_id: nil, reply: nil, reply_markup: nil) ⇒ Object
- #set_webhook(url = nil, certificate = nil) ⇒ Object
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
#id ⇒ Object
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) = { :inline_query_id => id, :results => results, :cache_time => cache_time, :is_personal => is_personal, } if !next_offset.nil? .merge!(:next_offset => next_offset) end return self.get_request("answerInlineQuery", ) 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 (id, from_id, ) = { :chat_id => id, :from_chat_id => from_id, :message_id => } self.get_request("forwardMessage", ) end |
#get_file(file_id) ⇒ Object
264 265 266 267 268 269 |
# File 'lib/telegruby.rb', line 264 def get_file(file_id) = { :file_id => file_id } return self.get_request("getFile", ) 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) = { :offset => @id } request = self.get_request("getUpdates", ) 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) = { :user_id => id, :offset => offset, :limit => limit } return self.get_request("getUserProfilePhotos", ) 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) = { :chat_id => id, :action => action } return self.post_request("sendChatAction", ) 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) = { :chat_id => id, } if file_id .merge!(:audio => file_id) else .merge!(:audio => File.new(filename)) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end self.post_request("sendAudio", ) 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) = { :chat_id => id } if file_id .merge!(:document => file_id) else .merge!(:document => File.new(filename)) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end return self.post_request("sendDocument", ) 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) = { :chat_id => id, :latitude => lat, :longitude => long } if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end self.post_request("sendLocation", ) 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 (id, text, reply: nil, parse_mode: nil, disable_preview: nil, reply_markup: nil) = { :chat_id => id, :text => text } if !parse_mode.nil? .merge!(:parse_mode => parse_mode) end if !disable_preview.nil? .merge!(:disable_preview => disable_preview) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup.to_json) end self.get_request("sendMessage", ) 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) = { :chat_id => id, } if file_id .merge!(:photo => file_id) else .merge!(:photo => File.new(filename)) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !caption.nil? .merge!(:caption => caption) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end self.post_request("sendPhoto", ) 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) = { :chat_id => id, } if file_id .merge!(:sticker => file_id) else .merge!(:sticker => File.new(filename)) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end self.post_request("sendSticker", ) 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) = { :chat_id => id, } if file_id .merge!(:video => file_id) else .merge!(:video => File.new(filename)) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end self.post_request("sendVideo", ) 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) = { :chat_id => id, } if file_id .merge!(:audio => file_id) else .merge!(:audio => File.new(filename)) end if !reply.nil? .merge!(:reply_to_message_id => reply) end if !reply_markup.nil? .merge!(:reply_markup => reply_markup) end self.post_request("sendVoice", ) 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) = { :url => nil, } if certificate .merge!(:certificate => File.new(certificate)) end return self.get_request("setWebhook", ) end |