Telegram bots: want to use php sleep() function in webhook - php

What I have
a telegram bot (backend PHP) with webhook set. It have several commands I am entertaining.
What I want to do
As soon as user enters a specific command in telegram chat to my bot,
I am sending him message that I will message him after 15 mins . Now I
want to send him some message after 15 mins.
What I have done
I tried using sleep()of php to pause webhook execution for 15 mins and then send message. But after few seconds though user have not sent any message webhook recieved a message with text field containing 5\u2063 and php script got reloaded.
Is there any way I can do what I am trying to do?

you can insert a query in db
time_end = (time()+(60*15));
and in another php file that set it for a cron job(every one min) check if time() >= $row['time_end']
do somthing

Related

Scheduling emails with cron jobs from a mysql table

I am trying to set up a system where a user enters some information in a form and an email will be constructed where the information is saved into mysql.
I am trying to figure out how to make it so the email will be sent, for example, 20 minutes after the user makes their input. (Without the user staying on the browser).
I need this delay as I need the ability for an admin to log on to a page to look at the email and possibly edit it before it sends.
Is this possible through a cron job. Am I able to set one up that automatically checks sql table for an update and then sends the email after a certain time?
Or is it possible to delay a php script with the sleep function and then send the email. Can I make the PHP script still run when user has closed site and left?
you can use mySQL to store the data sent by the user. this data will be accessed later using another script triggered by a cron Job: if you have the ability to set cron jobs in the control panel or via access to the server, go ahead, use cron tab syntax to define when the job will be triggered, this website may help you:
https://crontab-generator.org/
another approach is to use external service to trigger an event every interval, the event could be accessing the cron job script via HTTP.
if you want your email to be sent exactly after 20 mins, please add a field to your mysql table indicating the desired send date(beware of timezones).
you may also want to add a flag indicating if the email is sent, so you do not send the same email twice.
You can't (easily) have a PHP script stay alive that long.
Your best strategy, IMO, would be to have the PHP script create the email file, and notify the human.
Then you can have PHP run a shell script which uses the "at" program to schedule a task to happen in 20 minutes. At is a cousin of cron, but is better suited for this job.
That scheduled task will be to take the e-mail message, move it some place else (like a "done" directory), and pipe it through your mailer. tip: /usr/sbin/sendmail -t < myEmailFile will work on most Linux boxen.

How to send automatic email after 5 and 8 minutes of login

I am working on a PHP application where I have to send automatic sms/email after user send request.
USER-1 sends request to USER-2, if there is no response from USER-2 (i.e. Do not accept the request or rejects the request), then after 5-minutes user-2 should get email/sms to take action on request. If User-2 still takes action then after 8-minutes request of User-1 should get cancelled.
But I have no idea how to do?
Usually you have some kind of scheduler which take care of this kind of work.
This could be for example a cronjob or jenkins which is running a script all x minutes. This script is looking for any kind of changes that you want to threat.
You could for example save the timestamp when the message was sent to another user.
Your script requests then all items from your database where the time between now and the saved timestamp is greater than 8 minutes.
Sending emails for that etc.

Delay in PHP mail() function

