Deploy a Personal chatgpt-telegram-bot
ChatGPT usage can be unstable. To avoid downtime impacting work, I set up a private Telegram bot as a backup. After some research, I chose to deploy the excellent chatgpt-telegram-bot.
Setup steps
In Telegram, talk to @BotFather to create a bot.
In Telegram, talk to @useridinfobot to get your user ID (not your display name).
Choose a HK or overseas server and pull the image.
docker pull n3d1117/chatgpt-telegram-bot:latest
Create a
.env
file and set environment variables. Reference: https://github.com/n3d1117/chatgpt-telegram-bot/blob/main/.env.exampleThe four main settings:
# Your OpenAI API key OPENAI_API_KEY=XXX # Your Telegram bot token obtained using @BotFather TELEGRAM_BOT_TOKEN=XXX # Telegram user ID of admins, or - to assign no admin ADMIN_USER_IDS=ADMIN_1_USER_ID,ADMIN_2_USER_ID # Comma separated list of telegram user IDs, or * to allow all ALLOWED_TELEGRAM_USER_IDS=USER_ID_1,USER_ID_2
Start the container
docker run -d --name "chatgpt-telegram-bot" --env-file ./.env n3d1117/chatgpt-telegram-bot:latest
Now you can talk to your bot in Telegram.
Auto‑restart on config changes
With a plain docker run
container, updating the .env
file won’t auto‑reload the container. To recreate it on changes, I used inotify-tools
(CentOS 7 example below):
sudo yum -y install inotify-tools
- Create
watch_env.sh
#!/bin/sh
while true; do
inotifywait -e modify .env
echo ".env file has been modified. Restarting the container..."
docker stop chatgpt-telegram-bot
docker rm chatgpt-telegram-bot
docker run -d --name "chatgpt-telegram-bot" --env-file /root/chatgpt-telegram-bot/.env n3d1117/chatgpt-telegram-bot:latest
done
- Create
watch-env.service
sudo vi /etc/systemd/system/watch-env.service
Service configuration:
[Unit]
Description=Watch and restart Docker container on .env file changes
[Service]
Type=simple
ExecStart=/bin/bash /root/chatgpt-telegram-bot/watch_env.sh
Restart=always
User=root
[Install]
WantedBy=multi-user.target
- Enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable watch-env.service
sudo systemctl start watch-env.service
GPT‑4
After getting access approval, set OPENAI_MODEL=gpt-4
in .env
.
Apply here: https://openai.com/waitlist/gpt-4-api
Final Thoughts
That’s it — a personal bot is up. It’s paid, but relatively stable. Hoping to get API access for GPT‑4 soon to make it even better.