/** * Sends a message to a contact. * * Depending on the inputs sends a * message/video/image/location message to * a contact. * * @param $toNumbers * @param $message * @param $type * @return array */ public function sendMessage($toNumbers, $message, $type) { $this->connectToWhatsApp(); if (!is_array($toNumbers)) { $toNumbers = array($toNumbers); } $messagesId = array(); foreach ($toNumbers as $to) { $id = null; if ($type === self::MESSAGE_TYPE_TEXT) { $this->wa->sendMessageComposing($to); $id = $this->wa->sendMessage($to, $message); } if ($type === self::MESSAGE_TYPE_IMAGE) { $id = $this->wa->sendMessageImage($to, $message); } if ($type === self::MESSAGE_TYPE_AUDIO) { $id = $this->wa->sendMessageAudio($to, $message); } if ($type === self::MESSAGE_TYPE_VIDEO) { $id = $this->wa->sendMessageVideo($to, $message); } if ($type === self::MESSAGE_TYPE_LOCATION) { $id = $this->wa->sendMessageLocation($to, $message['userlong'], $message['userlat'], $message['locationname'], null); } $messagesId[$to] = $id; } return $messagesId; }
/** IMPLEMENTS SEND MESSAGES **/ protected function sendFiles($to, $files) { set_time_limit(600); $explode = explode('.', $files['image']['name']); $extensao = end($explode); $extensao = strtolower($extensao); define('CAMINHO_MIDIA', 'c:/xampp5-6/htdocs/whatsApi/public/img/send-messages/'); if (in_array($extensao, array('3gp', 'mp4', 'mov', 'avi'))) { $newName = md5($files['image']['name'] . date('Ymdhis')) . "." . $extensao; $pathImage = CAMINHO_MIDIA . $newName; $urlMain = $_SERVER['HTTP_ORIGIN'] . '/img/send-messages/' . $newName; if (move_uploaded_file($files['image']['tmp_name'], $pathImage)) { $this->managerWhats->sendMessageVideo($to, $urlMain); $this->managerWhats->pollMessage(); } else { $this->setLogTalk("Enviar image:" . $to, "Não foi possível enviar a mídia"); } } if (in_array($extensao, array('jpg', 'jpeg', 'gif', 'png'))) { $newName = md5($files['image']['name'] . date('Ymdhis')) . "." . $extensao; $pathImage = CAMINHO_MIDIA . $newName; $urlMain = $_SERVER['HTTP_ORIGIN'] . '/img/send-messages/' . $newName; if (move_uploaded_file($files['image']['tmp_name'], $pathImage)) { $this->managerWhats->sendMessageImage($to, $urlMain); $this->managerWhats->pollMessage(); } else { $this->setLogTalk("Enviar image:" . $to, "Não foi possível enviar a mídia"); } } if (in_array($extensao, array('3gp', 'caf', 'wav', 'mp3', 'wma', 'ogg', 'aif', 'aac', 'm4a'))) { $newName = md5($_FILES['image']['name'] . date('Ymdhis')) . "." . $extensao; $pathImage = CAMINHO_MIDIA . $newName; $urlMain = $_SERVER['HTTP_ORIGIN'] . '/img/send-messages/' . $newName; if (move_uploaded_file($files['image']['tmp_name'], $pathImage)) { $this->managerWhats->sendMessageAudio($to, $urlMain); $this->managerWhats->pollMessage(); } else { $this->setLogTalk("Enviar image:" . $to, "Não foi possível enviar a mídia"); } } return true; }
case '/sendimage': echo "\nEnter the URL or path of the image > "; $filepath = trim(fgets(STDIN)); echo "\nSay something about the image > "; $caption = trim(fgets(STDIN)); $target = $contact; $w->sendMessageImage($target, $filepath, false, $fsize, $fhash, $caption); $w->pollMessage(); break; case '/sendvideo': echo "\nEnter the URL or path of the video > "; $filepath = trim(fgets(STDIN)); echo "\nSay something about the video > "; $caption = trim(fgets(STDIN)); $target = $contact; $w->sendMessageVideo($target, $filepath, false, $fsize, $fhash, $caption); $w->pollMessage(); break; ////////////////////////// test /////////////////// ////////////////////////// test /////////////////// case '/block': echo "< User is now blocked >\n"; $w->sendMessagePaused($contact); $compose = true; $blocked = privacySettings($contact, 'block'); $w->sendSetPrivacyBlockedList($blocked); break; case '/unblock': echo "< User is now unblocked >\n"; $w->sendMessagePaused($contact); $compose = true;