Skip to content

mohammadhoseinGhezelsofla/videoToLinkBot-A-Telegram-bot-is-written-by-Django-and-Telegram-bot-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

videoToLinkBot: A Telegram bot is written by Python and Django and Telegram bot library

For my university project, I wrote a robot that receives telegram messages through Django (webhook) and responds to them.

What can this bot do?

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

How to run this bot on a server?

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:

1: Create a new django project:

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.

2: Set webhook:

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

3: Install the bot app, configure it, and add it in created django project:

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.

4: Configuring Django server to work with HTTPS

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'
]

starting the server:

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.

Additional explanations:

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.

About

using django and python-telegram-bot together to make a telegram bot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages