~fnux/telegram-tl-elixir

ref: a41fd5de39256374508294282dfd50702e9584c9 telegram-tl-elixir/lib/tl.ex -rw-r--r-- 2.7 KiB
a41fd5de — Timothée Floure Update documentation for flags + bump version to 0.2.0-beta 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
defmodule TL do
  @moduledoc """
  Telegram TL : base module.
  """

  @doc """
  Build a TL object. See
  [core.telegram.org/schema/mtproto](https://core.telegram.org/schema/mtproto)
  and [core.telegram.org/schema](https://core.telegram.org/schema).

  **About flags :** you must provide the index of the enabled flags in the `flags`
  field. Except for `flags.X?true`, any enabled flag must be provided with a value.

  ## Example

      iex> TL.build "ping", %{:ping_id => 666}
      <<236, 119, 190, 122, 154, 2, 0, 0, 0, 0, 0, 0>>

      # Flags, example for :
      # auth.sendCode#86aef0ec flags:# allow_flashcall:flags.0?true phone_number:string current_number:flags.0?Bool api_id:int api_hash:string = auth.SentCode;
      # Here flags with index '0' ([0]) are enabled. If you want to enable
      # flags with index 0, 2 or 4 : [0,2,4].
      iex> TL.build("auth.sendCode", %{flags: [0], phone_number: "0041763332222", sms_type: 0, api_id: 1234, api_hash: "hashashash", lang_code: "en", current_number: "BoolFalse"})
      <<236, 240, 174, 134, 1, 0, 0, 0, 13, 48, 48, 52, 49, 55, 54, 51, 51, 51, 50,
      50, 50, 50, 0, 0, 66, 111, 111, 108, 70, 97, 108, 115, 101, 210, 4, 0, 0, 10,
      104, 97, 115, 104, 97, 115, 104, 97, 115, 104, 0>>
  """
  def build(container, content), do: TL.Build.encode(container, content)

  @doc """
  Decode a TL object.  See
  [core.telegram.org/schema/mtproto](https://core.telegram.org/schema/mtproto)
  and [core.telegram.org/schema](https://core.telegram.org/schema).

  ## Examples

      iex> TL.parse 0x7abe77ec, <<154, 2, 0, 0, 0, 0, 0, 0>>
      {%{name: "ping", ping_id: 666}, ""}

      iex> TL.parse 0x7abe77ec, <<154, 2, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4>>
      {%{name: "ping", ping_id: 666}, <<1, 2, 3, 4>>}
  """
  def parse(container, content), do: TL.Parse.decode(container, content)

  @doc """
  Serialize an object given its type. Available types :

    * `:int`
    * `:int64`
    * `:int128`
    * `:int256`
    * `:long`
    * `:float`
    * `:string`
    * `:bytes`
    * `:vector`

  ## Examples

      iex> TL.serialize(123456789, :int128)
      <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 91, 205, 21>>

      iex> TL.serialize("Hello world!", :string)
      <<12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 0, 0, 0>>
  """
  def serialize(data, type), do: TL.Build.serialize(data, type)

  @doc """
    Deserialize an object given its type. Available types :

    * `:int`
    * `:int64`
    * `:int128`
    * `:int256`
    * `:long`
    * `:float`
    * `:string`
    * `:bytes`
    * `:vector`
    * `:boxed`

  ## Example

      iex(12)> TL.deserialize(<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 91, 205, 21>>, :int128)
      123456789
  """
  def deserialize(data, type), do: TL.Parse.deserialize(data, type)
end