mtproto 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/FUTURE.md +9 -0
  3. data/docs/test_architecture_level1.md +237 -0
  4. data/lib/mtproto/auth_key_generator.rb +5 -5
  5. data/lib/mtproto/client/api/get_contacts.rb +14 -0
  6. data/lib/mtproto/client/api/send_message.rb +15 -0
  7. data/lib/mtproto/client/api.rb +2 -0
  8. data/lib/mtproto/client/rpc/response.rb +1 -1
  9. data/lib/mtproto/client/rpc.rb +35 -2
  10. data/lib/mtproto/client.rb +81 -18
  11. data/lib/mtproto/tl/constructor_names.rb +10 -0
  12. data/lib/mtproto/tl/constructors.rb +26 -0
  13. data/lib/mtproto/tl/object.rb +1 -1
  14. data/lib/mtproto/tl/objects/account_password.rb +1 -1
  15. data/lib/mtproto/tl/objects/authorization.rb +1 -1
  16. data/lib/mtproto/tl/objects/channels_create_channel.rb +48 -0
  17. data/lib/mtproto/tl/objects/channels_delete_messages.rb +32 -0
  18. data/lib/mtproto/tl/objects/channels_delete_participant_history.rb +34 -0
  19. data/lib/mtproto/tl/objects/channels_edit_banned.rb +54 -0
  20. data/lib/mtproto/tl/objects/channels_get_messages.rb +33 -0
  21. data/lib/mtproto/tl/objects/channels_get_participant.rb +36 -0
  22. data/lib/mtproto/tl/objects/channels_join_channel.rb +22 -0
  23. data/lib/mtproto/tl/objects/channels_leave_channel.rb +22 -0
  24. data/lib/mtproto/tl/objects/channels_report_spam.rb +37 -0
  25. data/lib/mtproto/tl/objects/check_password.rb +1 -1
  26. data/lib/mtproto/tl/objects/client_dh_inner_data.rb +1 -1
  27. data/lib/mtproto/tl/objects/contacts.rb +156 -0
  28. data/lib/mtproto/tl/objects/create_bot.rb +54 -0
  29. data/lib/mtproto/tl/objects/delete_messages.rb +25 -0
  30. data/lib/mtproto/tl/objects/dh_gen_response.rb +1 -1
  31. data/lib/mtproto/tl/objects/dialogs.rb +18 -4
  32. data/lib/mtproto/tl/objects/edit_access_settings.rb +46 -0
  33. data/lib/mtproto/tl/objects/edit_message.rb +58 -0
  34. data/lib/mtproto/tl/objects/export_bot_token.rb +32 -0
  35. data/lib/mtproto/tl/objects/export_login_token.rb +1 -1
  36. data/lib/mtproto/tl/objects/exported_bot_token.rb +27 -0
  37. data/lib/mtproto/tl/objects/forward_messages.rb +55 -0
  38. data/lib/mtproto/tl/objects/get_access_settings.rb +28 -0
  39. data/lib/mtproto/tl/objects/get_channel_difference.rb +41 -0
  40. data/lib/mtproto/tl/objects/get_config.rb +1 -1
  41. data/lib/mtproto/tl/objects/get_contacts.rb +17 -0
  42. data/lib/mtproto/tl/objects/get_dialogs.rb +1 -1
  43. data/lib/mtproto/tl/objects/get_difference.rb +1 -1
  44. data/lib/mtproto/tl/objects/get_file.rb +54 -0
  45. data/lib/mtproto/tl/objects/get_full_channel.rb +29 -0
  46. data/lib/mtproto/tl/objects/get_full_user.rb +28 -0
  47. data/lib/mtproto/tl/objects/get_history.rb +1 -1
  48. data/lib/mtproto/tl/objects/get_messages_reactions.rb +39 -0
  49. data/lib/mtproto/tl/objects/get_password.rb +1 -1
  50. data/lib/mtproto/tl/objects/get_state.rb +1 -1
  51. data/lib/mtproto/tl/objects/get_users.rb +1 -1
  52. data/lib/mtproto/tl/objects/help_config.rb +1 -1
  53. data/lib/mtproto/tl/objects/import_bot_authorization.rb +44 -0
  54. data/lib/mtproto/tl/objects/import_login_token.rb +1 -1
  55. data/lib/mtproto/tl/objects/init_connection.rb +2 -2
  56. data/lib/mtproto/tl/objects/input_keyboard_button_request_peer.rb +54 -0
  57. data/lib/mtproto/tl/objects/invite_to_channel.rb +35 -0
  58. data/lib/mtproto/tl/objects/invoke_with_layer.rb +2 -2
  59. data/lib/mtproto/tl/objects/login_token.rb +2 -2
  60. data/lib/mtproto/tl/objects/messages.rb +1 -1
  61. data/lib/mtproto/tl/objects/messages_get_messages.rb +26 -0
  62. data/lib/mtproto/tl/objects/pq_inner_data.rb +1 -1
  63. data/lib/mtproto/tl/objects/raw_response.rb +29 -0
  64. data/lib/mtproto/tl/objects/reply_keyboard_markup.rb +35 -0
  65. data/lib/mtproto/tl/objects/req_dh_params.rb +1 -1
  66. data/lib/mtproto/tl/objects/req_pq_multi.rb +1 -1
  67. data/lib/mtproto/tl/objects/request_peer_type_create_bot.rb +47 -0
  68. data/lib/mtproto/tl/objects/res_pq.rb +1 -1
  69. data/lib/mtproto/tl/objects/resolve_username.rb +40 -0
  70. data/lib/mtproto/tl/objects/save_file_part.rb +40 -0
  71. data/lib/mtproto/tl/objects/send_bot_requested_peer.rb +51 -0
  72. data/lib/mtproto/tl/objects/send_code.rb +1 -1
  73. data/lib/mtproto/tl/objects/send_media.rb +96 -0
  74. data/lib/mtproto/tl/objects/send_message.rb +73 -0
  75. data/lib/mtproto/tl/objects/send_reaction.rb +68 -0
  76. data/lib/mtproto/tl/objects/sent_code.rb +1 -1
  77. data/lib/mtproto/tl/objects/server_dh_inner_data.rb +1 -1
  78. data/lib/mtproto/tl/objects/server_dh_params.rb +1 -1
  79. data/lib/mtproto/tl/objects/set_bot_commands.rb +50 -0
  80. data/lib/mtproto/tl/objects/set_bot_info.rb +64 -0
  81. data/lib/mtproto/tl/objects/set_client_dh_params.rb +1 -1
  82. data/lib/mtproto/tl/objects/sign_in.rb +1 -1
  83. data/lib/mtproto/tl/objects/update.rb +1 -1
  84. data/lib/mtproto/tl/objects/update_short.rb +2 -2
  85. data/lib/mtproto/tl/objects/update_short_message.rb +1 -1
  86. data/lib/mtproto/tl/objects/update_short_sent_message.rb +36 -0
  87. data/lib/mtproto/tl/objects/update_username.rb +39 -0
  88. data/lib/mtproto/tl/objects/updates_difference.rb +93 -14
  89. data/lib/mtproto/tl/objects/updates_state.rb +1 -1
  90. data/lib/mtproto/tl/objects/upload_profile_photo.rb +65 -0
  91. data/lib/mtproto/tl/objects/users.rb +1 -1
  92. data/lib/mtproto/version.rb +1 -1
  93. metadata +49 -1
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class EditAccessSettings
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0x31813cd8
9
+ INPUT_USER = 0xf21158c6
10
+ INPUT_USER_SELF = 0xf7c1b13f
11
+
12
+ # bot / add_users entries: { id:, access_hash: } or { type: :self }
13
+ def initialize(bot:, restricted: false, add_users: [])
14
+ @bot = bot
15
+ @restricted = restricted
16
+ @add_users = add_users
17
+ end
18
+
19
+ def serialize
20
+ flags = 0
21
+ flags |= (1 << 0) if @restricted
22
+ flags |= (1 << 1) unless @add_users.empty?
23
+
24
+ result = u32_b(CONSTRUCTOR)
25
+ result += u32_b(flags)
26
+ result += serialize_input_user(@bot)
27
+ unless @add_users.empty?
28
+ result += u32_b(Constructors::VECTOR)
29
+ result += u32_b(@add_users.size)
30
+ @add_users.each { |u| result += serialize_input_user(u) }
31
+ end
32
+ result
33
+ end
34
+
35
+ private
36
+
37
+ def serialize_input_user(user)
38
+ if user[:type] == :self
39
+ u32_b(INPUT_USER_SELF)
40
+ else
41
+ u32_b(INPUT_USER) + u64_b(user[:id]) + u64_b(user[:access_hash] || 0)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class EditMessage
6
+ include Binary
7
+
8
+ def initialize(peer:, id:, message:)
9
+ @peer = peer
10
+ @id = id
11
+ @message = message
12
+ end
13
+
14
+ def serialize
15
+ flags = 1 << 11 # message field present
16
+ result = u32_b(Constructors::MESSAGES_EDIT_MESSAGE)
17
+ result += u32_b(flags)
18
+ result += serialize_input_peer
19
+ result += u32_b(@id)
20
+ result += serialize_tl_string(@message)
21
+ result
22
+ end
23
+
24
+ private
25
+
26
+ def serialize_input_peer
27
+ case @peer[:type]
28
+ when :self
29
+ u32_b(Constructors::INPUT_PEER_SELF)
30
+ when :user
31
+ u32_b(Constructors::INPUT_PEER_USER) + u64_b(@peer[:id]) + u64_b(@peer[:access_hash] || 0)
32
+ when :chat
33
+ u32_b(Constructors::INPUT_PEER_CHAT) + u64_b(@peer[:id])
34
+ when :channel
35
+ u32_b(Constructors::INPUT_PEER_CHANNEL) + u64_b(@peer[:id]) + u64_b(@peer[:access_hash] || 0)
36
+ else
37
+ raise "Unknown peer type: #{@peer[:type]}"
38
+ end
39
+ end
40
+
41
+ def serialize_tl_string(str)
42
+ bytes = str.encode('UTF-8').bytes
43
+ length = bytes.length
44
+
45
+ if length <= 253
46
+ [length] + bytes + padding(length + 1)
47
+ else
48
+ [254] + u32_b(length)[0, 3] + bytes + padding(length + 4)
49
+ end
50
+ end
51
+
52
+ def padding(current_length)
53
+ pad_length = (4 - (current_length % 4)) % 4
54
+ [0] * pad_length
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class ExportBotToken
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0xbd0d99eb
9
+ INPUT_USER = 0xf21158c6
10
+ INPUT_USER_SELF = 0xf7c1b13f
11
+ BOOL_TRUE = 0x997275b5
12
+ BOOL_FALSE = 0xbc799737
13
+
14
+ # bot: { id:, access_hash: } or { type: :self }
15
+ def initialize(bot:, revoke: false)
16
+ @bot = bot
17
+ @revoke = revoke
18
+ end
19
+
20
+ def serialize
21
+ result = u32_b(CONSTRUCTOR)
22
+ result += if @bot[:type] == :self
23
+ u32_b(INPUT_USER_SELF)
24
+ else
25
+ u32_b(INPUT_USER) + u64_b(@bot[:id]) + u64_b(@bot[:access_hash] || 0)
26
+ end
27
+ result += u32_b(@revoke ? BOOL_TRUE : BOOL_FALSE)
28
+ result
29
+ end
30
+ end
31
+ end
32
+ end
@@ -13,7 +13,7 @@ module MTProto
13
13
  @except_ids = except_ids
