Project (connection between DRF and bot with endpoints):
-bot-directory (not in django project) : services.py and main.py in it
-folders of django apps, manage.py etc.
My function from services.py from bot directory
def get_items_kb():
keyboard = InlineKeyboardMarkup()
button1 = InlineKeyboardButton('Подтвердить', callback_data="yes")
button2 = InlineKeyboardButton('Отклонить', callback_data="no")
keyboard.row(button1, button2)
return keyboard
async def register_manager(CHAT_ID: str):
bot = Bot(token='same')
fake = Faker()
username = fake.user_name()
password = fake.password()
await bot.send_message(CHAT_ID, f'Подтвердите:{username}\n{password}', reply_markup=get_items_kb())
Using of this function in django-signals (signals.py)
@receiver(pre_save, sender=BotUser)
def bot_user_manager_changed(sender, instance, **kwargs):
if instance.id is not None:
previous = BotUser.objects.get(id=instance.id)
if not previous.manager and instance.manager:
register_manager_sync = async_to_sync(register_manager)
register_manager_sync(instance.user_id)
main.py with bot in bot directory (in the same directory as the function, but different file)
storage = MemoryStorage()
TOKEN='same'
bot = Bot(TOKEN)
dp = Dispatcher(bot,
storage=storage)
@dp.callback_query_handler(lambda query: query.data == 'yes', state="*")
@dp.callback_query_handler(lambda query: query.data == 'no', state="*")
async def activate_manager_handler(callback_query: CallbackQuery, state: FSMContext):
...
if __name__ == '__main__':
executor.start_polling(dp,skip_updates=True)
So all I want to do is get this inline keyboard to work and be caught by the handler when the function runs from django signals. Now, the function send a message, but inline-keyboard doesn't work.
Expectation: Example of work: I change manager field to True in django-admin -> signal uses register_manager function -> manager gets a message with inline-keyboard -> he pushes the button -> my handler from main bot file catches that callback_query and do what i say.
Now, handler doesn't see inline-keyboard callbacks