2.7 KiB
MyBot
A long-polling example that prints information about the bot and lets Telegram users replace or remove its global profile photo.
Behavior
At startup, the application prints the results of getMe and getChat for the bot itself to standard output. It then handles two commands:
/setMyProfilePhotoreplies withok, send me new photoand waits for the first photo sent in the same chat. It streams draft progress messages while downloading the photo to a temporary file, uploads it as a static bot profile photo, and replies when the change is complete. The photo may come from any user in that chat; it is not restricted to the user who sent the command./removeMyProfilePhotoremoves the bot's current profile photo and confirms success. On failure, it prints the exception and sends a generic error reply.
There is no /start handler, and ordinary messages are ignored unless the bot is waiting for a photo after /setMyProfilePhoto.
Setup and permissions
- Create a bot with @BotFather and obtain its token.
- No chat-administrator permission is required to change the bot's own profile photo. In a group, the bot still needs permission to send replies.
- Prefer using this example in a private chat. Telegram's draft-message API is intended for private chats, and BotFather privacy mode can prevent an unrelated group photo from reaching the bot unless it is sent as a reply or privacy mode is disabled.
- This example performs no authorization checks: anyone who can reach the commands can change or remove the bot's profile photo globally. Do not expose a production bot without adding access control.
- Long polling automatically removes an existing webhook at startup. Run only one update consumer for the token at a time.
Arguments
The first argument is always the required bot token. Optional arguments can follow it in any order:
debugenables formatted default KSLog output.testServermakes the behavior and long-polling client use Telegram's Bot API test environment. The initialgetMeandgetChatdiagnostics currently use a separate default-server client.
Other arguments are ignored. Argument matching is case-sensitive.
Launch
The intended command from the repository root is:
./gradlew :MyBot:run --args="<BOT_TOKEN>"
For example, to enable both optional modes:
./gradlew :MyBot:run --args="<BOT_TOKEN> debug testServer"
Known issue:
MyBot/build.gradlestill declares the oldGetMeBotKtmain class, while the current source producesMyBotKt. Consequently, theruntask cannot start until that build setting is corrected. It is left unchanged here because this example update is documentation-only.