14
14
  end
15
15
 
16
- def body
16
+ def serialize
17
17
  u32_b(Constructors::AUTH_EXPORT_LOGIN_TOKEN) +
18
18
  u32_b(@api_id) +
19
19
  serialize_tl_string(@api_hash) +
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class ExportedBotToken
6
+ CONSTRUCTOR = 0x3c60b621
7
+
8
+ attr_reader :token
9
+
10
+ def initialize(token:)
11
+ @token = token
12
+ end
13
+
14
+ def self.deserialize(bytes)
15
+ body = bytes[4..]
16
+ first = body.getbyte(0)
17
+ token = if first == 254
18
+ len = body.getbyte(1) | (body.getbyte(2) << 8) | (body.getbyte(3) << 16)
19
+ body[4, len]
20
+ else
21
+ body[1, first]
22
+ end
23
+ new(token: token.force_encoding('UTF-8'))
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module MTProto
6
+ module TL
7
+ class ForwardMessages
8
+ include Binary
9
+
10
+ CONSTRUCTOR = 0x978928ca
11
+
12
+ def initialize(from_peer:, to_peer:, ids:, random_ids: nil, drop_author: false, drop_media_captions: false)
13
+ @from_peer = from_peer
14
+ @to_peer = to_peer
15
+ @ids = ids
16
+ @random_ids = random_ids || Array.new(ids.length) { SecureRandom.random_number(2**63) }
17
+ @drop_author = drop_author
18
+ @drop_media_captions = drop_media_captions
19
+ end
20
+
21
+ def serialize
22
+ flags = 0
23
+ flags |= (1 << 11) if @drop_author
24
+ flags |= (1 << 12) if @drop_media_captions
25
+
26
+ result = u32_b(CONSTRUCTOR)
27
+ result += u32_b(flags)
28
+ result += serialize_input_peer(@from_peer)
29
+ result += u32_b(Constructors::VECTOR) + u32_b(@ids.length)
30
+ @ids.each { |id| result += u32_b(id) }
31
+ result += u32_b(Constructors::VECTOR) + u32_b(@random_ids.length)
32
+ @random_ids.each { |rid| result += u64_b(rid) }
33
+ result += serialize_input_peer(@to_peer)
34
+ result
35
+ end
36
+
37
+ private
38
+
39
+ def serialize_input_peer(peer)
40
+ case peer[:type]
41
+ when :self
42
+ u32_b(Constructors::INPUT_PEER_SELF)
43
+ when :user
44
+ u32_b(Constructors::INPUT_PEER_USER) + u64_b(peer[:id]) + u64_b(peer[:access_hash] || 0)
45
+ when :chat
46
+ u32_b(Constructors::INPUT_PEER_CHAT) + u64_b(peer[:id])
47
+ when :channel
48
+ u32_b(Constructors::INPUT_PEER_CHANNEL) + u64_b(peer[:id]) + u64_b(peer[:access_hash] || 0)
49
+ else
50
+ raise "Unknown peer type: #{peer[:type]}"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetAccessSettings
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0x213853a3
9
+ INPUT_USER = 0xf21158c6
10
+ INPUT_USER_SELF = 0xf7c1b13f
11
+
12
+ # bot: { id:, access_hash: } or { type: :self }
13
+ def initialize(bot:)
14
+ @bot = bot
15
+ end
16
+
17
+ def serialize
18
+ result = u32_b(CONSTRUCTOR)
19
+ result += if @bot[:type] == :self
20
+ u32_b(INPUT_USER_SELF)
21
+ else
22
+ u32_b(INPUT_USER) + u64_b(@bot[:id]) + u64_b(@bot[:access_hash] || 0)
23
+ end
24
+ result
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetChannelDifference
6
+ include Binary
7
+
8
+ def initialize(channel:, pts:, limit: 100, force: false)
9
+ @channel = channel
10
+ @pts = pts
11
+ @limit = limit
12
+ @force = force
13
+ end
14
+
15
+ def serialize
16
+ flags = 0
17
+ flags |= (1 << 0) if @force
18
+
19
+ result = u32_b(Constructors::UPDATES_GET_CHANNEL_DIFFERENCE)
20
+ result += u32_b(flags)
21
+ result += serialize_input_channel
22
+ result += u32_b(Constructors::CHANNEL_MESSAGES_FILTER_EMPTY)
23
+ result += u32_b(@pts)
24
+ result += u32_b(@limit)
25
+ result
26
+ end
27
+
28
+ private
29
+
30
+ def serialize_input_channel
31
+ if @channel.nil? || @channel[:type] == :empty
32
+ u32_b(Constructors::INPUT_CHANNEL_EMPTY)
33
+ else
34
+ u32_b(Constructors::INPUT_CHANNEL) +
35
+ u64_b(@channel[:id]) +
36
+ u64_b(@channel[:access_hash])
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -5,7 +5,7 @@ module MTProto
5
5
  class GetConfig