I have a small ordering taking app which submits the order to a PHP script by ajax.
When the PHP script receives the order it places it in a database, sends an 'OK' message back to the client and then sends a confirmation email to the customer and another to me.
The problem is that the client is not seeing the 'OK' (which updates the browser display) for about 15 seconds. If I comment out the email sends, no delay.
... write to database
echo "OK";
mail($semail,'Msg Subject',$message, $header, $bounceto);
mail($semail2,'Msg Subject',$mesage, $header, $bounceto);
Message is only a couple of lines.
Questions:
why would there be a delay when calling the mail() function?
how can I cause the 'OK' message to be sent without waiting for the PHP script to complete? A flush of some sort I'm guessing.
That's because http://php.net/manual/en/function.mail.php returns boolean value for success or failure and your mail server takes time to process email request.
You can implement some kind of MYSQL queue for emails where you would put emails to be sent and check it with background task (for example using CRON to lauch YourMailQueueProcessingScripts.php that will check if there are any emails to send and perform the sending operation - have in mind that you can run CRON once per minute at most and I don't know if 1 minute delay of sending email is acceptable by you)
It should work with fastcgi-finish-request.php but it requires PHP FPM
Adding this will output the buffer to the user and cut the connection, the rest of the script will still be executed, but no additional output will be sent, so be sure to output everything before you call fastcgi_finish_request() and thereafter the mail() function

PHP- MySQL cronjob sending mail loop repeat

In one of my web application, I am sending daily mails to the users using a cron job function via AMAZON SES. The cronjob will run in the interval of 10 mins. The process will like
$sql-mysql_query("SELECT * FROM users WHERE send_date='2013-07-13' and alert_send=0");
while($row=mysql_fetch_array($sql)) {
// Get email id of the user and compose meggage
// Create a new Amazon Request and send the mail
// Update alert_send=1
}
If the loop contains more counts ie suppose a 500 mails, on next 10th min, an another cron job will start and start sending mails. At the end users will get the mail twice or thrice accordingly.
ie. If the loop contains 500 datas
Cron A will start at 12:00 and fetch all the 500 datas and send emails. Suppose send mails 120 with 10 mins.
Cron B will start at 12:10 and fetch data from 120 - 500. This will also send mail.
By this result, 121 th user will get mails from Cron A and Cron B.
I tried of limit the query count. But the problem is we cant predict when each loop ends. ie sometimes it will take 4,2,5,6 or 10 secs for send a mail.
Is there any way to avoid this duplicating ? Is there any way to kill the existing cronjob and start new ?
Thanks in advance
Everytime when the cronjob start you can kill the cronjob that is running
like this by the name:
pkill process-name
or like this by id:
kill 1234
Also you can take a look at this blog that use php code :
http://abhinavsingh.com/blog/2009/12/how-to-use-locks-in-php-cron-jobs-to-avoid-cron-overlaps/

dont wait for script to finish

I have a php script that sends SMS, the problem is that it takes some time before every SMS is sent. In my site the page will wait until this script has finished running. How can I give user a message that SMS will be sent and resume the site's normal operation.
The only issue here is that the browser thinks it is are waiting for more output from the script when there will be none. You could offload to a seperate process, or use an asynchronous web call, or you could simply.....
<?php
register_shutdown_function('when_alls_done');
.... // render page
exit;
function when_alls_done()
{
if ($_REQUEST['send_to_phone']) {
send_sms($_REQUEST['send_to_phone'], $_REQUEST['message']);
}
}
The webserver should flush the request at the 'exit' and let the browser know that the response is complete (an explicit flush in the PHP code prior to that will either not flush the webserver buffer or it will result in the output being chunk encoded with another chunk to come).
C.
Put the messages in a queue in your database. Then have a script running as a cron job in the background to take care of the queue.
It's very easy when you know how:
<?php
echo 'A sms will be sent!';
fastcgi_finish_request();
// Put all your time consuming code here!
?>
Instead or sending the SMS right there and then you can store it somewhere (for example the database). You can then build an extra script (in PHP or whatever else you want) that polls the database looking for SMS to send and dispatches them. You can have this run every x seconds or minutes via cron or scheduled task according to your OS. This way you take a time consuming task out of the page whose job is to communicate with user in a timely manner.
If you are on LAMP
Write a cron job that will query a queue (in database) for pending SMS and send them.
In your script add the SMS to the queue.
Show the user status of his SMS on another page.
With Ajax you can query in background for the status of recently sent SMS. As soon as you see sent notify user.
to do this you can use ajax.
on some button press just send an ajax request to the php file moulded to send sms. and put a notification in the screen like 'sending sms!!!' and on the response of the ajax action change the 'sending sms' to 'success!!!'....
for using ajax you can use jquery..
if you dont know jquery comment me for the video tutorials... i have some beginners video tutorial for jquery....
Have a nice day!!!!!

Categories