Django: a Telegram Bot with Authentication
4 min readJun 4, 2023
How to integrate a Telegram bot inside your Django App
The problem
We want to create a Telegram bot that will be able to access the database.
If you simply write a function for the bot inside your Django app, how would you run it? You can't run it from the Django app, because it should be running in a separate process.
You can't run it from the normal Python app, because it needs access to the database.
We will be using pyTelegramBotAPI
There are many libraries for Telegram bots in Python, but we will be using pyTelegramBotAPI.
Install
pip install pyTelegramBotAPI
Usage
import telebot
bot = telebot.TeleBot("TOKEN", parse_mode=None)
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")
@bot.message_handler(func=lambda m: True)
def echo_all(message):
bot.reply_to(message, message.text)
This is a simple example of a bot that replies to every message with the same message.