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) ⇒ Object
- #getMe ⇒ Object
- #getUpdates(options = {"timeout"=>0, "limit"=>100}) ⇒ Object
-
#getUserProfilePhotos(id, options = {}) ⇒ UserProfilePhotos
Get a list of user profile photos, in up to 4 sizes each.
-
#initialize(token) ⇒ TelegramAPI
constructor
A new instance of TelegramAPI.
-
#kickChatMember(chat_id, user_id) ⇒ Object
Kick the user user_id from the chat chat_id.
- #sendAudio(to, path, options = {}) ⇒ Object
-
#sendChatAction(to, act) ⇒ Object
Send a Chat Action.
-
#sendDocument(to, path, options = {}) ⇒ Object
Send a general document (file, image, audio).
-
#sendLocation(to, lat, long, options = {}) ⇒ Object
Send a location.
- #sendMessage(to, text, options = {}) ⇒ Object
- #sendPhoto(to, path, options = {}) ⇒ Object
-
#sendSticker(to, id, options = {}) ⇒ Object
Send a Sticker through its ID.
-
#sendStickerFromFile(to, path, options = {}) ⇒ Object
Send a Sticker from File.
-
#sendVideo(to, path, options = {}) ⇒ Object
Send a video file in mp4 format of max 50MB.
-
#unbanChatMember(chat_id, user_id) ⇒ Object
Unban user_id from chat_id see kickChatMember.
Constructor Details
#initialize(token) ⇒ TelegramAPI
Returns a new instance of TelegramAPI.
15 16 17 18 |
# File 'lib/telegramAPI.rb', line 15 def initialize token @token = token @last_update = 0 end |
Instance Method Details
#forwardMessage(to, from, msg) ⇒ Object
60 61 62 |
# File 'lib/telegramAPI.rb', line 60 def forwardMessage to, from, msg self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})["result"] end |
#getMe ⇒ Object
49 50 51 |
# File 'lib/telegramAPI.rb', line 49 def getMe self.query("getMe") end |
#getUpdates(options = {"timeout"=>0, "limit"=>100}) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/telegramAPI.rb', line 42 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 if r['result'][-1]!=nil then @last_update=r['result'][-1]['update_id']+1 end return r['result'] end |
#getUserProfilePhotos(id, options = {}) ⇒ UserProfilePhotos
Get a list of user profile photos, in up to 4 sizes each
123 124 125 |
# File 'lib/telegramAPI.rb', line 123 def getUserProfilePhotos id, ={} self.query("getUserProfilePhotos", {"user_id"=>id}.merge(parse_hash()))["result"] end |
#kickChatMember(chat_id, user_id) ⇒ Object
Kick the user user_id from the chat chat_id
130 131 132 |
# File 'lib/telegramAPI.rb', line 130 def kickChatMember chat_id, user_id self.query "kickChatMember", {"chat_id"=>chat_id, "user_id"=>user_id} end |
#sendAudio(to, path, options = {}) ⇒ Object
68 69 70 |
# File 'lib/telegramAPI.rb', line 68 def sendAudio to, path, ={} self.post "/sendAudio", :audio, path, to, end |
#sendChatAction(to, act) ⇒ Object
Send a Chat Action
115 116 117 |
# File 'lib/telegramAPI.rb', line 115 def sendChatAction to, act self.query "sendChatAction", {"chat_id"=>to, "action"=>act} end |
#sendDocument(to, path, options = {}) ⇒ Object
Send a general document (file, image, audio)
75 76 77 |
# File 'lib/telegramAPI.rb', line 75 def sendDocument to, path, ={} self.post "/sendDocument", :document, path, to, end |
#sendLocation(to, lat, long, options = {}) ⇒ Object
Send a location
108 109 110 |
# File 'lib/telegramAPI.rb', line 108 def sendLocation to, lat, long, ={} self.query("sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge(parse_hash()))["result"] end |
#sendMessage(to, text, options = {}) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/telegramAPI.rb', line 53 def sendMessage to, text, ={} if .has_key?"reply_markup" then ["reply_markup"]=["reply_markup"].to_json end self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(parse_hash()))["result"] end |
#sendPhoto(to, path, options = {}) ⇒ Object
64 65 66 |
# File 'lib/telegramAPI.rb', line 64 def sendPhoto to, path, ={} self.post "/sendPhoto", :photo, path, to, end |
#sendSticker(to, id, options = {}) ⇒ Object
Send a Sticker through its ID
91 92 93 |
# File 'lib/telegramAPI.rb', line 91 def sendSticker to, id, ={} JSON.parse(RestClient.post(@@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(parse_hash())).body)["result"] end |
#sendStickerFromFile(to, path, options = {}) ⇒ Object
Send a Sticker from File
82 83 84 |
# File 'lib/telegramAPI.rb', line 82 def sendStickerFromFile to, path, ={} self.post "/sendSticker", :sticker, path, to, end |
#sendVideo(to, path, options = {}) ⇒ Object
Send a video file in mp4 format of max 50MB
98 99 100 |
# File 'lib/telegramAPI.rb', line 98 def sendVideo to, path, ={} self.post "/sendVideo", :video, path, to, end |
#unbanChatMember(chat_id, user_id) ⇒ Object
Unban user_id from chat_id see kickChatMember
136 137 138 |
# File 'lib/telegramAPI.rb', line 136 def unbanChatMember chat_id, user_id self.query "unbanChatMember", {"chat_id"=>chat_id, "user_id"=>user_id} end |