/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $this->line('==============================/');
     $this->line('Registering WhatsApp');
     $this->line('==============================/');
     $numbers = Number::where('type', 'like', 'mobile%')->where('features', 'like', '%SMS%')->get(array('number'))->map(function ($number) {
         return $number->number;
     })->toArray();
     if (empty($numbers)) {
         return $this->error('No mobile number found.');
     }
     $number = $this->choice('Choose which number to register.', $numbers);
     $numberObj = Number::where('number', $number)->first();
     if ($numberObj->voice_callback_type != 'tel') {
         $newNumber = $this->ask('Enter your personal mobile number to receive whatsapp verification code.');
         $numberObj->voice_callback_type = 'tel';
         $numberObj->voice_callback_value = $newNumber;
         $isSaved = $numberObj->save();
         if (!$isSaved) {
             return $this->error('Number is fail to update.');
         }
     }
     // confirm personal number to forward call
     $isNumberOkay = $this->confirm('Verification code will be sent to this number, ' . $numberObj->voice_callback_value . '. Proceed?', true);
     if (!$isNumberOkay) {
         return;
     }
     // registering
     $proceed = $this->confirm('This is very important. You will receive the verification code via phone call, you have to key in the code before the phone call end (around 30 secs) and correct on first try, otherwise you have to wait for 30 mins to 24 hours to get another verification code. Proceed?', true);
     $wa = new WhatsProt($number, $number, false);
     try {
         $waResponse = $wa->codeRequest('voice');
         if ($waResponse->status != 'ok') {
             $verificationCode = str_replace('-', '', $this->ask('Enter your verification code.'));
             $waResponse = $wa->codeRegister($verificationCode);
         }
         $numberObj->wa_password = $waResponse->pw;
         //$numberObj->wa_identity = $waResponse->identity;
         $numberObj->wa_expiration = $waResponse->expiration;
         $numberObj->save();
         $this->line('Done. Run following command in supervisord, php artisan whatsapp:start ' . $number);
     } catch (Exception $e) {
         $this->error($e->getMessage());
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $number = Number::where('number', $id)->first();
     if ($number->delete()) {
         return array();
     }
     return $this->response->errorInternal();
 }
 public function dnCallback($job, $outbound_chunk_id)
 {
     $client = new Client();
     $outbound_chunk = OutboundChunk::find($outbound_chunk_id);
     $number = Number::where('number', $outbound_chunk->outbound->from)->first();
     if (filter_var($number->own_callback_url, FILTER_VALIDATE_URL) !== FALSE) {
         try {
             $client->post($number->own_callback_url, array('headers' => array('Content-Type' => 'application/x-www-form-urlencoded'), 'body' => array_merge($outbound_chunk->toArray(), $outbound_chunk->outbound->toArray(), array('callback_type' => 'dn'))));
         } catch (Exception $e) {
         }
     }
     $job->delete();
 }
Exemple #4
0
 public function moCallback($job, $inbound_id)
 {
     $client = new Client();
     $inbound = Inbound::find($inbound_id);
     $number = Number::where('number', $inbound->to)->first();
     if (filter_var($number->own_callback_url, FILTER_VALIDATE_URL) !== FALSE) {
         $success = true;
         try {
             $client->post($number->own_callback_url, array('headers' => array('Content-Type' => 'application/x-www-form-urlencoded'), 'body' => array_merge($inbound->toArray(), array('callback_type' => 'mo'))));
         } catch (Exception $e) {
             $success = false;
         }
         if (!$success && $job->attempts() < 7) {
             $job->release(10);
         }
     }
     $job->delete();
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     if (!$this->argument('number')) {
         return $this->error('No number specified.');
     }
     $number = Number::where('number', $this->argument('number'))->first();
     if (is_null($number) || is_null($number->wa_password) || is_null($number->wa_expiration)) {
         return $this->error('Whatsapp not registered. Run php artisan whatsapp:register');
     }
     $contacts = $number->contacts->map(function ($contact) {
         return $contact->number;
     });
     $wa = new WhatsProt($number->number, $number->number, true);
     $wa->connect();
     // Connects to WhatsApp
     $wa->loginWithPassword($number->wa_password);
     // Login
     $wa->pollMessage();
     $wa->sendGetPrivacyBlockedList();
     // Get our privacy list
     $wa->sendGetClientConfig();
     // Get client config
     $wa->sendGetServerProperties();
     // Get server properties
     if (!$contacts->isEmpty()) {
         $wa->sendGetHasVoipEnabled($contacts);
     }
     // Get which users have voip enabled
     $wa->sendGetGroups();
     // Get groups (participating)
     $wa->sendGetBroadcastLists();
     // Get broadcasts lists
     // $wa->sendGetProfilePicture(self); // self preview profile picture [OPTIONAL]
     if (!$contacts->isEmpty()) {
         $wa->sendSync($contacts);
     }
     // Sync all contacts
     // $wa->sendGetStatuses(All contacts); // Get contacts status [OPTIONAL]
     /*
     for (All contacts) [OPTIONAL]
     {
       			$wa->sendGetProfilePicture(contact); // preview profile picture of every contact
     }
     */
     // $wa->sendPing(); // Keep alive
     $wa->eventManager()->bind("onGetMessage", function ($mynumber, $from, $id, $type, $time, $name, $body) {
         // todo // save message id and compare to avoid duplicate
         $inbound = new Inbound();
         $inbound->from = $this->getFrom($from);
         $inbound->to = $mynumber;
         $inbound->text = $body;
         $inbound->type = 'whatsapp';
         $inbound->save();
     });
     $wa->eventManager()->bind("onPresenceAvailable", function ($mynumber, $from) {
         $from = $this->getFrom($from);
         // todo // add or update wa-contact
     });
     $wa->eventManager()->bind("onPresenceUnavailable", function ($mynumber, $from, $last) {
         $from = $this->getFrom($from);
         // todo // add or update wa-contact
     });
     $wa->eventManager()->bind("onMessageReceivedClient", function ($mynumber, $from, $id) {
         $outbound_chunk = OutboundChunk::where('message_id', $id)->first();
         if (!$outbound_chunk) {
             return;
         }
         $outbound_chunk->dn_error_code = 0;
         $outbound_chunk->dn_status = 'delivered';
         $outbound_chunk->save();
     });
     $time = time();
     while (true) {
         sleep(1);
         $wa->pollMessage();
         $this->processMessages($wa);
         if (time() - $time >= 8) {
             $wa->sendActiveStatus();
             $time = time();
             // whatsapp command action
             $whatsAppAction = Cache::get('whatsAppAction_' . $number->number, false);
             if ($whatsAppAction) {
                 $whatsAppActionInput = Cache::get('whatsAppActionInput_' . $number->number, false);
                 Cache::forget('whatsAppAction_' . $number->number);
                 Cache::forget('whatsAppActionInput_' . $number->number);
                 switch (strtolower($whatsAppAction)) {
                     case 'updatestatus':
                         $wa->sendStatusUpdate($whatsAppActionInput);
                         break;
                     case 'setprofilepicture':
                         try {
                             $wa->sendSetProfilePicture($whatsAppActionInput);
                         } catch (Exception $e) {
                         }
                         break;
                     case 'stop':
                         $wa->disconnect();
                         exit('whatsapp is stopped');
                 }
             }
             // end whatsapp command action
         }
     }
 }