For my university project, I wrote a robot that receives telegram messages through Django (webhook) and responds to them.
Currently, if you send a playlist link from YouTube or Aparat site, it will extract the video link of that playlist and send those to you in a text file. (I used my personal experiences in this project and got help from some sites to get the video links.
Note: The bot language is Persian and can be accessed on Telegram with the username @v2link_bot
To run this bot, you need a few things:
- Python (usually 3.7 or higher)
- django version 3.2 or higher)
- python-telegram-bot version 13.13 (The version in which I wrote the bot)
- jsonpath_ng library
After installing these items, you need to do a few things:
django-admin startproject v2link_bot
then extract this project content into your project directory, so the "bot" folder should be first folder in your directory, then v2link_bot and manage.py file.
To receive updates from Telegram with Django, you need to set webhook. To know how to set it, refer to Marvin's Marvellous Guide to All Things Webhook
By now you should have the bot folder in your django project folder. You need to set it up:
Go to the bot folder and edit the "views.py" file and put your bot token instead of YOUR_BOT_TOKEN in line 7.
Go to v2link_bot folder and open "settings.py" file and add bot to installed_apps:
INSTALLED_APPS = [ ..., 'bot', ]
In the same folder, open the urls.py file and first, import the bot:
from django.urls import path, include from bot.views import callback
Next, redirect the bot path to the bot app:
urlpatterns = [ ..., path("bot", callback), ]
Congratulations, you have completed the bot configuration.
The default Django manage.py runserver command doesn't support SSL; therefore, we need to use the alternative manage.py runserver_plus command, which is part of the Django Extensions package.
pip install django-extensions Werkzeug
open the settings.py file in your code editor and add django_extensions to the INSTALLED_APPS list:
INSTALLED_APPS = [ ..., 'bot', 'django_extensions' ]
Use these commands to setting up the project:
python manage.py makemigrations
python manage.py migrate
start the local development server in HTTPS mode by running the command:
python manage.py runserver_plus --cert-file cert.pem --key-file key.pem
Note: cert.pem and key.pem files are the files that you must have created in the set webhook guide.
Congratulations, the bot has run successfully! If there is a problem, be sure to raise it in the problems section.
This is my first project that I put on github. If there was a problem in the guide or a problem occurred in the program, etc., I would be happy if you report it.