6
6
  include Binary
7
7
 
8
- def body
8
+ def serialize
9
9
  u32_b(Constructors::HELP_GET_CONFIG)
10
10
  end
11
11
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetContacts
6
+ include Binary
7
+
8
+ def initialize(hash: 0)
9
+ @hash = hash
10
+ end
11
+
12
+ def serialize
13
+ u32_b(Constructors::CONTACTS_GET_CONTACTS) + u64_b(@hash)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -16,7 +16,7 @@ module MTProto
16
16
  @folder_id = folder_id
17
17
  end
18
18
 
19
- def body
19
+ def serialize
20
20
  flags = 0
21
21
  flags |= (1 << 0) if @exclude_pinned
22
22
  flags |= (1 << 1) if @folder_id
@@ -16,7 +16,7 @@ module MTProto
16
16
  @qts_limit = qts_limit
17
17
  end
18
18
 
19
- def body
19
+ def serialize
20
20
  flags = 0
21
21
  flags |= (1 << 0) if @pts_limit
22
22
  flags |= (1 << 1) if @pts_total_limit
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetFile
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0xbe5335be
9
+ INPUT_PHOTO_FILE_LOCATION = 0x40181ffe
10
+
11
+ # location: { id:, access_hash:, file_reference: (binary String), thumb_size: (String) }
12
+ def initialize(location:, offset: 0, limit: 1024 * 1024)
13
+ @location = location
14
+ @offset = offset
15
+ @limit = limit
16
+ end
17
+
18
+ def serialize
19
+ result = u32_b(CONSTRUCTOR)
20
+ result += u32_b(0) # flags
21
+ result += serialize_location
22
+ result += u64_b(@offset)
23
+ result += u32_b(@limit)
24
+ result
25
+ end
26
+
27
+ private
28
+
29
+ def serialize_location
30
+ u32_b(INPUT_PHOTO_FILE_LOCATION) +
31
+ u64_b(@location[:id]) +
32
+ u64_b(@location[:access_hash]) +
33
+ serialize_tl_bytes(@location[:file_reference]) +
34
+ serialize_tl_bytes(@location[:thumb_size])
35
+ end
36
+
37
+ def serialize_tl_bytes(value)
38
+ bytes = value.to_s.b.bytes
39
+ length = bytes.length
40
+
41
+ if length <= 253
42
+ [length] + bytes + padding(length + 1)
43
+ else
44
+ [254] + u32_b(length)[0, 3] + bytes + padding(length + 4)
45
+ end
46
+ end
47
+
48
+ def padding(current_length)
49
+ pad_length = (4 - (current_length % 4)) % 4
50
+ [0] * pad_length
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetFullChannel
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0x08736a09
9
+
10
+ def initialize(channel:)
11
+ @channel = channel
12
+ end
13
+
14
+ def serialize
15
+ result = u32_b(CONSTRUCTOR)
16
+ result += serialize_input_channel
17
+ result
18
+ end
19
+
20
+ private
21
+
22
+ def serialize_input_channel
23
+ u32_b(Constructors::INPUT_CHANNEL) +
24
+ u64_b(@channel[:id]) +
25
+ u64_b(@channel[:access_hash])
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetFullUser
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0xb60f5918
9
+ INPUT_USER = 0xf21158c6
10
+ INPUT_USER_SELF = 0xf7c1b13f
11
+
12
+ # user: { id:, access_hash: } or { type: :self }
13
+ def initialize(user:)
14
+ @user = user
15
+ end
16
+
17
+ def serialize
18
+ result = u32_b(CONSTRUCTOR)
19
+ result += if @user[:type] == :self
20
+ u32_b(INPUT_USER_SELF)
21
+ else
22
+ u32_b(INPUT_USER) + u64_b(@user[:id]) + u64_b(@user[:access_hash] || 0)
23
+ end
24
+ result
25
+ end
26
+ end
27
+ end
28
+ end
@@ -17,7 +17,7 @@ module MTProto
17
17
  @min_id = min_id
