Class: TelegramAPI
- Inherits:
-
Object
- Object
- TelegramAPI
- Defined in:
- lib/telegramAPI.rb
Constant Summary collapse
- @@core =
"https://api.telegram.org/bot"
Instance Method Summary collapse
- #forwardMessage(to, from, msg) ⇒ Object
- #getMe ⇒ Object
- #getUpdates(options = {}) ⇒ Object
- #getUserProfilePhotos(id, options = {}) ⇒ Object
-
#initialize(t) ⇒ TelegramAPI
constructor
A new instance of TelegramAPI.
- #query(api, params = {}) ⇒ Object
-
#sendAudio(to, path, options = {}) ⇒ Object
Max size: 50MB Format: Ogg OPUS.
-
#sendChatAction(to, act) ⇒ Object
act is one between: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location.
- #sendDocument(to, path, options = {}) ⇒ Object
- #sendLocation(to, lat, long, options = {}) ⇒ Object
- #sendMessage(to, text, options = {}) ⇒ Object
- #sendPhoto(to, path, options = {}) ⇒ Object
- #sendSticker(to, id, options = {}) ⇒ Object
- #sendStickerFromFile(to, path, options = {}) ⇒ Object
-
#sendVideo(to, path, options = {}) ⇒ Object
Max size: 50MB Fromat: mp4.
Constructor Details
#initialize(t) ⇒ TelegramAPI
Returns a new instance of TelegramAPI.
12 13 14 15 |
# File 'lib/telegramAPI.rb', line 12 def initialize t @token = t @last_update = 0 end |
Instance Method Details
#forwardMessage(to, from, msg) ⇒ Object
43 44 45 |
# File 'lib/telegramAPI.rb', line 43 def forwardMessage to, from, msg Message.new self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg}) end |
#getMe ⇒ Object
27 28 29 |
# File 'lib/telegramAPI.rb', line 27 def getMe User.new self.query("getMe") end |
#getUpdates(options = {}) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/telegramAPI.rb', line 31 def getUpdates ={} r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge() if r['ok']!=true then return nil end up=ArrayOf.new(r['result'],Update).to_a if up[-1]!=nil then @last_update=up[-1].update_id+1 end return up end |
#getUserProfilePhotos(id, options = {}) ⇒ Object
84 85 86 |
# File 'lib/telegramAPI.rb', line 84 def getUserProfilePhotos id, ={} self.query "getUserProfilePhotos", {"user_id"=>id}.merge() end |
#query(api, params = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/telegramAPI.rb', line 17 def query api, params={} p=[] params_s="" params.each do |param| p<<param.join("=") end params_s="?"+p.join("&") if p.length!=0 JSON.parse(open(@@core+@token+"/"+api+params_s).read) end |
#sendAudio(to, path, options = {}) ⇒ Object
Max size: 50MB Format: Ogg OPUS. On ubuntu: avconv -i input.ext -acodec libopus output.ogg
53 54 55 |
# File 'lib/telegramAPI.rb', line 53 def sendAudio to, path, ={} RestClient.post @@core+@token+"/sendAudio", {:audio=>File.new(path, 'rb'), :chat_id=>to.to_s}.merge() end |
#sendChatAction(to, act) ⇒ Object
act is one between: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location
80 81 82 |
# File 'lib/telegramAPI.rb', line 80 def sendChatAction to, act self.query "sendChatAction", {"chat_id"=>to, "action"=>act} end |
#sendDocument(to, path, options = {}) ⇒ Object
57 58 59 |
# File 'lib/telegramAPI.rb', line 57 def sendDocument to, path, ={} RestClient.post @@core+@token+"/sendDocument", {:document=>File.new(path,'rb'), :chat_id=>to.to_s}.merge() end |
#sendLocation(to, lat, long, options = {}) ⇒ Object
75 76 77 |
# File 'lib/telegramAPI.rb', line 75 def sendLocation to, lat, long, ={} self.query "sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge() end |
#sendMessage(to, text, options = {}) ⇒ Object
39 40 41 |
# File 'lib/telegramAPI.rb', line 39 def sendMessage to, text, ={} Message.new self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge()) end |
#sendPhoto(to, path, options = {}) ⇒ Object
47 48 49 |
# File 'lib/telegramAPI.rb', line 47 def sendPhoto to, path, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendPhoto", {:photo=>File.new(path,'rb'), :chat_id=>to.to_s}.merge()).body)["result"] end |
#sendSticker(to, id, options = {}) ⇒ Object
65 66 67 |
# File 'lib/telegramAPI.rb', line 65 def sendSticker to, id, ={} RestClient.post @@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge() end |
#sendStickerFromFile(to, path, options = {}) ⇒ Object
61 62 63 |
# File 'lib/telegramAPI.rb', line 61 def sendStickerFromFile to, path, ={} RestClient.post @@core+@token+"/sendStiker", {:sticker=>File.new(path,'rb'), :chat_id=>to.to_s}.merge() end |
#sendVideo(to, path, options = {}) ⇒ Object
Max size: 50MB Fromat: mp4
71 72 73 |
# File 'lib/telegramAPI.rb', line 71 def sendVideo to, path, ={} RestClient.post @@core+@token+"/sendVideo", {:video=>File.new(path,'rb'), :chat_id=>to.to_s}.merge() end |