I’m developing a djnago project and want to connect a telegram bot to it. I’m using python-telegram-bot
but do not know how to start the bot when the django server starts.
from django.apps import AppConfig
from .telegramBot import updater
class SocialMediaConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'memefinder'
def ready(self) -> None:
updater.start_polling()
pass
I added this code to the apps.py
file of one the project’s app but it’s not working. I got this error message evrytime I run the project
telegram.error.Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
and this the code of telegramBot.py
file. it’s very simple code.
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
updater = Updater("TOKEN")
dispatcher = updater.dispatcher
def start(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
update.message.reply_markdown_v2(
fr'Hi {user.mention_markdown_v2()}!',
reply_markup=ForceReply(selective=True),
)
dispatcher.add_handler(CommandHandler("start", start))
2
Answers
The problem seems to be the Django’s auto-reloader. When
manage.py runserver
command is run, it spawns two instances with it. A file monitoring process which reloads the project every time there is some change in one of the project files, and the other is the main process. You can read more about it here in this article.To get around it, you need to check and load the process only when the program is run by the main process, which can be done by checking the ‘RUN_MAIN’ environmental variable:
Home/apps.py:
The example above assumes you have an app called Home where app.py is located.
Actually, the best solution here is to split Telegram bot and web application to use different processes (run it separately).
The problem here is that you’re trying to run telegram handler loop inside a Djnago application. It won’t work because of several causes:
Moreover, now you are using Django Development server using
manage.py
command, but if you’re going to deploy your application to production environment, your configuration will differ: you should use some production-ready solution like gunicorn. Different configurations of the web server in different environments will make it harder to write the correct and scalable code for serving both a web app and a Telegram bot.I understand that you’re likely seeking for a way to get data from Django ORM inside the Telegram bot or use some other Django features. If so, you can still access django from your other scripts. All you need to do is to specify
DJANGO_SETTINGS_MODULE
environment variable to help Django find your settings module.So, create a separate Python script (for example,
telegram_bot.py
in the root of your app) for running your Telegram bot and run it separately.will run your bot while you will be able to import Django Models and work with them inside that script.