Exemplo n.º 1
0
 public function getWhatsProt(Sender $sender, $connectionRequired = true)
 {
     $username = $sender->getUsername();
     // Telephone number including the country code without '+' or '00'.
     $identity = $sender->getIdentity();
     // Obtained during registration with this API or using MissVenom (https://github.com/shirioko/MissVenom) to sniff from your phone.
     $nickname = $sender->getNickname();
     // This is the username displayed by WhatsApp clients.
     if (!empty($this->whatsProts[$username])) {
         $w = $this->whatsProts[$username];
         if ($connectionRequired && empty($this->connected)) {
             $w->cleanDebug();
             $w->connect();
             $w->loginWithPassword($sender->getPassword());
             $w->sendPassive("false");
             $this->connected = true;
             $w->sendGetServerProperties();
             echo $w->getDebugBuf();
         }
         return $w;
     }
     $groupDAO = new GroupDAO();
     $w = new WhatsProt($username, $identity, $nickname, true);
     $w->cleanDebug();
     $w->eventManager()->bind('onGetSyncResult', function (\SyncResult $syncResult) use($groupDAO, $sender) {
         $groups = $groupDAO->getList(array('user_id' => $sender->getUserId()));
         foreach (array_keys($syncResult->existing) as $number) {
             if ($number[0] == '+') {
                 $number = substr($number, 1);
             }
             foreach ($groups['list'] as $groupId => $group) {
                 $groupDAO->markSynced($groupId, $number);
             }
         }
         foreach ($syncResult->nonExisting as $number) {
             if ($number[0] == '+') {
                 $number = substr($number, 1);
             }
             // TODO
         }
     });
     $w->eventManager()->bind('onCredentialsBad', function ($number, $status, $reason) use($sender) {
         $senderDao = new SenderDAO();
         $senderMod = $senderDao->fetch($sender->getId());
         $senderMod->setPassword('');
         $senderDao->save($senderMod);
         throw new \Exception('Bad credentials for: ' . $number . ', status: ' . $status . ', reason: ' . $reason, 403);
     });
     $w->eventManager()->bind('onLoginFailed', function ($number) use($sender) {
         $senderDao = new SenderDAO();
         if ('' != $sender->getPassword()) {
             $wdao = WhatsappDAO::instance($sender);
             $wdao->sendSmsCode();
         } else {
             //            $senderMod = $senderDao->fetch($sender->getId());
             ////            $senderMod->setPassword('');
             //            $senderDao->save($senderMod);
         }
         throw new \Exception('Login failed for ' . $number, 403);
     });
     $w->eventManager()->bind('onSendMessage', function ($from, $target, $whatsapp_id, $node) {
         $this->lastSentMessageId = $whatsapp_id;
         if (empty($this->currentMessage)) {
             return;
         }
         $dao = new MessageDAO();
         if (empty($target) || is_array($target)) {
             if (!empty($this->currentTargets)) {
                 foreach ($this->currentTargets as $target) {
                     $dao->addStatus($this->currentMessage, Message::MESSAGE_STATUS_SENT, '', $target, $whatsapp_id);
                 }
             }
         } else {
             $target = explode('@', $target);
             $target = $target[0];
             $dao->addStatus($this->currentMessage, Message::MESSAGE_STATUS_SENT, '', $target, $whatsapp_id);
         }
     });
     $w->eventManager()->bind('onGetReceipt', function ($from, $id, $offline, $retry) {
         $target = explode('@', $from);
         $target = $target[0];
         $dao = new MessageDAO();
         $item = $dao->getMessageByWhatsappId($whatsapp_id);
         if (!empty($item)) {
             if (strpos($from, 'broadcast')) {
                 $target = explode('@', $node->getAttribute('participant'));
                 $target = $target[0];
                 if (!empty($target)) {
                     $dao->setMessageTargetStatus($item, $target, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE);
                     $dao->addStatus($item, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE, '', $target, $whatsapp_id);
                 }
             } else {
                 $dao->setMessageTargetStatus($item, $target, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE);
                 $dao->addStatus($item, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE, '', $target, $whatsapp_id);
             }
         }
     });
     $w->eventManager()->bind('onGetAck', function ($from, $whatsapp_id, $class) {
         if ($class != 'message') {
             return;
         }
         $target = explode('@', $from);
         $target = $target[0];
         $dao = new MessageDAO();
         $item = $dao->getMessageByWhatsappId($whatsapp_id);
         if (!empty($item)) {
             if (strpos($from, 'broadcast')) {
                 $dao->getSentTargetsById($whatsapp_id);
             } else {
                 $dao->setMessageTargetStatus($item, $target, Message::MESSAGE_STATUS_RECEIVED_BY_SERVER);
                 $dao->addStatus($item, Message::MESSAGE_STATUS_RECEIVED_BY_SERVER, '', $target, $whatsapp_id);
             }
         }
     });
     $w->eventManager()->bind('onGetMessage', function ($number, $from, $id, $type, $t, $name, $data) {
         if ($type == 'text') {
             $chatDAO = new ChatDAO();
             $from = preg_replace('!@.*!', '', $from);
             $chat = new Chat();
             $chat->setFrom($from);
             $chat->setFromNickname($name);
             $chat->setTo($number);
             $chat->setData($data);
             $chat->setCtime($t);
             $chat->setWhatsappId($id);
             $chatDAO->save($chat);
         }
     });
     if ($connectionRequired && empty($this->connected)) {
         $w->cleanDebug();
         $w->connect();
         $w->loginWithPassword($sender->getPassword());
         $w->sendGetStatuses("True");
         $this->connected = true;
         $w->sendGetServerProperties();
         echo $w->getDebugBuf();
     }
     $this->whatsProts[$username] = $w;
     return $this->whatsProts[$username];
 }
