Skip to content

Commit

Permalink
Added entrypoint that executes some stuff on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed Mar 21, 2022
1 parent 69d2162 commit 2b0eb91
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor/
.idea
.env
logs/*
logs/*
docker-compose.override.yml
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ This repository is a boilerplate for development of the [php-telegram-bot](https
git clone --recurse-submodules https://github.com/php-telegram-bot/workspace.git
```

After cloning the workspace, you can use the workspace as if it were a project.
We provided a few scripts and a docker-compose.yml that configures a webserver and a local Telegram Bot API server for using and testing webhooks.
After cloning the workspace, you can use the workspace as if it were a project. We provided a few scripts and a
docker-compose.yml that configures a webserver and a local Telegram Bot API server for using and testing webhooks.

### Working on php-telegram-bot
To work on the php-telegram-bot repository itself you need to `cd` into the `packages/php-telegram-bot` directory and checkout a branch with

To work on the php-telegram-bot repository itself you need to `cd` into the `packages/php-telegram-bot` directory and
checkout a branch with

```shell
git checkout develop
```

To pull all the current changes from upstream you can call from any location

```shell
git submodule update --remote --merge
```
Expand All @@ -56,17 +60,19 @@ If you want to commit changes to upstream, you can simply do that as anywhere el
By default the Docker setup launches preconfigured with the following containers:

1. **nginx** _Webserver_
- to accept webhook calls from the custom bot api server
- to accept webhook calls from the custom bot api server
2. **php-fpm** _PHP 8.1_
- runs all the code
- runs all the code
3. **mariadb** _Database_
- already has the structure imported
- already has the structure imported
4. **[tdlight](https://github.com/tdlight-team/tdlight-telegram-bot-api)** _Custom Bot API Server_
- Lighter version of the custom Telegram bot API server.
- Lighter version of the custom Telegram bot API server.

To start the container copy the `.env.example` into a `.env` file and fill out your `TELEGRAM_API_ID` and `TELEGRAM_API_HASH` data from https://my.telegram.org/apps.
To start the container copy the `.env.example` into a `.env` file and fill out your `TELEGRAM_API_ID`
and `TELEGRAM_API_HASH` data from https://my.telegram.org/apps.

After this you can start the containers with

```shell
docker-compose up -d
```
Expand All @@ -77,12 +83,34 @@ docker-compose up -d

By default there is a /start command.

So if you've also filled out your `TELEGRAM_BOT_USERNAME` and `TELEGRAM_BOT_TOKEN` in your `.env` file, you can execute the `setWebhook.php` inside the php Docker container.
So if you've also filled out your `TELEGRAM_BOT_USERNAME` and `TELEGRAM_BOT_TOKEN` in your `.env` file, you can execute
the `setWebhook.php` inside the php Docker container.

If successful, you should be able to send a `/start` to your bot.

<p align="right">(<a href="#top">back to top</a>)</p>

## Manually calling the webserver

**WIP**
If you need to call the webserver directly or have a look at the stats of the Bot API server you can create
a `docker-compose.override.yml` file and add the needed port forwarding like in this example:

```yaml
version: '3'
services:
nginx:
ports:
- 8080:80

api-server:
environment:
TELEGRAM_STAT: yes
ports:
- 8082:8082
```
The first block inside `services` extends the nginx service and adds a port forwarding from 8080 (on your host) to 80 inside the container.
You can then call any php script or static files with `http://localhost:8080`

The second block extends the api-server service, enables the stats with an environment variable and forwards the port 8082 from the container to your host.
This allows you to call `http://localhost:8082` to access the stats page of the Telegram Bot API server and check i.e. if the webhook was registered correctly.
5 changes: 5 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ RUN set -eux; \
apt-get install -y git; \
install-php-extensions pdo_mysql zip

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm"]

WORKDIR /app
16 changes: 16 additions & 0 deletions docker/php/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

composer install --no-interaction --quiet

if [ ! -f .env ]; then
cp .env.example .env
fi

php scripts/setWebhook.php

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$@"
fi

exec "$@"

0 comments on commit 2b0eb91

Please sign in to comment.