Skip to content
Advertisement

Do someting if user does not interact after some time – PHP

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement