This package provides the ability to integrate the Telegram messaging service and GitHub/GitLab. With this package, you can create a Telegram bot to receive notifications from GitHub or GitLab events and manage customization through messages and buttons on Telegram.
- Send GitHub/GitLab notifications of your repositories to Telegram Bots, Groups, and Channels.
- The bot must be created using the BotFather
- GitHub/GitLab Notifications to Telegram: The package allows you to configure a Telegram bot to receive notifications from various GitHub/GitLab events, including events like commits, pull requests, issues, releases, and many more.
- Customize Notifications: You can customize the types of notifications you want to receive through options on Telegram.
- Interactive Buttons: The package supports creating interactive buttons on Telegram to perform actions such as enabling or disabling notifications.
- Event Management: You can manage specific events that you want to receive notifications for, allowing you to focus on what's most important for your projects.
- Easy Integration: The package provides an API and user-friendly functions to create a Telegram bot and link it to your GitHub/GitLab account.
- PHP ^8.0
- Composer
- Telegram Bot
As for the installation and configuration, this project provides two different installation ways depending on your preference or suitable for your system.
Way 1: Install by composer directly on the system (Requires the system to install composer, previous PHP version)
Way 2: Install by Docker (Requires the system to install Docker, Docker Compose)
Please choose only one of the following two ways to set up the project.
Way 1: Install by composer directly on the system β
First, please clone and install this project via Composer:
composer create-project lbiltech/telegram-bot-github-notify
After running the command above, you will have the project installed under the telegram-bot-github-notify
directory,
and the environment file .env
will be created automatically.
Some the json files will be created automatically in the storage
directory.
These files are used to store the data and serve for features in this bot.
To create a new bot, you need to talk to BotFather and follow a few simple steps.
- Open a chat with BotFather and send
/newbot
command. - Enter a friendly name for your bot. This name will be displayed in contact details and elsewhere.
- Enter a unique username for your bot. It must end in
bot
. Like this, for example:TetrisBot
ortetris_bot
. - Copy the HTTP API access token provided by BotFather and paste it into your
.env
file.
TELEGRAM_BOT_TOKEN=123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ
- Open a chat with your bot.
- Send any message to your bot. (This handle needs to get your chat id)
- Go to the following URL:
https://api.telegram.org/bot<YourTelegramBotToken>/getUpdates
- Look for the
"chat":{"id":
field and copy the number after it. This is your Chat ID. - Paste the Chat ID in your
.env
file.
TELEGRAM_BOT_CHAT_ID=123456789
We recommend that you use HTTPS to set up your domain and webhook. You can build your own server or use a service like Heroku.
In this example, we will use localhost and ngrok to set up the domain and webhook:
- Download and install ngrok.
- Go to this project directory and run command in terminal:
php -S localhost:8000
- Continue to run command in terminal:
ngrok http 8000
- Copy the HTTPS URL provided by ngrok and paste it into your
.env
file.
APP_URL=https://123456789.ngrok.io
At this time, the source launch process is done, please skip way 2 and go to step II. Set the webhook to continue.
Way 2: Install by Docker π³
Note: This way requires the system to install Docker and Docker Compose.
β If you set up the project by way one, please can skip this way. πΈ
And go to step II. Set the webhook to continue.
First, please clone this project and copy the environment file .env.example
to .env
:
git clone [email protected]:lbiltech/telegram-bot-github-notify.git
cd telegram-bot-github-notify
cp .env.example .env
Open the .env
file and update the following variables:
PHP_VERSION_SELECTED=8.2
APP_PORT=3180
# You can customize the container name to suit your needs using GitHub and GitLab
CONTAINER_NAME=telegram-notify-bot
Note:
- The
PHP_VERSION_SELECTED
variable is the PHP version you want to use in the container.- The
APP_PORT
variable is the port of the container. (Please don't set the same port as the host)- The
CONTAINER_NAME
variable is the name of the container you want to create.
Run the following command to install and run the container:
bash ./docker.sh
Some the json files will be created automatically in the storage
directory.
These files are used to store the data and serve for features in this bot.
To create a new bot, you need to talk to BotFather and follow a few simple steps.
- Open a chat with BotFather and send
/newbot
command. - Enter a friendly name for your bot. This name will be displayed in contact details and elsewhere.
- Enter a unique username for your bot. It must end in
bot
. Like this, for example:TetrisBot
ortetris_bot
. - Copy the HTTP API access token provided by BotFather and paste it into your
.env
file.
TELEGRAM_BOT_TOKEN=123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ
- Open a chat with your bot.
- Send any message to your bot. (This handle needs to get your chat id)
- Go to the following URL:
https://api.telegram.org/bot<YourTelegramBotToken>/getUpdates
- Look for the
"chat":{"id":
field and copy the number after it. This is your Chat ID. - Paste the Chat ID in your
.env
file.
TELEGRAM_BOT_CHAT_ID=123456789
In this way, we use the proxy in container and ngrok to set up the domain and webhook:
- Check the proxy of the container:
docker inspect <CONTAINER_NAME>-nginx | grep IPAddress
Note: Replace
<CONTAINER_NAME>
with the name of the container in .env file.
Example:
- Download and install ngrok.
- Continue to run command in terminal:
ngrok http <CONTAINER_IP>
Note: Replace
<CONTAINER_IP>
is the IP address of the container in step 1.
Example:
ngrok http 172.28.0.3
- Copy the HTTPS URL provided by ngrok and paste it into your
.env
file.
APP_URL=https://123456789.ngrok-free.app
We have two ways to set the webhook:
After setting up your domain and SSL certificate, you need to set up the webhook for your bot.
Go to:
<APP_URL>/webhook/set.php
Note: Replace
<APP_URL>
with your app URL in .env file.
If you see the following message, it means that the webhook has been sent successfully.
{"ok":true,"result":true,"description":"Webhook was set"}
If you want to set the webhook manually, you can use the following URL:
https://api.telegram.org/bot<YourTelegramBotToken>/setWebhook?url=<APP_URL>
Note: Replace
<YourTelegramBotToken>
with your bot token and<APP_URL>
with your app URL in.env
file.
You can add multiple chat ids to the .env
file.
These chat ids will be the chat ids of your groups, channels, or users.
Also, you can add the chat id of this bot to receive incoming notifications for itself.
TELEGRAM_NOTIFY_CHAT_IDS="-978339113,-1001933979183"
Note: Please use comma to separate chat ids.
Now your configuration is complete. And the .env
file will be like this:
APP_NAME='Telegram GitHub/GitLab Notify Bot'
# Set your app URL here
APP_URL=https://123456789.ngrok.io
TELEGRAM_BOT_TOKEN=6162840106:AAH3g20lMQIkG_wHHu8R_ngdtG541uzoq4
TELEGRAM_BOT_CHAT_ID=6872320129
# Set your telegram group chat ids here ( please use comma to separate )
TELEGRAM_NOTIFY_CHAT_IDS="-978339113,-1001933979183"
TIMEZONE=Asia/Ho_Chi_Minh
PHP_VERSION_SELECTED=8.2
CONTAINER_NAME=telegram-notify-bot
APP_PORT=3180
Now you can send a message to your bot, and you will receive a welcome message from the bot.
/start
If you want to check the menu, you can send the following message to your bot.
/menu
π At this point, the configuration process for your telegram bot is completed. You can use all the features of this bot. ππ
To increase ease of use. Let's create a menu with a list of commands listed for you.
Please send the following message to your bot to create a menu button.
/set_menu
Now you will need to add the Webhook for your GitHub and GitLab repository to receive notifications.
- Go to your repository settings.
- Go to the
Webhooks
section. - Click on
Add webhook
. - Set
Payload URL
to<APP_URL>
. - Set
Content type
toapplication/x-www-form-urlencoded
. - Which events would you like to trigger this webhook? Select
Let me select individual events.
. - Click on the
Active
checkbox and Add webhook button. - Done. You will receive a notification when your repository has a new event.
Here is the first notification you will receive:
- Go to your repository settings.
- Go to the
Webhooks
section. - Click on
Add new webhook
. - Set
URL
to<APP_URL>
. - Choose any
Trigger
you want. - Click on the
Enable SSL verification
checkbox and Add webhook button.
Note: You can set up this webhook for different repositories. Please similarly set up the webhook for each repository.
Then every time one of those repositories appears an event, this telegram bot will immediately send you a notification.