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



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

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



44
45
46
# File 'lib/telegramAPI.rb', line 44

def getMe
  User.new self.query("getMe")
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



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

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:



147
148
149
# File 'lib/telegramAPI.rb', line 147

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



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

def sendAudio to, path, options={}
  Message.new JSON.parse(RestClient.post(@@core+@token+"/sendAudio", {:audio=>File.new(path, 'rb'), :chat_id=>to.to_s}.merge(parse_hash(options))).body)["result"]
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



139
140
141
# File 'lib/telegramAPI.rb', line 139

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



99
100
101
# File 'lib/telegramAPI.rb', line 99

def sendDocument to, path, options={}
  Message.new JSON.parse(RestClient.post(@@core+@token+"/sendDocument", {:document=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash(options))).body)["result"]
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



132
133
134
# File 'lib/telegramAPI.rb', line 132

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



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

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



85
86
87
# File 'lib/telegramAPI.rb', line 85

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(parse_hash(options))).body)["result"]
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



115
116
117
# File 'lib/telegramAPI.rb', line 115

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



106
107
108
# File 'lib/telegramAPI.rb', line 106

def sendStickerFromFile to, path, options={}
  Message.new JSON.parse(RestClient.post(@@core+@token+"/sendStiker", {:sticker=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash(options))).body)["result"]
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



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

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