Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ConnorJohn321 opened this issue Oct 15, 2024 · 0 comments
Open

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

ConnorJohn321 opened this issue Oct 15, 2024 · 0 comments

Comments

@ConnorJohn321
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant