M
M
moki1982021-12-31 00:20:41
Python
moki198, 2021-12-31 00:20:41

How to include a script for a specific block of code in python?

I want to connect a js script to a bot written in python! So that when the script is launched, a certain cycle is launched in Python!
Tell me how to do it!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-12-31
@moki198

I assume that you can match the user who opened the page with the user who subscribed to your telegram bot. I hope you are aware that bots are limited in their ability to initiate contact with a user - the user must send a message or enter a chat where the bot is the admin.
I think the scheme would be like this:
1. Upon entering the landing page, JS generates an AJAX request using fetch() or XMLHttpRequest, your choice. The form of the request (GET/POST) and the transmitted data are up to you, but they should be enough to identify the user. In the simplest case, the landing page URL must be unique for each user, and then this unique identifier can be passed.
2. There is a small HTTP server that accepts these requests, writes them to a local database, and gives a success/failure response. It doesn't matter what it's written in, but Flask can be used for simplicity. There will be 100 lines at most. You can use sqlite as a base.
3. There is a Python bot that uses one of the libraries for the cart, which periodically looks into this database and checks for pending requests. If there are any, the bot iterates over them, determines the corresponding user for each request, sends the file, and marks the request as served (well, or simply deletes it).
61ceec5390670627570751.png
There is a temptation to combine points 2 and 3 in one process, but this may not be trivial - you will have to run the web server in the main thread, and the bot in the secondary thread. Then, instead of a database, it will be possible to use a queue (threading.Queue) - the main thread (Flask) puts tasks in the queue, and the bot thread selects and serves.
61ceedcee35a5542427561.png
Pros - one Python script instead of two, no intermediate database.
Cons - the increased complexity of the script, if the script suddenly stops, the current tasks in the queue will be lost, it is more difficult to keep statistics on how users are served by the bot.
Decide for yourself if it's worth it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question