Visionario / django_pyrogram_bot

Django Pyrogram Bot: Basic, simple and 100% functional example demonstrating how Pyrogram integrates with Django, ready to use.
MIT No Attribution
14 stars 4 forks source link

Send a message #2

Closed NeiTRoN96 closed 1 year ago

NeiTRoN96 commented 1 year ago

Hello.

I am new to python. Can you help me with one question? I installed and ran everything according to your instructions and it works. Thank you for this. In the logs, I see incoming messages.

But I can’t figure out how to transfer a message from django to a bot? I re-read your instructions a hundred times, I saw that there is a common file "running_bot_data.json", but I don't understand how data for the bot can be passed through it... As far as I understand, "bot_plugins" is also not suitable for this, because. it's not part of django. Therefore, I don’t even understand where I need to add a method to send a message ... Thanks again for your work

Visionario commented 1 year ago

Hello, yes of course.

To send a message when bot is running, please do this:

1.- Start de bot python manage.py start_bot 2.- Open a django shell console python manage.py shell 3.- Paste into console this code:

from django.conf import settings
from pyrogram import Client

from bot.utils import RunningBot

bot_settings = settings.PYROGRAM_BOT
running_bot = RunningBot()
running_bot_data = running_bot.read_data()

with Client(
        name=f"{bot_settings['BOT_NAME']}",
        session_string=running_bot_data["session_string"],
        no_updates=True,
        workers=1,
        ) as app:

    for admin in bot_settings['ADMINS']:
        app.send_message(
                chat_id=admin,
                text=f"Hello.\n This is a test message.\n"
                )

This example will send a hello message to all admins.

Good luck 🖖

NeiTRoN96 commented 1 year ago

Thanks a lot!

Without this code, I would not have succeeded. Now I understand how to proceed further)

Created a new file so that you can send a message through the django API. At first I got an error, but then I added asynchrony and it worked. Code.

from django.conf import settings
from pyrogram import Client
from bot.utils import RunningBot

async def sendMessage(chat_id, text):
    bot_settings = settings.PYROGRAM_BOT
    running_bot = RunningBot()
    running_bot_data = running_bot.read_data()

    async with Client(
            name=f"{bot_settings['BOT_NAME']}",
            session_string=running_bot_data["session_string"],
            no_updates=True,
            workers=1,
            ) as app:

        await app.send_message(
            chat_id=chat_id,
            text=text
        )

Thank you again!

jeromepacman commented 1 year ago

Ya Visionario made an excellent framework by coupling Django and Pyrogram. I didn't expect so much. have a look at the Pyrogram doc, you can do a lot with decorators. Also Django is not full async, dunno if you gonna host or not, but I ran the bot on Gunicorn which is sync for Django & direct native for the Telegram bot, Everything runs smoothly. I've just retrieve a 2k db users from my group with a simple script. The Pyrogram - Django ORM mix is great. I even don't need to be full async. Thanks to Visionario for bringing up this project. Have a great time