18
18
  end
19
19
 
20
- def body
20
+ def serialize
21
21
  result = u32_b(CONSTRUCTOR)
22
22
  result += serialize_input_peer
23
23
  result += u32_b(@offset_id)
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class GetMessagesReactions
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0x8bba90e6
9
+
10
+ def initialize(peer:, ids:)
11
+ @peer = peer
12
+ @ids = ids
13
+ end
14
+
15
+ def serialize
16
+ result = u32_b(CONSTRUCTOR)
17
+ result += serialize_input_peer
18
+ result += u32_b(Constructors::VECTOR) + u32_b(@ids.length)
19
+ @ids.each { |id| result += u32_b(id) }
20
+ result
21
+ end
22
+
23
+ private
24
+
25
+ def serialize_input_peer
26
+ case @peer[:type]
27
+ when :channel
28
+ u32_b(Constructors::INPUT_PEER_CHANNEL) + u64_b(@peer[:id]) + u64_b(@peer[:access_hash] || 0)
29
+ when :user
30
+ u32_b(Constructors::INPUT_PEER_USER) + u64_b(@peer[:id]) + u64_b(@peer[:access_hash] || 0)
31
+ when :chat
32
+ u32_b(Constructors::INPUT_PEER_CHAT) + u64_b(@peer[:id])
33
+ else
34
+ raise "Unknown peer type: #{@peer[:type]}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -5,7 +5,7 @@ module MTProto
5
5
  class GetPassword
