Skip to content

OCaml implementation of the MTProto protocol and TL binary data serialization format. 🚧

License

Notifications You must be signed in to change notification settings

eilvelia/camlproto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Camlproto   CI

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

Usage

Example to use the library with Telegram:

open Camlproto

module T = TLSchema.Telegram

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

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

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%lwt () = Client.init t (Telegram.Settings.create ~api_id ()) in
  let%lwt TL_auth_sentCode { phone_code_hash; _ } =
    Client.invoke t (module T.TL_auth_sendCode) {
      phone_number;
      api_id;
      api_hash;
      settings = TL_codeSettings {
        allow_flashcall = None;
        current_number = None;
        allow_app_hash = None;
        allow_missed_call = None;
        logout_tokens = None;
      }
    } in
  let%lwt phone_code = prompt "Enter the code: " in
  let%lwt [@warning "-8"] TL_auth_authorization { user; _ } =
    Client.invoke t (module T.TL_auth_signIn) {
      phone_number;
      phone_code_hash;
      phone_code;
    } in
  let TL_user { id; _ } | TL_userEmpty { id } = user in
  print_endline ("Signed as " ^ Int64.to_string id);
  Lwt.return_unit

let _ = Lwt_main.run (main ())

(see examples/e02_telegram/ and examples/e01_mtproto/)

TL <-> OCaml mapping

TL OCaml
int int
nat (#) int32
long int64
string string
double float
int128 Cstruct.t
int256 Cstruct.t
bytes Cstruct.t
Bool bool
vector a 'a list
Conditional definitions 'a option

"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

About

OCaml implementation of the MTProto protocol and TL binary data serialization format. 🚧

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages