rubenlagus / TelegramApi

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

GetMessage History #46

Open olala2288 opened 6 years ago

olala2288 commented 6 years ago

i want to get history of a channel and i keep requesting and it returns null here is my Code is there anybody can help me ? ` public void handleChannelMessage(@NotNull IUser user, @NotNull TLMessage message){ TLMessageFwdHeader tlMessageFwdHeader = message.getFwdFrom();

    int channelId = tlMessageFwdHeader.getChannelId();

    TLRequestMessagesGetHistory tlreqHistory = new TLRequestMessagesGetHistory();
    TLRequestContactsResolveUsername ru = new TLRequestContactsResolveUsername();
    ru.setUsername("OfficialPersianTwitter");

    TLInputPeerChannel peer = new TLInputPeerChannel();
    TLInputChannel channel = new TLInputChannel();

    try {
        TLResolvedPeer tlResolvedPeer = kernelComm.getApi().doRpcCall(ru);
        TLVector<TLAbsChat> v = tlResolvedPeer.getChats();
        long hash = ((TLChannel) tlResolvedPeer.getChats().get(0)).getAccessHash();
        peer.setChannelId((int)channelId);
        peer.setAccessHash(hash);

        tlreqHistory.setPeer(peer);
        tlreqHistory.setLimit(10);

      TLAbsMessages messages =  kernelComm.doRpcCallSyncNoAuth(tlreqHistory);
       //this line returns null

    }  catch (RpcException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

}`

sadeghpro commented 6 years ago

I use this and it's work for me

TLRequestMessagesGetAllChats getAllChats = new TLRequestMessagesGetAllChats();
getAllChats.setExceptIds(new TLIntVector());
                try {
                    TLAbsMessagesChats messagesChats = kernel.getKernelComm().getApi().doRpcCall(getAllChats);
                    TLVector<TLAbsChat> absChats = messagesChats.getChats();
                    absChats.forEach((absChat) -> {
                        if (absChat instanceof TLChannel) {
                            int channelId = absChat.getId();
                            Long channelHash = ((TLChannel) absChat).getAccessHash();
                            String channelUsername = ((TLChannel) absChat).getUsername();
                            TLRequestMessagesGetHistory getHistory = new TLRequestMessagesGetHistory();
                            TLInputPeerChannel channel = new TLInputPeerChannel();
                            channel.setChannelId(absChat.getId());
                            channel.setAccessHash(((TLChannel) absChat).getAccessHash());
                            try {
                                ResultSet result = db.select("SELECT max(messageId) as maxId FROM messages WHERE channelId=" + channelId + ";");
                                if (result.next()) {
                                    if (result.getInt("maxId") > 0) {
                                        getHistory.setMinId(result.getInt("maxId"));
                                    } else {
                                        getHistory.setLimit(1);
                                    }
                                } else {
                                    getHistory.setLimit(1);
                                }
                            } catch (SQLException ex) {
                                Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
                                return;
                            }
                            getHistory.setPeer(channel);
                            try {
                                TLAbsMessages t = kernel.getKernelComm().getApi().doRpcCall(getHistory);
                                TLVector<TLAbsMessage> absMessages = t.getMessages();
                                absMessages.forEach((absMessage) -> {
                                    if (absMessage instanceof TLMessage) {
                                        TLMessage message = (TLMessage) absMessage;
                                    }
                                 }
                             }

I don't test your code but I think you must use kernelComm.doRpcCall(tlreqHistory); not kernelComm.doRpcCallSyncNoAuth(tlreqHistory); the second thing you must test is your phone join the channel you want to get his history

ghost commented 6 years ago

Hi, How do you intercept messages from channels?

I tried to add these codes in MessageHandler class but it's not functioning.

Pls. maybe you can help me. Thank you.