Exemplo n.º 2
0
usort($messages, function ($a, $b) {
    return $a->getGroupId() - $b->getGroupId();
});
logToCronFile("Fetched messages to send: " . count($messages));
foreach ($messages as $message) {
    $messageDAO->clearRetry($message);
    $groupId = $message->getGroupId();
    $sender = $senderDAO->fetch($message->getSenderId());
    if (!$sender->getId()) {
        continue;
    }
    if ('' == $sender->getPassword()) {
        echo "Empty password for " . $sender->getUsername() . ' ' . $sender->getNickname() . "\n";
        $messageDAO->addStatus($message, Message::MESSAGE_STATUS_ERROR, 'Sender not registered - go sender and use confirm SMS function');
        continue;
    }
    logToCronFile("Sending: " . $sender->getUsername() . '=>' . $message->getTarget());
    $whatsappDAO = WhatsappDAO::instance($sender);
    $whatsappDAO->sendMessage($message, $sender);
}
foreach ($senders['list'] as $sender) {
    if ('' == $sender->getPassword()) {
        continue;
    }
    $whatsappDAO = WhatsappDAO::instance($sender);
    $whatsappDAO->processPoll($sender);
}
$buff = ob_get_contents();
ob_end_clean();
logToCronFile("\n" . str_replace('<br />', "\n", html_entity_decode($buff)));
@unlink($lockFile);
Exemplo n.º 3
0
 /**
  * Process confirmation SMS code
  *
  * @param number $id
  */
 public static function postConfirmPage($id)
 {
     $app = \Slim\Slim::getInstance();
     $senderDAO = new SenderDAO();
     $sender = $senderDAO->fetch($id);
     if (empty($sender) || empty($id)) {
         return $app->notFound();
     }
     $smscode = $_POST['smscode'];
     $smscode = preg_replace('![^0-9]*!', '', $smscode);
     $errors = array();
     try {
         $wdao = WhatsappDAO::instance($sender);
         $wdao->confirmSmsCode($smscode);
         $app->flash('info', 'Your confirmation code has been successfully verified');
         $app->redirect(MAINURL . '/senders/edit/' . $id);
     } catch (\Exception $ex) {
         $errors['debug'] = array($ex->getMessage() . "\n" . $wdao->getDebugBuf());
         $app->view->set('menu', 'senders');
         $app->view->set('id', $id);
         $app->view->set('item', $sender);
         $app->view->set('errors', $errors);
         $app->render('senders/confirm.twig.html');
         return;
     }
 }