0.0.0 • Published 5 years ago

camlproto v0.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Camlproto

Portable and type-safe client implementation of Telegram's MTProto protocol and TL data serialization format.

Usage

From OCaml

open Camlproto

module TLT = TLGen.Telegram

let prompt str = Lwt_io.(let%lwt () = write stdout str in read_line stdin)

module Client = Telegram.Client.Make(PlatformCaml)(TransportTcpFullCaml)

let main () =
  (* api_id and api_hash can be obtained at https://my.telegram.org/ *)
  let%lwt phone_number = prompt "Enter your phone number: " in
  let%lwt api_id = prompt "Enter your api id: " in
  let api_id = int_of_string api_id in
  let%lwt api_hash = prompt "Enter your api hash: " in

  let%lwt t = Client.create () in

  let promise =
    let%lwt () = Client.init (Telegram.Settings.create ~api_id ()) t in
    let%lwt C_auth_sentCode { phone_code_hash; _ } =
      Client.invoke t (module TLT.C_auth_sendCode) {
        allow_flashcall = None;
        phone_number;
        current_number = None;
        api_id;
        api_hash;
      } in
    let%lwt phone_code = prompt "Enter phone code: " in
    let%lwt C_auth_authorization { user; _ } =
      Client.invoke t (module TLT.C_auth_signIn) {
        phone_number;
        phone_code_hash;
        phone_code;
      } in
    let (C_user { id; _ } | C_userEmpty { id }) = user in
    print_endline ("Signed as " ^ string_of_int id);
    Lwt.return_unit
  in

  Lwt.pick [promise; Client.loop t]

let _ = Lwt_main.run (main ())

(see examples/ex2/ and examples/ex1/)

From JavaScript

wip

TL <-> OCaml <-> JS mapping

Builtins

TLOCamlJavaScript
intintnumber
nat (#)int32number
longint64string
stringstringstring
doublefloatnumber
int128Cstruct.tUint8Array
int256Cstruct.tUint8Array
bytesCstruct.tUint8Array

Non-builtins

TLOCamlJavaScript
Boolboolboolean
truedefaulttrue
vector a'a listArray<A>
Nullskippedskipped

Other

TLOCamlJavaScript
Conditional definitions'a optionA \| undefined

Transport components

Implemented

  • tcp_full (ocaml, node.js)

  • tcp_abridged (ocaml)

In progress

  • tcp_abridged (node.js)

Not implemented

  • websocket secure (browser)

  • tcp_intermediate

  • tcp_obfuscated2

  • http

  • https

  • udp

Build

Note: Node.js v6.0+ is also required.

Codegen:

cd codegen
npm install
npm run build
npm run codegen
cd ..

Compile the OCaml code:

dune build

Run tests:

dune runtest
0.0.0

5 years ago