Class: TelegramAPI

Inherits:
Object
  • Object
show all
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

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

#getMeObject



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 options={"timeout"=>0, "limit"=>100}
  r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash(options))
  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

Parameters:

  • id (Integer)

    ID user whom getting the photos

Returns:

  • (UserProfilePhotos)


123
124
125
# File 'lib/telegramAPI.rb', line 123

def getUserProfilePhotos id, options={}
  self.query("getUserProfilePhotos", {"user_id"=>id}.merge(parse_hash(options)))["result"]
end

#kickChatMember(chat_id, user_id) ⇒ Object

Kick the user user_id from the chat chat_id

Parameters:

  • chat_id (Integer or String)

    ID of the chat, or @publicname

  • user_id (Integer)

    ID of the user to kick



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, options={}
  self.post "/sendAudio", :audio, path, to, options
end

#sendChatAction(to, act) ⇒ Object

Send a Chat Action

Parameters:

  • act (String)

    One of: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location



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, options={}
  self.post "/sendDocument", :document, path, to, options
end

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

Send a location

Parameters:

  • lat (Float)

    Latitude

  • long (Float)

    Longitude



108
109
110
# File 'lib/telegramAPI.rb', line 108

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

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



53
54
55
56
57
58
# File 'lib/telegramAPI.rb', line 53

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"=>URI::encode(text)}.merge(parse_hash(options)))["result"]
end

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



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

def sendPhoto to, path, options={}
  self.post "/sendPhoto", :photo, path, to, options
end

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

Send a Sticker through its ID

Parameters:

  • id (Integer)

    the ID of the sticker to send



91
92
93
# File 'lib/telegramAPI.rb', line 91

def sendSticker to, id, options={}
  JSON.parse(RestClient.post(@@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(parse_hash(options))).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, options={}
  self.post "/sendSticker", :sticker, path, to, options
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, options={}
  self.post "/sendVideo", :video, path, to, options
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