tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
6.99k stars 1.43k forks source link

tdweb usage #602

Closed telion2 closed 5 years ago

telion2 commented 5 years ago

Hello, I just started with tdlib and wanted to create a td Client for angular (so in the browser) using tdweb. I however am having trouble to use it. So the steps I did up till now is the following -Installed tdweb via npm install --save tdweb -Imported tdweb like this: import * as TDClient from 'tdweb'; -tried this as well: import {TDClient} from 'tdweb'; Not sure if I did it correctly till now, but I can't access the functions and creating it by constructor doesnt work either.

What I expected would be something like this:

import {TDClient} from 'tdweb';
...
createClient(options: object){
   let client = TDClient.init(options); 
   client.someTDLibQuery(parameters); 
}

Alternatively is it possible to use tdweb via a cdn link?

morojenoe commented 5 years ago

Hello, it exported as default, therefore you can import it as:

import TdClient from 'tdweb';
...
createClient(options: object){
   let client = new TdClient(options); 
   client.someTDLibQuery(parameters);
}

Note that you need to create an instance of TdClient class.

telion2 commented 5 years ago

Thank you for your response. I finally made it work. The problem was that tdweb didnt have any typings so I had to include a .d.ts file with declare module 'tdweb'; so that TS is importing it properly. (https://stackoverflow.com/questions/41292559/could-not-find-a-declaration-file-for-module-module-name-path-to-module-nam])

I also assumed that tdweb is using the JSON query format like this:

query = {
      _: 'getChats',
      offset_order: '9223372036854775807',
      offset_chat_id: 0,
      limit: 100
    }

for client.send(query); Now I just need to understand the responses of TDLib.
I guess I'm supposed to use it like this:

client.send({
      _: 'getChats',
      offset_order: '9223372036854775807',
      offset_chat_id: 0,
      limit: 100
    }).then((response: any) => {
            doSomethingWithResponse();
            console.log('Response'+JSON.stringify(response))})
      .catch((err: any)=> {
            console.log('ErrResponse'+JSON.stringify(err))
});

I unfortunately dont get the console.logs but that might be my mistake. If that is working I guess, everything is ready to go, and I need to look up the official API here: https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_function.html

arseny30 commented 5 years ago

At least it should be '@type': 'getChats' instead of _: 'getChats'

levlam commented 5 years ago

@telion2 Also see https://core.telegram.org/tdlib/getting-started for a brief introduction to TDLib usage.

isopen commented 5 years ago

@telion2 TDLib main events are asynchronous. It may be necessary to accept responses in a more general handler. For example through the listener td_json_client_receive.

telion2 commented 5 years ago

I have got an Error where the file '5bf22bc6b3c8aab8a6e3.worker.js' couldnt be loaded because a conflicting mime-type. I am not sure where that error really comes from. Initially, I thought it comes from angular, so asked stackoverflow (https://stackoverflow.com/questions/56751587/how-to-make-sure-that-a-3rd-party-node-module-not-fails-to-require-a-file-due-to). Do you have an idea of what the origin of the error might be?

@isopen In your source code there is this callback function 'onUpdate' that I need to provide to get responses. So I thought tdweb is doing that for me, I couldnt test that yet however due to the loading issue. Does the td_json_client_receive call work the same way like the other functions? send({@type: td_json_client_receive, etc.})?

levlam commented 5 years ago

@telion2 You don't need to call td_json_client_receive directly, this is done by tdweb. You need to use only TdClient and TdClient.send.

isopen commented 5 years ago

@telion2 td_json_client_receive separate interface function. Perhaps this feature is already implemented in tdweb. In this case, it is better to try the tdweb wrapper. I did not meet td_json_client_receive in the interface scheme.

@levlam How to run a global handler? (in which we can collect all answers from TdClient.send)

isopen commented 5 years ago

@telion2 Eg in "Hellow, world!" style: https://github.com/evgeny-nadymov/telegram-react

esindger commented 5 years ago

@telion2, check out strict typed tdweb-airgram. There are examples for rollup and webpack.

isopen commented 4 years ago
import TDClient from 'tdweb/dist/tdweb';
...
let client = new TDClient(options);