6
6
  include Binary
7
7
 
8
- def body
8
+ def serialize
9
9
  u32_b(Constructors::ACCOUNT_GET_PASSWORD)
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module MTProto
5
5
  class GetState
6
6
  include Binary
7
7
 
8
- def body
8
+ def serialize
9
9
  u32_b(Constructors::UPDATES_GET_STATE)
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module MTProto
5
5
  class GetUsers
6
6
  include Binary
7
7
 
8
- def body
8
+ def serialize
9
9
  u32_b(Constructors::USERS_GET_USERS) +
10
10
  u32_b(Constructors::VECTOR) +
11
11
  u32_b(1) +
@@ -14,7 +14,7 @@ module MTProto
14
14
  @dc_options = dc_options
15
15
  end
16
16
 
17
- def self.parse(data)
17
+ def self.deserialize(data)
18
18
  constructor = data[0, 4].unpack1('L<')
19
19
  unless [Constructors::CONFIG, Constructors::GZIP_PACKED].include?(constructor)
20
20
  raise UnexpectedConstructorError, constructor
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class ImportBotAuthorization
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0x67a3ff2c
9
+
10
+ def initialize(api_id:, api_hash:, bot_auth_token:)
11
+ @api_id = api_id
12
+ @api_hash = api_hash
13
+ @bot_auth_token = bot_auth_token
14
+ end
15
+
16
+ def serialize
17
+ result = u32_b(CONSTRUCTOR)
18
+ result += u32_b(0) # flags (int, unused)
19
+ result += u32_b(@api_id)
20
+ result += serialize_tl_string(@api_hash)
21
+ result += serialize_tl_string(@bot_auth_token)
22
+ result
23
+ end
24
+
25
+ private
26
+
27
+ def serialize_tl_string(str)
28
+ bytes = str.to_s.b.bytes
29
+ length = bytes.length
30
+
31
+ if length <= 253
32
+ [length] + bytes + padding(length + 1)
33
+ else
34
+ [254] + u32_b(length)[0, 3] + bytes + padding(length + 4)
35
+ end
36
+ end
37
+
38
+ def padding(current_length)
39
+ pad_length = (4 - (current_length % 4)) % 4
40
+ [0] * pad_length
41
+ end
42
+ end
43
+ end
44
+ end
@@ -11,7 +11,7 @@ module MTProto
11
11
  @token = token
