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.



14
15
16
17
# File 'lib/telegramAPI.rb', line 14

def initialize token
  @token = token
  @last_update = 0
end

Instance Method Details

#answerInlineQuery(inline_query_id, results) ⇒ Object



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

def answerInlineQuery inline_query_id, results
  self.query("answerInlineQuery", {:inline_query_id=>inline_query_id, :results=>JSON.dump(results)})
end

#forwardMessage(to, from, msg) ⇒ Object



57
58
59
# File 'lib/telegramAPI.rb', line 57

def forwardMessage to, from, msg
  self.query("forwardMessage", {:chat_id=>to, :from_chat_id=>from, :message_id=>msg})
end

#getChat(chat_id) ⇒ Object



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

def getChat chat_id
  self.query("getChat", {:chat_id=>chat_id})
end

#getChatAdministrators(chat_id) ⇒ Object



130
131
132
# File 'lib/telegramAPI.rb', line 130

def getChatAdministrators chat_id
  self.query("getChatAdministrators", {:chat_id=>chat_id})
end

#getChatMember(chat_id, user_id) ⇒ Object



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

def getChatMember chat_id, user_id
  self.query("getChatMember", {:chat_id=>chat_id, :user_id=>user_id})
end

#getChatMembersCount(chat_id) ⇒ Object



134
135
136
# File 'lib/telegramAPI.rb', line 134

def getChatMembersCount chat_id
  self.query("getChatMembersCount", {:chat_id=>chat_id})
end

#getFile(file_id) ⇒ Object



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

def getFile file_id
  self.query("getFile", {:file_id=>file_id})
end

#getMeObject



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

def getMe
  self.query("getMe")
end

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



42
43
44
# File 'lib/telegramAPI.rb', line 42

def getUpdates options={"timeout"=>0, "limit"=>100}
  self.query("getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash(options)))
end

#getUserProfilePhotos(id, options = {}) ⇒ Object



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

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

#kickChatMember(chat_id, user_id) ⇒ Object



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

def kickChatMember chat_id, user_id
  self.query("kickChatMember", {:chat_id=>chat_id, :user_id=>user_id})
end

#leaveChat(chat_id) ⇒ Object



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

def leaveChat chat_id
  self.query("leaveChat", {:chat_id=>chat_id})
end

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



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

def sendAudio to, path, options={}
  self.post("/sendAudio", :audio, path, to, options)
end

#sendChatAction(to, act) ⇒ Object

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



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

def sendChatAction to, act
  self.query("sendChatAction", {:chat_id=>to, :action=>act})
end

#sendContact(to, phone_number, first_name, options = {}) ⇒ Object



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

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 = {}) ⇒ Object



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

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

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



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

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 = {}) ⇒ Object



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

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 = {}) ⇒ Object



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

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

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



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

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 = {}) ⇒ Object



73
74
75
# File 'lib/telegramAPI.rb', line 73

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

#sendVenue(to, lat, long, title, address, options = {}) ⇒ Object



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

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 = {}) ⇒ Object



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

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

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



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

def sendVoice to, path, options={}
  self.post("/sendVoice", :voice, path, to, options)
end

#setWebhook(url) ⇒ Object



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

def setWebhook url
  self.query("setWebhook", {"url"=>URI::encode(url)})
end

#unbanChatMember(chat_id, user_id) ⇒ Object



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

def unbanChatMember chat_id, user_id
  self.query("unbanChatMember", {:chat_id=>chat_id, :user_id=>user_id})
end