This repository contains an example of telegram bot written in Java (17 and above)
Current version supports the following commands:
- /help - Displays help about all or specified command
- /whoami - Displays information about the person who wrote the command: User Id, Name and chat type
- /start - The default bot command, shows greeting message
- /menu - Shows example of Inline Menu with buttons
Configuration is supplied through Environment Variables:
EXAMPLE_TG_BOT_TOKEN=123455:abcd.. # The token from BotFather
EXAMPLE_TG_BOT_ADMIN_IDS=123456 # User ids separated by comma, allowing them to access /menu (You can obtain that id by typing /whoami)
Click here to talk to BotFather
There are multiple ways to start this example, here are two:
You can run this example with docker.
First create new file called .env with the content mentioned Environment Variables above.
- Create and start the container
docker-compose up -d
- Check the logs for errors
docker-compose logs
- If bot is working correctly you should see the following logs
Attaching to example_telegram_bot example_telegram_bot | [20:30:06] [INFO] Main: Initializing **Your bot name here** ... example_telegram_bot | [20:30:06] [INFO] Main: Authorized admin ids: [**Your ids here**] example_telegram_bot | [20:30:07] [INFO] Main: Initialization done
- If you want to stop the bot
docker-compose down
Alternatively you can run the example from source
- Clone the repository
git clone https://github.com/UnAfraid/ExampleTelegramBot.git
- Build the source
# Note: If you want all of the dependencies to be packaged along with the application use installShadowDist instead of installDist # For Windows gradlew.bat installDist # For Linux/Mac ./gradlew installDist
- Run the bot
# Set Environment Variables # For Windows set EXAMPLE_TG_BOT_TOKEN=123455:abcd.. # The token from BotFather set EXAMPLE_TG_BOT_ADMIN_IDS=123456 # User ids separated by comma, allowing them to access /menu (You can obtain that id by typing /whoami) # For Linux/Mac export EXAMPLE_TG_BOT_TOKEN=123455:abcd.. # The token from BotFather export EXAMPLE_TG_BOT_ADMIN_IDS=123456 # User ids separated by comma, allowing them to access /menu (You can obtain that id by typing /whoami) # Navigate to the bot build directory cd build/install/ExampleTelegramBot # Run the bot java --add-opens java.base/java.lang=ALL-UNNAMED -jar ExampleTelegramBot.jar
- If bot is working correctly you should see the following logs
[20:30:06] [INFO] Main: Initializing **Your bot name here** ... [20:30:06] [INFO] Main: Authorized admin ids: [**Your ids here**] [20:30:07] [INFO] Main: Initialization done