Telebot register_next_step_handler_by_chat_id не работает в общей задаче celery
У меня есть небольшой проект tg bot django, и бот должен отправлять сообщение пользователям, которые были неактивны более часа, а затем ждать ввода от пользователя с помощью tg_nick_or_phone_input_handler и записывать его в экземпляр TelegramBotClientModel, отправляя больше сообщений в чат
def tg_nick_or_phone_input_handler(message):
chat_id = message.chat.id
bot_client = TelegramBotClientModel.objects.get(chat_id=chat_id)
bot_client.phone_or_nickname = message.text
bot_client.request_sent = True
bot_client.save()
bot.send_message(
chat_id=chat_id,
text='REQUEST SENT'
)
bot.send_message(
chat_id=chat_id,
text='some message'
)
import django.utils.timezone as tz
from celery import shared_task
from tg_funnel_bot.bot import bot
from .views import tg_nick_or_phone_input_handler
from .models import TelegramBotClientModel, BotMessagesSettingsModel
# celery task where should register next step handler
@shared_task(name='send_message_for_interrupted_dialog_after_one_hour')
def send_message_for_interrupted_dialog_after_one_hour():
bot_messages_settings = BotMessagesSettingsModel.objects.all().first()
inactive_for_hour_clients = TelegramBotClientModel.objects.filter(
updated_at__lte=tz.now() - tz.timedelta(minutes=5),
request_sent=False,
message_for_one_hour_inactive_sent=False
)
for inactive_client in inactive_for_hour_clients:
chat_id = inactive_client.chat_id
bot.send_message(
chat_id=chat_id,
text=bot_messages_settings.user_inactive_for_hour_message_text_first_part
)
bot.send_message(
chat_id=chat_id,
text=bot_messages_settings.user_inactive_for_hour_message_text_second_part,
reply_markup=bot_messages_settings.get_inactive_message_second_part_markup()
)
bot.register_next_step_handler_by_chat_id(
chat_id=chat_id,
callback=tg_nick_or_phone_input_handler
)
inactive_client = TelegramBotClientModel.objects.get(pk=inactive_client.pk)
inactive_client.message_for_one_hour_inactive_sent = True
inactive_client.save()
return tz.now()
bot.register_next_step_handler_by_chat_id
не регистрируется обработчик и не обрабатывается ввод в чате бота.
Возможно я не могу использовать экземпляр бота в других процессах, но я могу отправлять сообщения и register_next_step_handler_by_chat_id работает, если использовать его в файле с webhook update APIView
вот так
from telebot import types
from rest_framework.views import APIView
from rest_framework.response import Response
from tg_funnel_bot.bot import bot
from .models import BotMessagesSettingsModel, TelegramBotClientModel
class UpdatesHandlerBotAPIView(APIView):
def post(self, request):
json_data = request.body.decode('UTF-8')
update_data = types.Update.de_json(json_data)
bot.process_new_updates([update_data])
return Response({'code': 200})
def after_loading_questions_data_handler(message):
chat_id = message.chat.id
bot_messages_settings = BotMessagesSettingsModel.objects.all().first()
after_loading_questions_data_text = bot_messages_settings.after_data_loading_text
bot.send_message(
chat_id=chat_id,
text=after_loading_questions_data_text
)
bot.register_next_step_handler_by_chat_id(
chat_id=chat_id,
callback=tg_nick_or_phone_input_handler
)
def tg_nick_or_phone_input_handler(message):
chat_id = message.chat.id
bot_client = TelegramBotClientModel.objects.get(chat_id=chat_id)
bot_client.phone_or_nickname = message.text
bot_client.request_sent = True
bot_client.save()
bot.send_message(
chat_id=chat_id,
text='REQUEST SENT'
)
bot.send_message(
chat_id=chat_id,
text='some text'
)
настройки сельдерея
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_BEAT_SCHEDULE = {
'add-every-10-minutes': {
'task': 'send_message_for_interrupted_dialog_after_one_hour',
'schedule': crontab(minute='*/1'),
}
}
Это так странно. Помогите мне, пожалуйста