Skip to content

Commit

Permalink
updated for tdlib 1.8.8 (probably 1.6.0 and newer); fixed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lattenwald committed Nov 15, 2022
1 parent ec69dff commit 0704304
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 88 deletions.
60 changes: 9 additions & 51 deletions doc/tdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ __Behaviours:__ [`gen_server`](gen_server.md).
## Function Index ##


<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#auth_code-2">auth_code/2</a></td><td>Send authentication code.</td></tr><tr><td valign="top"><a href="#auth_code-3">auth_code/3</a></td><td>Send authentication code for unregistered user.</td></tr><tr><td valign="top"><a href="#auth_code-4">auth_code/4</a></td><td>Send authentication code for unregistered user.</td></tr><tr><td valign="top"><a href="#auth_password-2">auth_password/2</a></td><td>Send password.</td></tr><tr><td valign="top"><a href="#config-2">config/2</a></td><td>Send tdlib configuration.</td></tr><tr><td valign="top"><a href="#execute-2">execute/2</a></td><td>Execute synchronous tdlib request.</td></tr><tr><td valign="top"><a href="#get_auth_state-1">get_auth_state/1</a></td><td>Get current auth state of tdlib.</td></tr><tr><td valign="top"><a href="#get_config-1">get_config/1</a></td><td>Get tdlib configuration.</td></tr><tr><td valign="top"><a href="#get_handlers-1">get_handlers/1</a></td><td>Get list of current handlers.</td></tr><tr><td valign="top"><a href="#phone_number-2">phone_number/2</a></td><td>Send phone number.</td></tr><tr><td valign="top"><a href="#register_handler-2">register_handler/2</a></td><td>Add handler to tdlib instance.</td></tr><tr><td valign="top"><a href="#send-2">send/2</a></td><td>Send tdlib request.</td></tr><tr><td valign="top"><a href="#send_sync-2">send_sync/2</a></td><td>Send tdlib request and block until response is received.</td></tr><tr><td valign="top"><a href="#send_sync-3">send_sync/3</a></td><td>Send tdlib request and block until response is received.</td></tr><tr><td valign="top"><a href="#set_log_file_path-0">set_log_file_path/0</a></td><td>Set log logging behaviour to default.</td></tr><tr><td valign="top"><a href="#set_log_file_path-1">set_log_file_path/1</a></td><td>Set log file path.</td></tr><tr><td valign="top"><a href="#set_log_max_file_size-1">set_log_max_file_size/1</a></td><td>Set max log file size.</td></tr><tr><td valign="top"><a href="#set_log_verbosity_level-1">set_log_verbosity_level/1</a></td><td>Set tdlib log verbosity level.</td></tr><tr><td valign="top"><a href="#start_link-0">start_link/0</a></td><td>Start new tdlib instance.</td></tr><tr><td valign="top"><a href="#start_link-2">start_link/2</a></td><td>Start new tdlib instance, register it and send config when ready.</td></tr></table>
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#auth_code-2">auth_code/2</a></td><td>Send authentication code.</td></tr><tr><td valign="top"><a href="#auth_code-3">auth_code/3</a></td><td>Send authentication code for unregistered user.</td></tr><tr><td valign="top"><a href="#auth_code-4">auth_code/4</a></td><td>Send authentication code for unregistered user.</td></tr><tr><td valign="top"><a href="#auth_password-2">auth_password/2</a></td><td>Send password.</td></tr><tr><td valign="top"><a href="#config-2">config/2</a></td><td>Send tdlib configuration.</td></tr><tr><td valign="top"><a href="#execute-2">execute/2</a></td><td>Execute synchronous tdlib request.</td></tr><tr><td valign="top"><a href="#get_auth_state-1">get_auth_state/1</a></td><td>Get current auth state of tdlib.</td></tr><tr><td valign="top"><a href="#get_config-1">get_config/1</a></td><td>Get tdlib configuration.</td></tr><tr><td valign="top"><a href="#get_handlers-1">get_handlers/1</a></td><td>Get list of current handlers.</td></tr><tr><td valign="top"><a href="#get_version-1">get_version/1</a></td><td>Get tdlib version.</td></tr><tr><td valign="top"><a href="#phone_number-2">phone_number/2</a></td><td>Send phone number.</td></tr><tr><td valign="top"><a href="#register_handler-2">register_handler/2</a></td><td>Add handler to tdlib instance.</td></tr><tr><td valign="top"><a href="#send-2">send/2</a></td><td>Send tdlib request.</td></tr><tr><td valign="top"><a href="#send_sync-2">send_sync/2</a></td><td>Send tdlib request and block until response is received.</td></tr><tr><td valign="top"><a href="#send_sync-3">send_sync/3</a></td><td>Send tdlib request and block until response is received.</td></tr><tr><td valign="top"><a href="#start_link-0">start_link/0</a></td><td>Start new tdlib instance.</td></tr><tr><td valign="top"><a href="#start_link-2">start_link/2</a></td><td>Start new tdlib instance, register it and send config when ready.</td></tr></table>


<a name="functions"></a>
Expand Down Expand Up @@ -90,6 +90,14 @@ Get tdlib configuration.

Get list of current handlers.

<a name="get_version-1"></a>

### get_version/1 ###

`get_version(Pid) -> any()`

Get tdlib version.

<a name="phone_number-2"></a>

### phone_number/2 ###
Expand Down Expand Up @@ -135,56 +143,6 @@ Send tdlib request and block until response is received.
Inside it's a `gen_server:call`, so expect
`{timeout, _}` error if you need to handle this.

<a name="set_log_file_path-0"></a>

### set_log_file_path/0 ###

`set_log_file_path() -> any()`

Set log logging behaviour to default.

