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

Create a new instance of TelegramAPI

Parameters:

  • token (String)

    the access token, obtained thanks to @BotFather on Telegram.



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

Parameters:

  • from (Integer)

    chat_id of the original message.

  • msg (Integer)

    The message_id of the original message

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

Returns:

  • (Message)

    Message with the Photo sent



80
81
82
# File 'lib/telegramAPI.rb', line 80

def forwardMessage to, from, msg
  Message.new self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})["result"]
end

#getMeUser

Provide information about the bot itself

Returns:

  • (User)

    Information about the bot



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

def getMe
  User.new self.query("getMe")["result"]
end

#getUpdates(options = {"timeout"=>0, "limit"=>100}) ⇒ Array<Update>

Get last updates, including last received messages

Parameters:

  • options (Hash<String, String>) (defaults to: {"timeout"=>0, "limit"=>100})

    Optional settings

Returns:

  • (Array<Update>)

    List of all updates



55
56
57
58
59
60
61
# File 'lib/telegramAPI.rb', line 55

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
  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

Parameters:

  • id (Integer)

    ID user whom getting the photos

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:



151
152
153
# File 'lib/telegramAPI.rb', line 151

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

#sendAudio(to, path, options = {}) ⇒ Message

Send an audio file in Ogg OPUS format of max 50MB

Parameters:

  • path (String)

    The path of the file to send

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



96
97
98
# File 'lib/telegramAPI.rb', line 96

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

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id



143
144
145
# File 'lib/telegramAPI.rb', line 143

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)

Parameters:

  • path (String)

    The path of the file to send

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



103
104
105
# File 'lib/telegramAPI.rb', line 103

def sendDocument to, path, options={}
  self.post "/sendDocument", :document, path, to, options
end

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

Send a location

Parameters:

  • lat (Float)

    Latitude

  • long (Float)

    Longitude

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



136
137
138
# File 'lib/telegramAPI.rb', line 136

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

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

Send a message to the user with id to, with the text text

Parameters:

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • text (String)

    The text to send

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



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

def sendMessage to, text, options={}
  if options.has_key?"reply_markup" then
    options["reply_markup"]=options["reply_markup"].to_json
  end
  Message.new self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(parse_hash(options)))["result"]
end

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

Send a local file containing a photo

Parameters:

  • path (String)

    The path of the file to send

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



89
90
91
# File 'lib/telegramAPI.rb', line 89

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

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

Send a Sticker through its ID

Parameters:

  • id (Integer)

    the ID of the sticker to send

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



119
120
121
# File 'lib/telegramAPI.rb', line 119

def sendSticker to, id, options={}
  Message.new 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 = {}) ⇒ Message

Send a Sticker from File

Parameters:

  • path (String)

    The path of the file to send

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



110
111
112
# File 'lib/telegramAPI.rb', line 110

def sendStickerFromFile to, path, options={}
  self.post "/sendSticker", :sticker, path, to, options
end

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

Send a video file in mp4 format of max 50MB

Parameters:

  • path (String)

    The path of the file to send

  • to (Integer)

    chat_id to which send the message. Usually message.chat.id

  • options (Hash<String, String>) (defaults to: {})

    Optional settings

Returns:

  • (Message)

    Message with the Photo sent



126
127
128
# File 'lib/telegramAPI.rb', line 126

def sendVideo to, path, options={}
  self.post "/sendVideo", :video, path, to, options
end