12
12
  end
13
13
 
14
- def body
14
+ def serialize
15
15
  u32_b(Constructors::AUTH_IMPORT_LOGIN_TOKEN) + serialize_tl_bytes(@token)
16
16
  end
17
17
 
@@ -20,7 +20,7 @@ module MTProto
20
20
  @query = query
21
21
  end
22
22
 
23
- def body
23
+ def serialize
24
24
  flags = 0
25
25
 
26
26
  result = u32_b(Constructors::INIT_CONNECTION)
@@ -32,7 +32,7 @@ module MTProto
32
32
  result += serialize_tl_string(@system_lang_code)
33
33
  result += serialize_tl_string(@lang_pack)
34
34
  result += serialize_tl_string(@lang_code)
35
- result + @query.body
35
+ result + @query.serialize
36
36
  end
37
37
 
38
38
  private
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class InputKeyboardButtonRequestPeer
6
+ include Binary
7
+
8
+ CONSTRUCTOR = 0x02b78156
9
+
10
+ def initialize(text:, button_id:, peer_type:, max_quantity: 1,
11
+ name_requested: false, username_requested: false, photo_requested: false)
12
+ @text = text
13
+ @button_id = button_id
14
+ @peer_type = peer_type
15
+ @max_quantity = max_quantity
16
+ @name_requested = name_requested
17
+ @username_requested = username_requested
18
+ @photo_requested = photo_requested
19
+ end
20
+
21
+ def serialize
22
+ flags = 0
23
+ flags |= (1 << 0) if @name_requested
24
+ flags |= (1 << 1) if @username_requested
25
+ flags |= (1 << 2) if @photo_requested
26
+
27
+ result = u32_b(CONSTRUCTOR)
28
+ result += u32_b(flags)
29
+ result += serialize_tl_string(@text)
30
+ result += u32_b(@button_id)
31
+ result += @peer_type.serialize
32
+ result += u32_b(@max_quantity)
33
+ result
34
+ end
35
+
36
+ private
37
+
38
+ def serialize_tl_string(str)
39
+ bytes = str.encode('UTF-8').bytes
40
+ length = bytes.length
41
+ if length <= 253
42
+ [length] + bytes + padding(length + 1)
43
+ else
44
+ [254] + u32_b(length)[0, 3] + bytes + padding(length + 4)
45
+ end
46
+ end
47
+
48
+ def padding(current_length)
49
+ pad_length = (4 - (current_length % 4)) % 4
50
+ [0] * pad_length
51
+ end
52
+ end
53
+ end
54
+ end