-1

I have got a couple of images I want to send to a WhatsApp number using Twilio. I want to then send a text message only after all the images have been delivered. While sending an image, I specify a callback that gets the delivery status and stores it using redis. After I have made a call to send these images, I check the delivery statuses of all the images, if none of the images have its status to be equal to zero, I then send the text message else I do a check until I get a delivery status that is not zero. However, my proposed solution is timing out. I am getting this error - Maximum execution time of 60 seconds exceeded from Redis Even the images are not sending. See the attached imageenter image description here

This is what I am doing:

Route::get('test', function (){
    $redis = new \Predis\Client();
    $images = \App\ProductImage::where('product_id', 7)->get();

    foreach ($images as$image){
        $imgUrl = url(env('BOT_URL').'/images/'.$image['img_url']);
        $sid[] = \App\Libraries\Utilities::sendMediaMessage('+23xxxxx', '', $imgUrl);
    }
    foreach ($sid as $id){
        $statuses[] = $redis->get($id);
    }

    if(in_array(0, $statuses)){
           $check = \App\Libraries\Utilities::checkIfWhatsAppStatusHasChanged($sid);
        if ($check){
            \App\Libraries\Utilities::sendWhatsAppMessage('+23xxxxx', 'Send message - had to check');
        }
    }else{
        \App\Libraries\Utilities::sendWhatsAppMessage('+23xxxx', 'Send message ');
    }

});

Here is the sendMediaMessage method

 public static function sendMediaMessage($phone, $msg, $mediaUrl){
             try{

            $client = new Client(env('TWILIO_SID'), env('TWILIO_TOKEN'));
            $send = $client->messages->create(
                "whatsapp:".$phone, // Text this number
                array(
                    'from' => "whatsapp:".env('TWILIO_NUMBER'), // From a valid Twilio number
                    'body' => $msg,
                    'mediaUrl' => $mediaUrl,
                    'statusCallback' => url(env('BOT_URL').'/whatsapp-delivery-status')
                )
            );
            //
            if($send != false){
                $message_sid = $send->sid;
                return $message_sid;
            }

        }catch (\Exception $exception){
        }
    }

Here is the callback method -

   public function checkWhatsapDelivery(Request $request)
    {
        $redis = new Client();
        $body = $request->all();
        $messageSid = $body['MessageSid'];
        $MessageStatus = $body['MessageStatus'];
        $status_id = Utilities::getWhatsappMessageStatus($MessageStatus);
        $redis->set($messageSid, $status_id);
    }

Here is the getWhatsappMessageStatus method

 public static function getWhatsappMessageStatus($status){
        switch (strtolower($status)){
            case 'queued':
                $id = 1;
                break;
            case 'failed':
                $id = 2;
                break;
            case 'sent':
                $id = 3;
                break;
            case 'delivered':
                $id = 4;
                break;
            case 'read':
                $id = 5;
                break;
            default:
                $id = 0;
        }
        return $id;
    }

Here is checkIfWhatsAppStatusHasChanged method

  public static function checkIfWhatsAppStatusHasChanged($sids){
        $statuses = self::getWhatsAppStatuses($sids);
        if(in_array(0, $statuses)){
            self::checkIfWhatsAppStatusHasChanged($sids);
        }else{
            return true;
        }
    }

Here is getWhatsAppStatuses method

public static function getWhatsAppStatuses($sids){
        $redis = new \Predis\Client();
        foreach ($sids as $id){
            $statuses[] = $redis->get($id);
        }
        return $statuses;
}

Please what am I doing wrong?

shekwo
  • 1,411
  • 1
  • 20
  • 50
  • 2
    check out this https://stackoverflow.com/questions/1420249/maximum-execution-time-of-60-seconds-exceeded-error – Md. Amirozzaman Jan 11 '20 at 18:33
  • You can find the solution here https://stackoverflow.com/questions/1420249/maximum-execution-time-of-60-seconds-exceeded-error – Shohag Monzur Jan 11 '20 at 19:36
  • 1
    A better idea, is to send your final message within the `checkWhatsapDelivery` callback, this way, the main script does not need to wait. – levi Jan 12 '20 at 00:08
  • @levi Exactly what I did and its working fine now. Thanks. – shekwo Jan 12 '20 at 19:42

1 Answers1

-2

Check the php.ini file located in C:\xampp\php\php.ini(windows) and opt/lampp/etc/php.ini in linux. Here is max_execution_time = 60 second by default. Therefore you have to increase max_execution_time for sending/posting big size data.

Md. Robi Ullah
  • 1,703
  • 3
  • 20
  • 32