Class: TelegramAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/telegramAPI.rb

Constant Summary collapse

@@core =
"https://api.telegram.org/bot"

Instance Method Summary collapse

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

#getMeObject



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 options={}
  r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(options)
  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, options={}
  self.query "getUserProfilePhotos", {"user_id"=>id}.merge(options)
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, options={}
  RestClient.post @@core+@token+"/sendAudio", {:audio=>File.new(path, 'rb'), :chat_id=>to.to_s}.merge(options)
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, options={}
  RestClient.post @@core+@token+"/sendDocument", {:document=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)
end

#sendLocation(to, lat, long, options = {}) ⇒ Object



75
76
77
# File 'lib/telegramAPI.rb', line 75

def sendLocation to, lat, long, options={}
  self.query "sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge(options)
end

#sendMessage(to, text, options = {}) ⇒ Object



39
40
41
# File 'lib/telegramAPI.rb', line 39

def sendMessage to, text, options={}
  Message.new self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(options))
end

#sendPhoto(to, path, options = {}) ⇒ Object



47
48
49
# File 'lib/telegramAPI.rb', line 47

def sendPhoto to, path, options={}
  Message.new JSON.parse(RestClient.post(@@core+@token+"/sendPhoto", {:photo=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
end

#sendSticker(to, id, options = {}) ⇒ Object



65
66
67
# File 'lib/telegramAPI.rb', line 65

def sendSticker to, id, options={}
  RestClient.post @@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(options)
end

#sendStickerFromFile(to, path, options = {}) ⇒ Object



61
62
63
# File 'lib/telegramAPI.rb', line 61

def sendStickerFromFile to, path, options={}
  RestClient.post @@core+@token+"/sendStiker", {:sticker=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)
end

#sendVideo(to, path, options = {}) ⇒ Object

Max size: 50MB Fromat: mp4



71
72
73
# File 'lib/telegramAPI.rb', line 71

def sendVideo to, path, options={}
  RestClient.post @@core+@token+"/sendVideo", {:video=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)
end