Reputation: 39
I`m building a whatsapp bot, the bot is fully operational, but I want to do a API call if user does not interact with the api after some time. I tryed to do with session, but is not working, I tryed the following code.
session_start();
//**my bot code**
$minutesBeforeSessionExpire=30;
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > (2))) {
$data2 = [
'phone' => $_SESSION['phone'],
'body' => 'Hello, Andrew!',
];
$json2 = json_encode($data2);
$options2 = stream_context_create(['http' => [
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $json2
]
]);
$result2 = file_get_contents('api_call', false, $options2);
session_unset(); // unset $_SESSION
session_destroy(); // destroy session data
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity
Upvotes: 1
Views: 72
Reputation: 39
For those who find this question, I solved my issue by storing the user last interaction on my db.
After that I created a cron on my cpanel to run the update code, and everything ran as expected.
Upvotes: 0
Reputation: 13561
In order to “do something” when a user doesn’t interact, there must be something that interacts with the user. This is typically either a client side script, websockets, signal R. It doesn’t just happen naturally, in fact quite the opposite, naturally the server totally forgets about an incoming request as soon as it is done with it.
Upvotes: 1
Reputation: 8945
Unfortunately for you, "any API" is purely "server side." Therefore, it cannot "react" when anyone "fails to reply." The only thing that it can do is, when presented with [any ...] subsequent request, to say: "So sorry, too late!"
The key point being that the response is reactive, not proactive.
Upvotes: 1