Waiting in a Django View For a Webhook or Timeout - python

I am using WhatsApp official API to send messages to customers. I want to send them a reminder message if they have not replied during a certain period of time (a couple of hours more or less). In other words, I will send a message and wait for a certain period of time for their response. If they respond, no reminder message is needed. If they don't, I will send them a reminder message. WhatsApp API sends webhook notifications when they reply.
How do I implement that in a Django view? I am thinking of creating an async process/thread each time I want to wait, and handle the timeout condition in that process somehow. Is this the right approach? I think that would be costly in terms of server time used, but I am not sure. If this is the right approach, please show some pseudo-code on how it can be done.
I will be deploying my app on Heroku.
Your answer is much appreciated..

Related

Telegram Bulk Messaging Limitation

this Platform is really really helpful. My issue is that is it possible to send 500k promotional messages per day? I have the script ready using the API # and API ID using Telethon. But anytime i try to send Messages, the accounts get banned. Maybe it got banned because i was on the same IP Address while scraping the users and sending messages. But what I really need to know is that is it even Possible to send 500k messages in day, if yes:
How many Accounts are needed(where can I get these number of
accounts from)?
Do I have to manually create accounts and receive
OTP's for each or is there a script ready for that as well that I
can implement?
What should be the duration between each messages and
how many messages from one single account.
How to shuffle between accounts while send messages? Manually or is there again a script
ready for it?
Please guys I really really need help. If not 500k how many can i send per day maximum.
You can send up to 20 messages per second in the same chat on Telegram.

How to send partial status of request to frontend by django python?

Suppose, I have sent a post request from react to Django rest API and that request is time taking. I want to get how many percentages it has been processed and send to the frontend without sending the real response?
There are two broad ways to approach this.
(which I would recommend to start with): Break the request up. The initial request doesn't start the work, it sends a message to an async task queue (such as Celery) to do the work. The response to the initial request is the ID of the Celery task that was spawned. The frontend now can use that request ID to poll the backend periodically to check if the task is finished and grab the results when they are ready.
Websockets, wherein the connection to the backend is kept open across many requests, and either side can initiate sending data. I wouldn't recommend this to start with, since its not really how Django is built, but with a higher level of investment it will give an even smoother experience.

Speeding up chat bot replies in django python

I have made a chat bot in django python which listens via Http requests. Certain chat channels, such as slack, require an immediate 200OK http response from the server. Hence I register a celery task (into a queue) to return http 200OK instantly and let the reply be processed in background.
It is taking 3-4 seconds in production (SQS based) for the bot's reply to be received by the end user. Through logs I have found out that the delay is in the task to reach the celery worker.
I want to make my chat bot's replies to come really fast when a user types in a message and am looking for a faster alternative to celery for this specific use case. Thank you!
Note that I do not want to use slack's RTM api because I do not intend to make my bot slack specific.
I solved it by using multi threading as explained in this answer, although I am not so sure about scalability of this solution as of yet.

How To Perform asynchronous task in django?

I am a bit lost here. I want to send email using django on a post request. But I don't want the user to wait for a reply so I am sending it in a background thread. Also the emails are to be send every 30 seconds in a sequential order provided by the user.
But there is a option for the user to cancel sending the emails i.e a different post request to handle the cancel input.
How do I listen for cancel operation in the thread sending the email?
How do I implement the 30 second delay?
I thought of using sleep but if I do that then will it skip the cancel signal?
As mentioned above, it would probably be the case of using a Celery task for sending the e-mails asynchronously. Then, for cancelling the e-mails sending, your view handling the POST (for cancelling) request could revoke the tasks responsible for sending that sequence of e-mails. This question/answer shows how to do it.

Use python to implement a async notification system

I have been troubled by a design question.
I want to implement a small service that looks like a mail office. It will accept clients request(in the request it has the message to deliver and destination url info). After that I will help the client to send the message to specified URL. BUT when the first trial fails(I expect a 200 with 'success' response) there is some strategy like I will continually send the message to the destination url at most 10 times. And I will wait for a certain time to send the message again. For example, I send the message immediately after I receive the request(the following looks like a delivery algorithm):
if it fails then
I will wait for 1 minutes to send it again
if it fails again then
I will wait for a longer time period(like 5 minutes) and so on
until the num of trials reaches Max(10 times).
I used to use django framework with django-cron to solve the problem. However it is not accurate for it depends on the time I set for the django-cron(the less time between the cron the better). I don't know whether there is an elegant and appropriate method to solve this kind of problem.
Thank you for your help!

Categories