Deploy a Personal chatgpt-telegram-bot

· 2 min read · 294 Words · -Views -Comments

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.

https://static.1991421.cn/2023/2023-04-26-231541.jpeg

Setup steps

  1. In Telegram, talk to @BotFather to create a bot.

  2. In Telegram, talk to @useridinfobot to get your user ID (not your display name).

  3. Choose a HK or overseas server and pull the image.

    docker pull n3d1117/chatgpt-telegram-bot:latest
    
  4. Create a .env file and set environment variables. Reference: https://github.com/n3d1117/chatgpt-telegram-bot/blob/main/.env.example

    The 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
    
  5. Start the container

    docker run  -d --name "chatgpt-telegram-bot" --env-file ./.env n3d1117/chatgpt-telegram-bot:latest
    
  6. 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):

  1. sudo yum -y install inotify-tools
  2. 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
  1. 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
  1. 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.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover