Class: Telegram::API

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/api.rb

Overview

Note:

You must avoid doing direct calls or initializes

Telegram API Implementation

See Also:

Version:

  • 0.1.0

Direct Known Subclasses

Client

Instance Method Summary collapse

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

Examples:

telegram.chat_add_user('chat#1234567', 'user#1234567') do |success, data|
  puts success # => true
  puts data # => {"event": "service", ...}
end

Parameters:

  • chat (String)

    Target chat group to add a user

  • user (String)

    User who would be added

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.0


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)

Examples:

telegram.chat_del_user('chat#1234567', 'user#1234567') do |success, data|
  puts success # => true
  puts data # => {"event": "service", ...}
end

Parameters:

  • chat (String)

    Target chat group to remove a user

  • user (String)

    User who would be removed from the chat

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.0


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

Parameters:

  • type (type)

    The type of an attachment (:photo, :video, :audio)

  • seq (String)

    Message sequence number

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.1


305
306
307
308
309
# File 'lib/telegram/api.rb', line 305

def download_attachment(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

Examples:

telegram.mark_read('user#1234567')

Parameters:

  • target (String)

    Target to mark read messages



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

Examples:

telegram.msg('user#1234567', 'hello!') do |success, data|
  puts success # => true
  puts data # => {"event": "message", "out": true, ...}
end

Parameters:

  • target (String)

    Target to send a message

  • text (String)

    Message content to be sent

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The data of the request

Since:

  • 0.1.0


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

Examples:

telegram.send_contact('chat#1234567', '9329232332', 'Foo', 'Bar')

Parameters:

  • peer (String)

    Target chat to which contact will be send

  • contact (String)

    phone number

  • contact (String)

    first name

  • contact (String)

    last name



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

Examples:

telegram.send_file('chat#1234567', file_path) do |success, data|
  puts "there was a problem during the sending" unless success
  puts success # => true
  puts data # => {"event": "message", "media": {"type": "document", ...}, ...}
end

Parameters:

  • chat (String)

    Target chat group to send a file

  • path (String)

    The path of the file you want to send

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request



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

Examples:

telegram.send_photo('chat#1234567') do |success, data|
  puts "there was a problem during the sending" unless success
  puts success # => true
  puts data # => {"event": "message", "media": {"type": "photo", ...}, ...}
end

Parameters:

  • chat (String)

    Target chat group to send a photo

  • path (String)

    The path of the image you want to send

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.1


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

Examples:

telegram.send_typing('chat#1234567') do |success, data|
  puts success # => true
  puts data # => {"result": "SUCCESS"}
end

Parameters:

  • chat (String)

    Target chat group to send typing signal

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.1


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

Examples:

telegram.send_typing_abort('chat#1234567') do |success, data|
  puts success # => true
  puts data # => {"result": "SUCCESS"}
end

Parameters:

  • chat (String)

    Target chat group to stop sending typing signal

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.1


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

Examples:

telegram.send_photo('chat#1234567') do |success, data|
  puts "there was a problem during the sending" unless success
  puts success # => true
  puts data # => {"event": "message", "media": {"type": "video", ...}, ...}
end

Parameters:

  • chat (String)

    Target chat group to send a video

  • path (String)

    The path of the video you want to send

  • callback (Block)

    Callback block that will be called when finished

Yield Parameters:

  • success (Bool)

    The result of the request (true or false)

  • data (Hash)

    The raw data of the request

Since:

  • 0.1.1


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