Skip to content

Telegram bot builded on Django web-framework, using pyTelegramBotAPI library

Notifications You must be signed in to change notification settings

xinitd/django-telegram-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telegram logo

Telegram bot with Webhooks

Development · Deployment in Debian · Docker deployment

About

Telegram bot builded on Django web-framework, using pyTelegramBotAPI library.

Why Django? Its very customizable. Here Django used for setting Webhooks, but also may be extended for storing any data in database and interaction with user.

Body of bot located in core/bot.py. First function need for handle /start command from user. Second function just quote and repeat all user sended messages.

File core/views.py contain two views. First view needed for setting Webhooks to Telegram's server. Second view get updates from Telegram's server.

Development

  • Install dependencies: sudo apt install python3 python3-pip python3-venv git -y

  • Clone repo: git clone https://github.com/xinitd/django-telegram-bot.git

  • Enter in project directory: cd django-telegram-bot

  • Create virtual environment: python -m venv env

  • Activate virtual environment: source env/bin/activate

  • Install requirements: pip install django djangorestframework gunicorn pyTelegramBotAPI pyyaml or pip install -r requirements.txt

  • Create settings from template: cp backend/settings.template.yaml backend/settings.yaml

  • Generate django secret key and insert in backend/settings.yaml

  • Get token from @BotFather and insert in backend/settings.yaml file (TELEGRAM_TOKEN) section

  • Enter your domain name in backend/settings.yaml (WEBHOOK_HOST)

  • Uncomment # bot.infinity_polling() line in core/bot.py file

  • Run bot for testing: python -m core.bot

  • Send /start message to your bot

Deployment (production usage) with Webhooks in Debian based distros

  • Install dependencies: sudo apt install python3 python3-pip python3-venv nginx certbot python3-certbot-nginx git -y

  • Clone repo: git clone https://github.com/xinitd/django-telegram-bot.git

  • Enter in project directory: cd django-telegram-bot

  • Create virtual environment: python -m venv env

  • Activate virtual environment: source env/bin/activate

  • Install requirements: pip install django djangorestframework gunicorn pyTelegramBotAPI pyyaml or pip install -r requirements.txt

  • Create settings from template: cp backend/settings.template.yaml backend/settings.yaml

  • Generate django secret key and insert in backend/settings.yaml

  • Get token from @BotFather and insert in backend/settings.yaml file (TELEGRAM_TOKEN) section

  • Enter your domain name in backend/settings.yaml (WEBHOOK_HOST)

  • Create socket file sudo nano /etc/systemd/system/mytelegrambot.socket:

[Unit]
Description=My Telegram bot's socket

[Socket]
ListenStream=/run/mytelegrambot.sock

[Install]
WantedBy=sockets.target
  • Create service file sudo nano /etc/systemd/system/mytelegrambot.service:
[Unit]
Description=My Telegram bot's service
Requires=mytelegrambot.socket
After=network.target

[Service]
User=your_user
Group=your_group
WorkingDirectory=/home/your_user/django-telegram-bot
ExecStart=/home/your_user/django-telegram-bot/env/bin/gunicorn \
          --access-logfile - \
          --workers 1 \
          --bind unix:/run/mytelegrambot.sock \
          backend.wsgi:application

[Install]
WantedBy=multi-user.target
  • Start socket and enable autorun:
sudo systemctl start mytelegrambot.socket
sudo systemctl enable mytelegrambot.socket
  • Start bot's service:
sudo systemctl daemon-reload
sudo systemctl restart mytelegrambot.service
sudo systemctl enable mytelegrambot.service
  • Configure Nginx. Create configuration: sudo nano /etc/nginx/sites-available/mytelegrambot
server {
    listen 80;
    server_name mytelegrambot.mydomain.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/mytelegrambot.sock;
    }
}
  • Create symlink for configuration file: sudo ln -s /etc/nginx/sites-available/mytelegrambot /etc/nginx/sites-enabled

  • Test Nginx configuration for syntax errors: sudo nginx -t

  • Receive certificate from Letsencrypt certbot sudo certbot --nginx -d mytelegrambot.mydomain.com

  • Restart Nginx: sudo systemctl restart nginx

  • Open mytelegrambot.mydomain.com url in browser

  • Now send /start message to your bot

Docker deployment

  • Install Docker in your OS

  • Install Git and Certbot: sudo apt install git certbot -y

  • Clone repo: git clone https://github.com/xinitd/django-telegram-bot.git

  • Enter in project directory: cd django-telegram-bot

  • Create settings from template: cp backend/settings.template.yaml backend/settings.yaml

  • Generate django secret key and insert in backend/settings.yaml

  • Get token from @BotFather and insert in backend/settings.yaml file (TELEGRAM_TOKEN) section

  • Enter your domain name in backend/settings.yaml (WEBHOOK_HOST)

  • Edit nginx/default.conf file and change mytelegrambot.mydomain.com to your domain

  • Generate new certificate and key for your domain: sudo certbot certonly --standalone -d mytelegrambot.mydomain.com

  • Copy certificate and key in nginx/ path: sudo cp /etc/letsencrypt/live/mytelegrambot.mydomain.com/fullchain.pem /etc/letsencrypt/live/mytelegrambot.mydomain.com/privkey.pem nginx/

  • Run: docker compose up -d