Class: Telegram::API
- Inherits:
-
Object
- Object
- Telegram::API
- Defined in:
- lib/telegram/api.rb
Overview
You must avoid doing direct calls or initializes
Telegram API Implementation
Direct Known Subclasses
Instance Method Summary collapse
- #add_contact(phone_number, first_name, last_name, &callback) ⇒ Object
-
#chat_add_user(chat, user, &callback) {|success, data| ... } ⇒ Object
Add a user to the chat group.
-
#chat_del_user(chat, user, &callback) {|success, data| ... } ⇒ Object
Remove a user from the chat group You can leave a group by this method (Set a user identifier to your identifier).
- #create_group_chat(chat_topic, *users, &callback) ⇒ Object
-
#disconnect(&callback) ⇒ Object
Closes the telegram CLI app (used in case of app shutdown to kill the child process).
-
#download_attachment(type, seq, &callback) {|success, data| ... } ⇒ Object
Download an attachment from a message.
-
#mark_read(target) ⇒ Object
Mark as read all received messages with specific user.
-
#msg(target, text) {|success, data| ... } ⇒ Object
Send a message to specific user or chat.
-
#send_contact(peer, phone, first_name, last_name) ⇒ Object
Send contact to peer chat.
-
#send_file(chat, path, &callback) {|success, data| ... } ⇒ Object
Send a file to the chat.
-
#send_photo(chat, path, &callback) {|success, data| ... } ⇒ Object
Send a photo to the chat.
-
#send_typing(chat, &callback) {|success, data| ... } ⇒ Object
Send typing signal to the chat.
-
#send_typing_abort(chat, &callback) {|success, data| ... } ⇒ Object
Abort sendign typing signal.
-
#send_video(chat, path, &callback) {|success, data| ... } ⇒ Object
Send a video to the chat.
-
#update!(&cb) ⇒ Object
private
Update user profile, contacts and chats.
-
#update_chats! ⇒ Object
private
Update user chats.
-
#update_contacts! ⇒ Object
private
Update user contacts.
-
#update_profile! ⇒ Object
private
Update user profile.
Instance Method Details
#add_contact(phone_number, first_name, last_name, &callback) ⇒ Object
286 287 288 289 |
# File 'lib/telegram/api.rb', line 286 def add_contact(phone_number, first_name, last_name, &callback) assert! @connection.communicate(['add_contact', phone_number, first_name.escape, last_name.escape], &callback) end |
#chat_add_user(chat, user, &callback) {|success, data| ... } ⇒ Object
Add a user to the chat group
153 154 155 156 |
# File 'lib/telegram/api.rb', line 153 def chat_add_user(chat, user, &callback) assert! @connection.communicate(['chat_add_user', chat, user], &callback) end |
#chat_del_user(chat, user, &callback) {|success, data| ... } ⇒ Object
Remove a user from the chat group You can leave a group by this method (Set a user identifier to your identifier)
172 173 174 175 |
# File 'lib/telegram/api.rb', line 172 def chat_del_user(chat, user, &callback) assert! @connection.communicate(['chat_del_user', chat, user], &callback) end |
#create_group_chat(chat_topic, *users, &callback) ⇒ Object
280 281 282 283 284 |
# File 'lib/telegram/api.rb', line 280 def create_group_chat(chat_topic, *users, &callback) assert! members = users.join(" ") @connection.communicate(['create_group_chat', chat_topic.escape, members], &callback) end |
#disconnect(&callback) ⇒ Object
Closes the telegram CLI app (used in case of app shutdown to kill the child process)
292 293 294 295 |
# File 'lib/telegram/api.rb', line 292 def disconnect(&callback) assert! @connection.communicate(['quit'], &callback) end |
#download_attachment(type, seq, &callback) {|success, data| ... } ⇒ Object
Download an attachment from a message
305 306 307 308 309 |
# File 'lib/telegram/api.rb', line 305 def (type, seq, &callback) assert! raise "Type mismatch" unless %w(photo video audio).include?(type) @connection.communicate(["load_#{type.to_s}", seq], &callback) end |
#mark_read(target) ⇒ Object
Mark as read all received messages with specific user
135 136 137 138 |
# File 'lib/telegram/api.rb', line 135 def mark_read(target) assert! @connection.communicate(['mark_read', target]) end |
#msg(target, text) {|success, data| ... } ⇒ Object
Send a message to specific user or chat
125 126 127 128 |
# File 'lib/telegram/api.rb', line 125 def msg(target, text, &callback) assert! @connection.communicate(['msg', target, text], &callback) end |
#send_contact(peer, phone, first_name, last_name) ⇒ Object
Send contact to peer chat
202 203 204 205 |
# File 'lib/telegram/api.rb', line 202 def send_contact(peer, phone, first_name, last_name) assert! @connection.communicate(['send_contact', peer, phone, first_name, last_name]) end |
#send_file(chat, path, &callback) {|success, data| ... } ⇒ Object
Send a file to the chat
275 276 277 278 |
# File 'lib/telegram/api.rb', line 275 def send_file(chat, path, &callback) assert! @connection.communicate(['send_file', chat, path], &callback) end |
#send_photo(chat, path, &callback) {|success, data| ... } ⇒ Object
Send a photo to the chat
238 239 240 241 |
# File 'lib/telegram/api.rb', line 238 def send_photo(chat, path, &callback) assert! @connection.communicate(['send_photo', chat, path], &callback) end |
#send_typing(chat, &callback) {|success, data| ... } ⇒ Object
Send typing signal to the chat
189 190 191 192 |
# File 'lib/telegram/api.rb', line 189 def send_typing(chat, &callback) assert! @connection.communicate(['send_typing', chat], &callback) end |
#send_typing_abort(chat, &callback) {|success, data| ... } ⇒ Object
Abort sendign typing signal
219 220 221 222 |
# File 'lib/telegram/api.rb', line 219 def send_typing_abort(chat, &callback) assert! @connection.communicate(['send_typing_abort', chat], &callback) end |
#send_video(chat, path, &callback) {|success, data| ... } ⇒ Object
Send a video to the chat
257 258 259 260 |
# File 'lib/telegram/api.rb', line 257 def send_video(chat, path, &callback) assert! @connection.communicate(['send_video', chat, path], &callback) end |
#update!(&cb) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Update user profile, contacts and chats
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/telegram/api.rb', line 11 def update!(&cb) done = false EM.synchrony do multi = EM::Synchrony::Multi.new multi.add :profile, update_profile! multi.add :contacts, update_contacts! multi.add :chats, update_chats! multi.perform done = true end check_done = Proc.new { if done @starts_at = Time.now cb.call unless cb.nil? logger.info("Successfully loaded all information") else EM.next_tick(&check_done) end } EM.add_timer(0, &check_done) end |
#update_chats! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Update user chats
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/telegram/api.rb', line 78 def update_chats! assert! callback = Callback.new collected = 0 collect_done = Proc.new do |id, data, count| collected += 1 @chats << TelegramChat.new(self, data) callback.trigger(:success) if collected == count end collect = Proc.new do |id, count| @connection.communicate(['chat_info', "chat\##{id}"]) do |success, data| collect_done.call(id, data, count) if success end end @chats = [] @connection.communicate('dialog_list') do |success, data| if success and data.class == Array chatsize = data.count { |chat| chat['peer_type'] == 'chat' } data.each do |chat| if chat['peer_type'] == 'chat' collect.call(chat['peer_id'], chatsize) elsif chat['peer_type'] == 'user' @chats << TelegramChat.new(self, chat) end end callback.trigger(:success) if chatsize == 0 else raise "Couldn't fetch the dialog(chat) list." end end callback end |
#update_contacts! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Update user contacts
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/telegram/api.rb', line 57 def update_contacts! assert! callback = Callback.new @contacts = [] @connection.communicate('contact_list') do |success, data| if success and data.class == Array callback.trigger(:success) data.each { |contact| contact = TelegramContact.pick_or_new(self, contact) @contacts << contact unless self.contacts.include?(contact) } else raise "Couldn't fetch the contact list." end end callback end |
#update_profile! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Update user profile
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/telegram/api.rb', line 37 def update_profile! assert! callback = Callback.new @profile = nil @connection.communicate('get_self') do |success, data| if success callback.trigger(:success) contact = TelegramContact.pick_or_new(self, data) @contacts << contact unless self.contacts.include?(contact) @profile = contact else raise "Couldn't fetch the user profile." end end callback end |