Skip to content

UnAfraid/ExampleTelegramBot

Repository files navigation

Example Telegram Bot Deploy

Build Status License: MIT Quality Gate Status Reliability Rating Security Rating Maintainability Rating Bugs

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

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:

Docker with docker-compose

You can run this example with docker.

First create new file called .env with the content mentioned Environment Variables above.

  1. Create and start the container
    docker-compose up -d
  2. Check the logs for errors
    docker-compose logs
  3. 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
  4. If you want to stop the bot
    docker-compose down

Source

Alternatively you can run the example from source

  1. Clone the repository
    git clone https://github.com/UnAfraid/ExampleTelegramBot.git
  2. 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
  3. 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
  4. 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