Class: TelegramAPI
- Inherits:
-
Object
- Object
- TelegramAPI
- Defined in:
- lib/telegramAPI.rb
Overview
This library provides an easy way to access to the Telegram Bot API
- Author
-
Benedetto Nespoli
- License
-
MIT
Constant Summary collapse
- @@core =
"https://api.telegram.org/bot"
Instance Method Summary collapse
-
#forwardMessage(to, from, msg) ⇒ Message
Send a message as forwarded.
-
#getMe ⇒ User
Provide information about the bot itself.
-
#getUpdates(options = {"timeout"=>0, "limit"=>100}) ⇒ Array<Update>
Get last updates, including last received messages.
-
#getUserProfilePhotos(id, options = {}) ⇒ UserProfilePhotos
Get a list of user profile photos, in up to 4 sizes each.
-
#initialize(token) ⇒ TelegramAPI
constructor
Create a new instance of TelegramAPI.
-
#sendAudio(to, path, options = {}) ⇒ Message
Send an audio file in Ogg OPUS format of max 50MB.
-
#sendChatAction(to, act) ⇒ Object
Send a Chat Action.
-
#sendDocument(to, path, options = {}) ⇒ Message
Send a general document (file, image, audio).
-
#sendLocation(to, lat, long, options = {}) ⇒ Message
Send a location.
-
#sendMessage(to, text, options = {}) ⇒ Message
Send a message to the user with id
to
, with the texttext
. -
#sendPhoto(to, path, options = {}) ⇒ Message
Send a local file containing a photo.
-
#sendSticker(to, id, options = {}) ⇒ Message
Send a Sticker through its ID.
-
#sendStickerFromFile(to, path, options = {}) ⇒ Message
Send a Sticker from File.
-
#sendVideo(to, path, options = {}) ⇒ Message
Send a video file in mp4 format of max 50MB.
Constructor Details
#initialize(token) ⇒ TelegramAPI
Create a new instance of TelegramAPI
19 20 21 22 |
# File 'lib/telegramAPI.rb', line 19 def initialize token @token = token @last_update = 0 end |
Instance Method Details
#forwardMessage(to, from, msg) ⇒ Message
Send a message as forwarded
76 77 78 |
# File 'lib/telegramAPI.rb', line 76 def forwardMessage to, from, msg Message.new self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})["result"] end |
#getMe ⇒ User
Provide information about the bot itself
44 45 46 |
# File 'lib/telegramAPI.rb', line 44 def getMe User.new self.query("getMe") end |
#getUpdates(options = {"timeout"=>0, "limit"=>100}) ⇒ Array<Update>
Get last updates, including last received messages
51 52 53 54 55 56 57 |
# File 'lib/telegramAPI.rb', line 51 def getUpdates ={"timeout"=>0, "limit"=>100} r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash()) 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 = {}) ⇒ UserProfilePhotos
Get a list of user profile photos, in up to 4 sizes each
147 148 149 |
# File 'lib/telegramAPI.rb', line 147 def getUserProfilePhotos id, ={} UserProfilePhotos.new self.query("getUserProfilePhotos", {"user_id"=>id}.merge(parse_hash()))["result"] end |
#sendAudio(to, path, options = {}) ⇒ Message
Send an audio file in Ogg OPUS format of max 50MB
92 93 94 |
# File 'lib/telegramAPI.rb', line 92 def sendAudio to, path, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendAudio", {:audio=>File.new(path, 'rb'), :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |
#sendChatAction(to, act) ⇒ Object
Send a Chat Action
139 140 141 |
# File 'lib/telegramAPI.rb', line 139 def sendChatAction to, act self.query "sendChatAction", {"chat_id"=>to, "action"=>act} end |
#sendDocument(to, path, options = {}) ⇒ Message
Send a general document (file, image, audio)
99 100 101 |
# File 'lib/telegramAPI.rb', line 99 def sendDocument to, path, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendDocument", {:document=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |
#sendLocation(to, lat, long, options = {}) ⇒ Message
Send a location
132 133 134 |
# File 'lib/telegramAPI.rb', line 132 def sendLocation to, lat, long, ={} Message.new self.query("sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge(parse_hash()))["result"] end |
#sendMessage(to, text, options = {}) ⇒ Message
Send a message to the user with id to
, with the text text
64 65 66 67 68 69 |
# File 'lib/telegramAPI.rb', line 64 def sendMessage to, text, ={} if .has_key?"reply_markup" then ["reply_markup"]=["reply_markup"].to_json end Message.new self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(parse_hash()))["result"] end |
#sendPhoto(to, path, options = {}) ⇒ Message
Send a local file containing a photo
85 86 87 |
# File 'lib/telegramAPI.rb', line 85 def sendPhoto to, path, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendPhoto", {:photo=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |
#sendSticker(to, id, options = {}) ⇒ Message
Send a Sticker through its ID
115 116 117 |
# File 'lib/telegramAPI.rb', line 115 def sendSticker to, id, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |
#sendStickerFromFile(to, path, options = {}) ⇒ Message
Send a Sticker from File
106 107 108 |
# File 'lib/telegramAPI.rb', line 106 def sendStickerFromFile to, path, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendStiker", {:sticker=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |
#sendVideo(to, path, options = {}) ⇒ Message
Send a video file in mp4 format of max 50MB
122 123 124 |
# File 'lib/telegramAPI.rb', line 122 def sendVideo to, path, ={} Message.new JSON.parse(RestClient.post(@@core+@token+"/sendVideo", {:video=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |