rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.8k stars 1.23k forks source link

How to get private or group chat messages in v7.10.0? #1447

Open ConnorJohn321 opened 1 month ago

ConnorJohn321 commented 1 month ago

I followed the documentation and implementing SpringLongPollingBotLongPollingSingleThreadUpdateConsumer method. I also wrote a ping method inside consume(List<Update> list){} and consume(Update update){}. However, after startup, while some initial messages are retrieved, no messages are being printed afterward, whether in private chats with the bot or in a group. What should I do to ensure that messages are received in real-time?

@Component public class BotTowServiceImpl implements SpringLongPollingBot, LongPollingSingleThreadUpdateConsumer {

private  BotConfig config;

private final TelegramClient telegramClient;

public BotTowServiceImpl(BotConfig config) {
    this.config = config;
    telegramClient = new OkHttpTelegramClient(getBotToken());

}

@Override
public String getBotToken() {
    return config.getToken();
}

@Override
public LongPollingUpdateConsumer getUpdatesConsumer() {
    return this;
}

@AfterBotRegistration
public void afterRegistration(BotSession botSession) {

    System.out.println("Registered bot running state is: " + botSession.isRunning());
}

@Override
public void consume(List<Update> list) {
    if(list.size()>0){
        for (Update update:list) {
            if(update.hasMessage()){
                System.out.println(update.getMessage());
                Chat chat = update.getMessage().getChat();

                long chatId = update.getMessage().getChatId();
                String messageText = update.getMessage().getText();
                System.out.println(messageText);
                User user = update.getMessage().getFrom();

                String message_text = "Hey,"+user.getFirstName()+",Welcome to chat "+chat.getTitle();
                SendMessage message = new SendMessage(String.valueOf(chatId),message_text); 
                try {
                    telegramClient.execute(message);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

@Override
public void consume(Update update) {
    System.out.println("message:"+update.getMessage());
    if (update.hasMessage() && update.getMessage().hasText()) {
        // Set variables
        String message_text = update.getMessage().getText();
        long chat_id = update.getMessage().getChatId();
        SendMessage message = SendMessage // Create a message object
                .builder()
                .chatId(chat_id)
                .text(message_text)
                .build();
        try {
            telegramClient.execute(message); // Sending our message object to user
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

}

who can help check?