项目作者: UnAfraid

项目描述 :
Telegram Bot example
高级语言: Java
项目地址: git://github.com/UnAfraid/ExampleTelegramBot.git
创建时间: 2019-08-30T19:26:54Z
项目社区:https://github.com/UnAfraid/ExampleTelegramBot

开源协议:MIT License

下载


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:

  1. EXAMPLE_TG_BOT_TOKEN=123455:abcd.. # The token from BotFather
  2. 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
    shell script docker-compose up -d
  2. Check the logs for errors
    shell script docker-compose logs
  3. If bot is working correctly you should see the following logs
    ```shell script
    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
    1. 4. If you want to stop the bot
    2. ```shell script
    3. docker-compose down

Source

Alternatively you can run the example from source

  1. Clone the repository
    shell script git clone https://github.com/UnAfraid/ExampleTelegramBot.git
  2. Build the source
    ```shell script

    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
    ```shell script

    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

    1. 4. If bot is working correctly you should see the following logs
    2. ```shell script
    3. [20:30:06] [INFO] Main: Initializing **Your bot name here** ...
    4. [20:30:06] [INFO] Main: Authorized admin ids: [**Your ids here**]
    5. [20:30:07] [INFO] Main: Initialization done