TelegramApi icon indicating copy to clipboard operation
TelegramApi copied to clipboard

reading all channel messages

Open olala2288 opened this issue 8 years ago • 2 comments

Hi , I want to write a bot that can read all channel messages that given to. And I need to use telegram API as well. There is a method to get all messages that the method is messages.getHistory(); so in this library I can not find this method can you help me ?

olala2288 avatar Nov 13 '17 15:11 olala2288

I write a code and run it with crontab every 1 hour to get all new message in channels, I hope this get you an idea for your work.

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;
                                        String messageText = "";
                                        if (!message.isForwarded()) {
                                            Calendar calendar = Calendar.getInstance();
                                            calendar.setTime(new Date(message.getDate() * 1000L));
                                            int hours = calendar.get(Calendar.HOUR_OF_DAY);
                                            int timeType = 0;
                                            if (hours >= 9 && hours < 13) {
                                                timeType = 1;
                                            } else if (hours >= 13 && hours < 17) {
                                                timeType = 2;
                                            } else if (hours >= 17 && hours < 21) {
                                                timeType = 3;
                                            } else if ((hours >= 21 && hours < 25) || (hours >= 0 && hours < 1)) {
                                                timeType = 4;
                                            }
                                            if (timeType != 0) {
                                                String query = "INSERT INTO messages "
                                                        + "(channelId,channelHash,channelUsername,messageId,messageDate,timeType)"
                                                        + " VALUES(" + channelId + "," + channelHash + ",'" + channelUsername + "',"
                                                        + message.getId() + "," + message.getDate() + "," + timeType + ");";
                                                try {
                                                    db.execute(query);
                                                } catch (SQLException ex) {
                                                    Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
                                                }
                                            }
                                        } else {
                                            System.out.println("it's forward");
                                        }

                                    }
                                });
                            } catch (IOException | TimeoutException ex) {
                                Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            System.out.println(((TLChannel) absChat).getUsername() + " its end");
                        }
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    });
                } catch (IOException | TimeoutException ex) {
                    Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
                }

sadeghpro avatar Jan 28 '18 05:01 sadeghpro

Hi where do you add this method? in MessageHandler class or in CustomUpdatesHandler?

ghost avatar Nov 23 '18 01:11 ghost