예제 #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker\Factory::create();
     for ($i = 0; $i < 50; $i++) {
         $inbound = Inbound::create(array('from' => '601' . $faker->randomNumber(9), 'to' => '601' . $faker->randomNumber(9), 'type' => $faker->randomElement(array('text', 'unicode')), 'text' => $faker->sentence()));
     }
 }
예제 #2
0
 public function postReply()
 {
     $inbound = Inbound::findOrFail(Input::get('inbound_id'));
     $text = trim(Input::get('text'));
     if ($text == "") {
         return array('error' => 'Text is empty');
     }
     $outbound = new Outbound();
     $outbound->from = $inbound->to;
     $outbound->to = $inbound->from;
     $outbound->text = Input::get('text');
     $outbound->type = $inbound->type;
     $outbound->save();
     return ['status' => 'success'];
 }
예제 #3
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();
 }
예제 #4
0
 /**
  * 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
         }
     }
 }
예제 #5
0
    Auth::logout();
    return Redirect::to('login');
});
/* Queue */
Route::post('queue/receive', function () {
    return Queue::marshal();
    //return Response::make(array('foo' => 'bar'), 202);
});
/* Callback Url */
Route::match(array('GET', 'POST'), 'callback/{item?}', function ($item = 'debug') {
    switch ($item) {
        case 'mo':
            if (!Input::has('msisdn') || !Input::has('to') || !Input::has('text')) {
                break;
            }
            $inbound = new Inbound();
            $inbound->from = Input::get('msisdn');
            $inbound->to = Input::get('to');
            $inbound->text = Input::get('text');
            $inbound->type = Input::get('type');
            $inbound->save();
            break;
        case 'dn':
            $outbound_chunk = OutboundChunk::where('message_id', '=', Input::get('messageId'))->first();
            if (!$outbound_chunk) {
                break;
            }
            $outbound_chunk->dn_status = Input::get('status');
            $outbound_chunk->dn_error_code = Input::get('err-code');
            $outbound_chunk->save();
            break;