rubenlagus / TelegramApi

Java library to create Telegram Clients
MIT License
300 stars 108 forks source link

How to make correct doRpcCall() #47

Closed dbambulaks closed 6 years ago

dbambulaks commented 6 years ago

Is it possible to get some kid of general guidelines on how to compose correct rpc calls? So, for example, how to get your TL contacts list, or contact name/all messages given chat id... Thank you in advance!

sadeghpro commented 6 years ago

hi for sending a request to telegram you can use TLRequest... classes for example for getting contacts I use this:

TLRequestContactsGetContacts contacts = new TLRequestContactsGetContacts();
                contacts.setHash("12");
                TLAbsContacts t;
                try {
                    t = kernel.getKernelComm().getApi().doRpcCall(contacts);
                    System.out.println("start");
                    if (t instanceof TLContacts) {
                        System.out.println("yes");
                        TLVector<TLAbsUser> contact = ((TLContacts) t).getUsers();
                        contact.forEach((c) -> {
                            if (c instanceof TLUser) {
                                TLUser tlUser = (TLUser) c;
                                    System.out.println(tlUser.getPhone());
                                    tlUser.getPhone();
                                    if(tlUser.getPhoto() instanceof TLUserProfilePhoto){
                                        TLUserProfilePhoto photo = (TLUserProfilePhoto) tlUser.getPhoto();
                                    }else{
                                        System.out.println("error: " + tlUser.getPhoto().getClass());
                                    }
//                                User u = new User(us.getId());
//                                u.setUserHash(us.getAccessHash());
//                                try {
//                                    kernel.getKernelComm().sendMessage(u, "salam");
//                                } catch (RpcException ex) {
//                                    Logger.getLogger(Deepthought.class.getName()).log(Level.SEVERE, null, ex);
//                                }
                            }
                        });
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Deepthought.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TimeoutException ex) {
                    Logger.getLogger(Deepthought.class.getName()).log(Level.SEVERE, null, ex);
                }

I write this code some time ago and don't check it again, I hope this help you

dbambulaks commented 6 years ago

Thank you! Already got idea that u need to cast Abs objects to the normal ones :)

chri1389 commented 6 years ago

Hello @sadeghpro!

I have included your code in the Deepthought project, but it blocks on the row ... t = kernel.getKernelComm().getApi().doRpcCall(contacts); ...

without launch an exception or do anythings..

Why?

Thank you very much!

sadeghpro commented 6 years ago

hi @chri1389 I don't know this code in try&catch and if any error happened you must see an exception. maybe some other lines have a bug. I suggest you run code in debugger mode and send me line and error to check it.