A Telegram Bot written in Python with Django Framework. Its main feature is to reply to messages by searching for keywords within them.
This Bot works with Webhooks method. View Telegram Bot API Documentation.
python3 -m venv venv
source venv/bin/activate
(win.\venv\Scripts\activate
)python -m pip install --upgrade pip
pip install -r requirements.txt
Clone this repo.
git clone https://github.com/fvlgnn/telegram-answer-bot
Use only remake project. Don't use with git clone.
django-admin startproject bot .
python manage.py startapp app
python manage.py collectstatic
- Copy or Rename
.env.example.file.txt
file in.env
- Edit
.env
file with your parameters
DEBUG
true/false be enable/disable debug modeTELEGRAM_BOT_TOKEN
is alphanumericals token released of Telegram BotFatherSECRET_KEY
is alphanumericals string for django securityHOST
list of allowed hosts or DNS domains separated by space (Importantapi.telegram.org
)SQLITE_DB
true use SQLite, false use PostgreSQL, in this case configure PostgreSQL parameters- If using PostgreSQL Database(
SQLITE_DB=false
)POSTGRESQL_NAME
is database namePOSTGRESQL_USER
is username of database user enabled to usePOSTGRESQL_PASS
is passoword of userPOSTGRESQL_HOST
is database host namePOSTGRESQL_PORT
is database port
python manage.py createsuperuser
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
- Visit Admin page http://127.0.0.1:8000/telegram-answer-bot-admin/ or https://your-domain.net/telegram-answer-bot-admin/
- Using credential created by manage.py createsuperuser
- Configure Keyword and Answer
With Postman or other REST Client or by cURL, set the Telegram Bot for using Webhooks
Method GET or POST https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/setWebhook?url=https://your-domain.net/telegram-answer-bot/webhook
EDIT THIS:
bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
is Telegram bot token apihttps://your-domain.net
is your domain with valid SSL certificate (HTTPS)
If you haven't an HTTPS connection (with valid SSL certificate), must be created a self signed certificate and upload it on Telegram API Webhook.
Curl example (edit token, domain and file)
curl --location --request POST 'https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/setWebhook' \
--form 'url="https://your-domain.net/telegram-answer-bot/webhook"' \
--form 'certificate=@"/path/to/self-sign-public-key-certificate-file"'
With Postman use method POST
and body
as form-data
with keys url
value and certificate
file.
Read https://core.telegram.org/bots/api#setwebhook and https://core.telegram.org/bots/self-signed
For deploy on custom host read https://github.com/fvlgnn/setup-django-web-server
It's very important that your host has an HTTPS connection (valid SSL encryption e.g. https://your-domain.net/) otherwise, a self-signed certificate must be created and used, view Set Webhook.
Read the documentation of the chosen service.
Advice Heroku, PythonAnyWhere, DigitalOcean.
- Send Broadcast message by Django Admin page, using django signals. Environment
TELEGRAM_BOT_TOKEN
will be used for this. - Add notification to ADMIN_CHAT_ID for important events.