This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Java library for the Telegram Bot API, published to Maven Central as com.github.pengrad:java-telegram-bot-api. It provides type-safe wrappers for all Telegram Bot API methods and models.
# Build
./gradlew build
# Run all tests (requires env vars - see Testing section)
./gradlew clean check
# Run PR tests (excludes long-running integration tests)
./gradlew clean check -PprTest
# Run a specific test class
./gradlew test --tests com.pengrad.telegrambot.TelegramBotTest
# Run a specific test method
./gradlew test --tests com.pengrad.telegrambot.TelegramBotTest.methodName
# Code coverage report
./gradlew jacocoTestReportIntegration tests require a real Telegram bot. Set these environment variables:
TEST_TOKEN— bot tokenCHAT_ID— test chat IDGROUP_ID— test group IDPRIVATE_KEY— for Passport decryption tests
Additional optional env vars: TEST_PASSPORT_DATA, TEST_CALLBACK_QUERY, TEST_INLINE_QUERY, etc.
This is a multi-module Gradle project:
library/— the published librarysample/— usage examples
- Instantiate a request object:
new SendMessage(chatId, "text") - Execute synchronously:
bot.execute(request)→ returns typed response - Or execute asynchronously:
bot.execute(request, callback)
Internally, TelegramBotClient serializes the request to an HTTP POST using Gson, calls api.telegram.org/bot<token>/<MethodName>, and deserializes the JSON response into a typed response class.
TelegramBot(library/.../TelegramBot.java) — Main entry point. Built vianew TelegramBot(token)ornew TelegramBot.Builder(token).build()for custom OkHttp/Gson config.TelegramBotClient(library/.../impl/TelegramBotClient.java) — OkHttp3 HTTP client; handles all serialization and deserialization.BaseRequest<T extends BaseRequest, R extends BaseResponse>(library/.../request/BaseRequest.java) — Generic base for all request classes. Uses generics to link each request type to its response type.BaseResponse(library/.../response/BaseResponse.java) — Base for all response classes. Containsok,errorCode,description.
request.*— ~100+ request classes, one per Telegram API method (e.g.,SendMessage,GetUpdates,AnswerCallbackQuery)model.*— ~100+ model classes for Telegram types (e.g.,Message,User,Chat,Update)response.*— Typed response wrappers (e.g.,SendResponse,GetUpdatesResponse)passport.*— Telegram Passport decryption logicimpl.*— Internal HTTP client and update handler implementations
Two modes are supported:
- Polling: Call
bot.setUpdatesListener(updates -> { ... })to start background polling viagetUpdates. - Webhook: Parse incoming webhook payloads with
bot.parseUpdate(json).
The UpdatesHandler / SleepUpdatesHandler classes manage the polling loop internally.
- Every Telegram API method maps 1:1 to a class in
request.*and a class inresponse.*. - Request classes use a fluent builder pattern:
new SendMessage(chatId, text).parseMode(ParseMode.HTML).replyMarkup(keyboard). - All model and request classes follow Telegram API naming closely (snake_case field names deserialized by Gson into camelCase Java fields).
- The library targets Java 8 runtime compatibility, though JDK 11+ is required to build.