By default TDLib writes logs to stderr or an OS specific log. Use this
method restore such behaviour.

__See also:__ [set_log_file_path/1](#set_log_file_path-1).

<a name="set_log_file_path-1"></a>

### set_log_file_path/1 ###

`set_log_file_path(Path) -> any()`

Set log file path.

Sets the path to the file where the internal TDLib log will be written.
By default TDLib writes logs to stderr or an OS specific log. Use this
method to write the log to a file instead.

__See also:__ [set_log_file_path/0](#set_log_file_path-0).

<a name="set_log_max_file_size-1"></a>

### set_log_max_file_size/1 ###

`set_log_max_file_size(Size) -> any()`

Set max log file size.

Sets maximum size of the file to where the internal TDLib log is
written before the file will be auto-rotated. Unused if log is not
written to a file. Defaults to 10 MB.

<a name="set_log_verbosity_level-1"></a>

### set_log_verbosity_level/1 ###

`set_log_verbosity_level(Level) -> any()`

Set tdlib log verbosity level.

Sets the verbosity level of the internal logging of TDLib. By default
the TDLib uses a log verbosity level of 5.

<a name="start_link-0"></a>

### start_link/0 ###
Expand Down
20 changes: 1 addition & 19 deletions doc/tdlib_nif.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Function Index ##


<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#execute-2">execute/2</a></td><td></td></tr><tr><td valign="top"><a href="#new-0">new/0</a></td><td></td></tr><tr><td valign="top"><a href="#recv-2">recv/2</a></td><td></td></tr><tr><td valign="top"><a href="#send-2">send/2</a></td><td></td></tr><tr><td valign="top"><a href="#set_log_file_path-1">set_log_file_path/1</a></td><td></td></tr><tr><td valign="top"><a href="#set_log_max_file_size-1">set_log_max_file_size/1</a></td><td></td></tr><tr><td valign="top"><a href="#set_log_verbosity_level-1">set_log_verbosity_level/1</a></td><td></td></tr></table>
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#execute-2">execute/2</a></td><td></td></tr><tr><td valign="top"><a href="#new-0">new/0</a></td><td></td></tr><tr><td valign="top"><a href="#recv-2">recv/2</a></td><td></td></tr><tr><td valign="top"><a href="#send-2">send/2</a></td><td></td></tr></table>


<a name="functions"></a>
Expand Down Expand Up @@ -40,21 +40,3 @@

`send(X1, X2) -> any()`

<a name="set_log_file_path-1"></a>

### set_log_file_path/1 ###

`set_log_file_path(X1) -> any()`

<a name="set_log_max_file_size-1"></a>

### set_log_max_file_size/1 ###

`set_log_max_file_size(X1) -> any()`

<a name="set_log_verbosity_level-1"></a>

### set_log_verbosity_level/1 ###

`set_log_verbosity_level(X1) -> any()`

69 changes: 51 additions & 18 deletions src/tdlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,8 @@ register_handler(Pid, Handler) ->
%%====================================================================
config(Pid, Cfg) ->
try
case lists:keyfind(api_id, 1, Cfg) of
false -> throw({missing_param, api_id});
_ -> ok
end,

case lists:keyfind(api_hash, 1, Cfg) of
false -> throw({missing_param, api_hash});
_ -> ok
end,

case lists:keyfind(database_directory, 1, Cfg) of
false -> throw({missing_param, database_directory});
_ -> ok
end,

gen_server:call(Pid, {config, Cfg})
Config = create_config(Cfg),
gen_server:call(Pid, {config, Config})
catch
_:Err = {missing_param, _} ->
{error, Err}
Expand Down Expand Up @@ -346,7 +332,7 @@ handle_call(
Cfg
),

Request = method(setTdlibParameters, [{parameters, Config}]),
Request = method(setTdlibParameters, Config),

send(self(), Request),

Expand Down Expand Up @@ -436,7 +422,9 @@ terminate(_, _) -> ok.
%%====================================================================

%% @private
method(Type, Params) ->
method(Type, Params) when is_map(Params) ->
Params#{'@type' => Type};
method(Type, Params) when is_list(Params) ->
[{'@type', Type} | Params].

%% @private
Expand Down Expand Up @@ -481,3 +469,48 @@ update_option(State, <<"commit_hash">>, Commit) ->
State#state{tdlib_commit = Commit};
update_option(State, _, _) ->
State.

%% @private
create_config(null) ->
null;
create_config(Cfg) when is_map(Cfg) ->
create_config(maps:to_list(Cfg));
create_config(Cfg) ->
DefaultConfig = [
{<<"application_version">>, <<"Unknown">>},
{<<"device_model">>, <<"Unknown">>},
{<<"enable_storage_optimizer">>, true},
{<<"files_directory">>, null},
{<<"ignore_file_names">>, true},
{<<"system_language_code">>, <<"en">>},
{<<"system_version">>, null},
{<<"use_chat_info_database">>, true},
{<<"use_file_database">>, true},
{<<"use_message_database">>, true},
{<<"use_secret_chats">>, false},
{<<"use_test_dc">>, false}
],

Config = lists:foldl(
fun({Key, Val}, Acc) ->
Key_ =
case Key of
_ when is_atom(Key) -> atom_to_binary(Key, utf8);
_ when is_list(Key) -> list_to_binary(Key);
_ when is_binary(Key) -> Key
end,
lists:keystore(Key_, 1, Acc, {Key_, Val})
end,
DefaultConfig,
Cfg
),

lists:foreach(
fun(Name) ->
case lists:keyfind(Name, 1, Config) of
false -> throw({missing_param, api_id});
_ -> ok
end
end, [<<"api_id">>, <<"api_hash">>, <<"database_directory">>]),

Config.

0 comments on commit 0704304

Please sign in to comment.