본문 바로가기
토이프로젝트/텔레그램 리마인더 봇

[JAVA/텔레그램봇] 2. 리마인더 봇 개발하기 - 메아리 기능

by 상후 2022. 10. 3.
728x90
반응형

 

 

 

토이프로젝트 - 텔레그램 리마인더 봇 만들기 - 메아리 기능


메아리 기능 만들기

- 본격적인 리마인더 개발 전 간단한 메아리 기능을 통해 텔레그램에서 어떤 식으로 메시지를 받을 수 있는지 확인합니다.


1. onUpdateReceived() 의 Update 객체에 받은 메시지에 대한 정보들이 들어있습니다.

2. 해당 정보들을 활용하여 채팅방ID와 받은 메시지를 추출 후 동일한 내용으로 다시 반환해줍니다.

3. 전송은 execute() 메서드를 통해 전송이 가능합니다.

 

- 해당 기능은 간단한 테스트를 위한 기능입니다.

 

각 오브젝트 참고 : https://core.telegram.org/bots/api#authorizing-your-bot

 

Telegram Bot API

The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram. To learn how to create…

core.telegram.org

 

@Override
public void onUpdateReceived(Update update) {
    String chatId = String.valueOf(update.getMessage().getChatId());
    String messageText = update.getMessage().getText();

    try {
        execute(SendMessage.builder().chatId(chatId).text(messageText).build());
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }

}

 

테스트

 

 

https://github.com/SanghooMoon/telegram-reminder-bot/commit/cf2cfa96468892c0eb738066783243954fe4bfe0

 

feat : 내가 보낸 메시지 그대로 응답받기 · SanghooMoon/telegram-reminder-bot@cf2cfa9

Showing 1 changed file with 10 additions and 1 deletion.

github.com

 

728x90
반응형

댓글