class TelegramAPI

This library provides an easy way to access to the Telegram Bot API

Author

Benedetto Nespoli

License

MIT

Public Class Methods

new(token) click to toggle source
# File lib/telegramAPI.rb, line 14
def initialize token
  @token = token
  @last_update = 0
end

Public Instance Methods

answerInlineQuery(inline_query_id, results) click to toggle source
# File lib/telegramAPI.rb, line 141
def answerInlineQuery inline_query_id, results
  self.query("answerInlineQuery", {:inline_query_id=>inline_query_id, :results=>JSON.dump(results)})
end
forwardMessage(to, from, msg) click to toggle source
# File lib/telegramAPI.rb, line 56
def forwardMessage to, from, msg
  self.query("forwardMessage", {:chat_id=>to, :from_chat_id=>from, :message_id=>msg})
end
getChat(chat_id) click to toggle source
# File lib/telegramAPI.rb, line 125
def getChat chat_id
  self.query("getChat", {:chat_id=>chat_id})
end
getChatAdministrators(chat_id) click to toggle source
# File lib/telegramAPI.rb, line 129
def getChatAdministrators chat_id
  self.query("getChatAdministrators", {:chat_id=>chat_id})
end
getChatMember(chat_id, user_id) click to toggle source
# File lib/telegramAPI.rb, line 137
def getChatMember chat_id, user_id
  self.query("getChatMember", {:chat_id=>chat_id, :user_id=>user_id})
end
getChatMembersCount(chat_id) click to toggle source
# File lib/telegramAPI.rb, line 133
def getChatMembersCount chat_id
  self.query("getChatMembersCount", {:chat_id=>chat_id})
end
getFile(file_id) click to toggle source
# File lib/telegramAPI.rb, line 109
def getFile file_id
  self.query("getFile", {:file_id=>file_id})
end
getMe() click to toggle source
# File lib/telegramAPI.rb, line 45
def getMe
  self.query("getMe")
end
getUpdates(options={"timeout"=>0, "limit"=>100}) click to toggle source
# File lib/telegramAPI.rb, line 41
def getUpdates options={"timeout"=>0, "limit"=>100}
  self.query("getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash(options)))
end
getUserProfilePhotos(id, options={}) click to toggle source
# File lib/telegramAPI.rb, line 105
def getUserProfilePhotos id, options={}
  self.query("getUserProfilePhotos", {:user_id=>id}.merge(parse_hash(options)))
end
kickChatMember(chat_id, user_id) click to toggle source
# File lib/telegramAPI.rb, line 113
def kickChatMember chat_id, user_id
  self.query("kickChatMember", {:chat_id=>chat_id, :user_id=>user_id})
end
leaveChat(chat_id) click to toggle source
# File lib/telegramAPI.rb, line 117
def leaveChat chat_id
  self.query("leaveChat", {:chat_id=>chat_id})
end
sendAudio(to, path, options={}) click to toggle source
# File lib/telegramAPI.rb, line 64
def sendAudio to, path, options={}
  self.post("/sendAudio", :audio, path, to, options)
end
sendChatAction(to, act) click to toggle source

act is one between: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location

# File lib/telegramAPI.rb, line 101
def sendChatAction to, act
  self.query("sendChatAction", {:chat_id=>to, :action=>act})
end
sendContact(to, phone_number, first_name, options={}) click to toggle source
# File lib/telegramAPI.rb, line 96
def sendContact to, phone_number, first_name, options={}
  self.query("sendContact", {:chat_id=>to, :phone_number=>phone_number, :first_name=>first_name}.merge(parse_hash(options)))
end
sendDocument(to, path, options={}) click to toggle source
# File lib/telegramAPI.rb, line 68
def sendDocument to, path, options={}
  self.post("/sendDocument", :document, path, to, options)
end
sendLocation(to, lat, long, options={}) click to toggle source
# File lib/telegramAPI.rb, line 88
def sendLocation to, lat, long, options={}
  self.query("sendLocation", {:chat_id=>to, :latitude=>lat, :longitude=>long}.merge(parse_hash(options)))
end
sendMessage(to, text, options={}) click to toggle source
# File lib/telegramAPI.rb, line 49
def sendMessage to, text, options={}
  if options.has_key?"reply_markup" then
    options["reply_markup"]=options["reply_markup"].to_json
  end
  self.query("sendMessage", {:chat_id=>to.to_s, :text=>text}.merge(parse_hash(options)))
end
sendPhoto(to, path, options={}) click to toggle source
# File lib/telegramAPI.rb, line 60
def sendPhoto to, path, options={}
  self.post("/sendPhoto", :photo, path, to, options)
end
sendSticker(to, id, options={}) click to toggle source
# File lib/telegramAPI.rb, line 76
def sendSticker to, id, options={}
  RestClient.post(@@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(parse_hash(options))).body
end
sendStickerFromFile(to, path, options={}) click to toggle source
# File lib/telegramAPI.rb, line 72
def sendStickerFromFile to, path, options={}
  self.post("/sendSticker", :sticker, path, to, options)
end
sendVenue(to, lat, long, title, address, options={}) click to toggle source
# File lib/telegramAPI.rb, line 92
def sendVenue to, lat, long, title, address, options={}
  self.query("sendVenue", {:chat_id=>to, :latitude=>lat, :longitude=>long, :title=>title, :address=>address}.merge(parse_hash(options)))
end
sendVideo(to, path, options={}) click to toggle source
# File lib/telegramAPI.rb, line 80
def sendVideo to, path, options={}
  self.post("/sendVideo", :video, path, to, options)
end
sendVoice(to, path, options={}) click to toggle source
# File lib/telegramAPI.rb, line 84
def sendVoice to, path, options={}
  self.post("/sendVoice", :voice, path, to, options)
end
setWebhook(url) click to toggle source
# File lib/telegramAPI.rb, line 37
def setWebhook url
  self.query("setWebhook", {"url"=>URI::encode(url)})
end
unbanChatMember(chat_id, user_id) click to toggle source
# File lib/telegramAPI.rb, line 121
def unbanChatMember chat_id, user_id
  self.query("unbanChatMember", {:chat_id=>chat_id, :user_id=>user_id})
end

Protected Instance Methods

parse_hash(hash) click to toggle source
# File lib/telegramAPI.rb, line 19
def parse_hash hash
  ret = {}
  hash.map do |k,v|
    ret[k]=URI::encode(v.to_s.gsub("\\\"", "\""))
  end
  return ret
end
post(api, name, path, to, options={}) click to toggle source
# File lib/telegramAPI.rb, line 33
def post api, name, path, to, options={}
  JSON.parse(RestClient.post(@@core+@token+api, {name=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash(options))).body)["result"]
end
query(api, params={}) click to toggle source
# File lib/telegramAPI.rb, line 27
def query api, params={}
  r = JSON.parse(RestClient.post(@@core+@token+"/"+api, params).body)
  if r['result'].class==Array and r['result'][-1]!=nil then @last_update=r['result'][-1]['update_id']+1 end
  return r["result"]
end