361 lines
11 KiB
PHP
361 lines
11 KiB
PHP
<?php
|
||
|
||
|
||
class Wazzap {
|
||
|
||
public function __construct($agency_id, $db = null, $debug = false) {
|
||
if ($debug == true) {
|
||
$this->debug = true;
|
||
error_reporting(E_ALL | E_STRICT);
|
||
ini_set('display_errors', 1);
|
||
}
|
||
|
||
if(!isset($agency_id) || (int)$agency_id == 0){
|
||
die('Не задано агентство');
|
||
}
|
||
|
||
$this->agency_id = $agency_id;
|
||
|
||
if (!is_null($db)) {
|
||
$this->db = $db;
|
||
} else {
|
||
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
||
$pdo->query("SET NAMES 'utf8'");
|
||
$this->db = $pdo;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Сохраненние ключа
|
||
*/
|
||
public function save_key($key){
|
||
$sql_up = "UPDATE users SET wazzap_api_key = '{$key}' WHERE id = {$this->agency_id}";
|
||
$this->db->query($sql_up);
|
||
}
|
||
|
||
/**
|
||
* Получение ключа
|
||
*/
|
||
public function get_key() {
|
||
$api_key = '';
|
||
$sql = "SELECT wazzap_api_key FROM users WHERE id = {$this->agency_id}";
|
||
|
||
$q = $this->db->query($sql);
|
||
$r = $this->db->fetch_assoc($q);
|
||
if(isset($r['wazzap_api_key'])){
|
||
$api_key = $r['wazzap_api_key'];
|
||
|
||
}
|
||
return $api_key;
|
||
}
|
||
|
||
public function getGlobalWidget($jwUserId, $scope, $chatType, $chatId, $channelId) {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$sql = "SELECT id, last_name, first_name, middle_name, phone FROM users WHERE id = {$jwUserId}";
|
||
|
||
$q = $this->db->query($sql);
|
||
$user = $this->db->fetch_assoc($q);
|
||
|
||
$username = $user['last_name'] . " " . $user['first_name'];
|
||
|
||
if ($user['middle_name']) {
|
||
$username = $username . " " . $user['middle_name'];
|
||
}
|
||
|
||
$url = 'https://api.wazzup24.com/v3/iframe';
|
||
|
||
$header = array('Content-type:application/json', 'Authorization: Bearer ' . $api_key);
|
||
|
||
if ($scope == 'global') {
|
||
$post = array(
|
||
'user' => array(
|
||
'id' => strval($user['id']),
|
||
'name' => $username,
|
||
),
|
||
'scope' => 'global',
|
||
'options' => array(
|
||
'useDealsEvents' => true,
|
||
'useMessageEvents' => true
|
||
)
|
||
);
|
||
} else {
|
||
if ($scope == 'card') {
|
||
if ($channelId) {
|
||
$post = array(
|
||
'user' => array(
|
||
'id' => strval($user['id']),
|
||
'name' => $username,
|
||
),
|
||
'scope' => 'card',
|
||
'options' => array(
|
||
'useDealsEvents' => true,
|
||
'useMessageEvents' => true
|
||
),
|
||
'filter' => array(array(
|
||
'chatType' => $chatType,
|
||
'chatId' => $chatId
|
||
)
|
||
));
|
||
} else {
|
||
$post = array(
|
||
'user' => array(
|
||
'id' => strval($user['id']),
|
||
'name' => $username,
|
||
),
|
||
'scope' => 'card',
|
||
'options' => array(
|
||
'useDealsEvents' => true,
|
||
'useMessageEvents' => true
|
||
),
|
||
'filter' => array(array(
|
||
'chatType' => $chatType,
|
||
'chatId' => $chatId
|
||
)
|
||
));
|
||
}
|
||
}
|
||
}
|
||
|
||
$data = Wazzap::sendCurl($url, json_encode($post), $header);
|
||
|
||
$json = json_decode($data[0], true);
|
||
|
||
if ($data[1] == 200) {
|
||
return $json['url'];
|
||
} else {
|
||
return "Ошибка: " . $json['error'];
|
||
}
|
||
} else {
|
||
return "Ошибка: не задан ключ АПИ";
|
||
}
|
||
}
|
||
|
||
public function getCounterHost() {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$url = 'https://integrations.wazzup24.com/counters/ws_host/api_v3/' . $api_key;
|
||
$data = Wazzap::sendCurl($url);
|
||
|
||
$json = json_decode($data[0], true);
|
||
|
||
if ($data[1] == 200) {
|
||
return '{"host" : "' . $json['host'] . '", "key" : "' . $api_key . '"}';
|
||
} else {
|
||
return "";
|
||
}
|
||
} else {
|
||
return "";
|
||
}
|
||
}
|
||
|
||
public function getChannels() {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$url = 'https://api.wazzup24.com/v3/channels';
|
||
$header = array('Authorization: Bearer ' . $api_key);
|
||
$data = Wazzap::sendCurl($url, false, $header, false);
|
||
|
||
if ($data[1] == 200) {
|
||
return json_decode($data[0], true);
|
||
} else {
|
||
return [];
|
||
}
|
||
} else {
|
||
return "no_key";
|
||
}
|
||
}
|
||
|
||
public function createWazzapUser($jwUserId) {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$sql = "SELECT id, last_name, first_name, middle_name, phone FROM users WHERE id = {$jwUserId}";
|
||
|
||
$q = $this->db->query($sql);
|
||
$user = $this->db->fetch_assoc($q);
|
||
|
||
$url = 'https://api.wazzup24.com/v3/users';
|
||
|
||
$username = $user['last_name'] . " " . $user['first_name'];
|
||
|
||
if ($user['middle_name']) {
|
||
$username = $username . " " . $user['middle_name'];
|
||
}
|
||
|
||
$post = array(
|
||
'id' => strval($user['id']),
|
||
'name' => $username,
|
||
'phone' => $user['phone']
|
||
);
|
||
|
||
$header = array('Content-type:application/json', 'Authorization: Bearer ' . $api_key);
|
||
|
||
$data = Wazzap::sendCurl($url, "[" . json_encode($post) . "]", $header);
|
||
|
||
if ($data[1] == 200) {
|
||
$sqlUpd = "UPDATE users SET added_to_wazzap = 1 WHERE id = {$jwUserId}";
|
||
$this->db->query($sqlUpd);
|
||
|
||
return "done";
|
||
} else {
|
||
$json = json_decode($data[0], true);
|
||
return "Ошибка: " . $json['error'];
|
||
}
|
||
} else {
|
||
return "no_key";
|
||
}
|
||
}
|
||
|
||
public function deleteWazzapUser($jwUserId) {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
|
||
$url = 'https://api.wazzup24.com/v3/users/' . $jwUserId;
|
||
|
||
$header = array('Authorization: Bearer ' . $api_key);
|
||
|
||
$data = Wazzap::sendCurl($url, false, $header, true);
|
||
|
||
if ($data[1] == 200) {
|
||
$sqlUpd = "UPDATE users SET added_to_wazzap = 0 WHERE id = {$jwUserId}";
|
||
$this->db->query($sqlUpd);
|
||
|
||
return "done";
|
||
} else {
|
||
$json = json_decode($data[0], true);
|
||
return "Ошибка: " . $json['error'];
|
||
}
|
||
} else {
|
||
return "no_key";
|
||
}
|
||
}
|
||
|
||
public function createWazzapContact($clientId, $userId, $clientName, $chatType, $chatId) {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$url = 'https://api.wazzup24.com/v3/contacts';
|
||
|
||
$post = array(
|
||
'id' => strval($clientId),
|
||
'responsibleUserId' => strval($userId),
|
||
'name' => $clientName,
|
||
'uri' => '#client',
|
||
'contactData' => array(array(
|
||
'chatType' => strval($chatType),
|
||
'chatId' => strval($chatId)
|
||
))
|
||
);
|
||
|
||
$header = array('Content-type:application/json', 'Authorization: Bearer ' . $api_key);
|
||
|
||
$data = Wazzap::sendCurl($url, "[" . json_encode($post) . "]", $header);
|
||
|
||
if ($data[1] == 200) {
|
||
return "done";
|
||
} else {
|
||
$json = json_decode($data[0], true);
|
||
return "Ошибка: " . $json['error'];
|
||
}
|
||
} else {
|
||
return "no_key";
|
||
}
|
||
}
|
||
|
||
public function createWazzapDeal($requisitionId, $userId, $requisitionName, $clientId) {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$url = 'https://api.wazzup24.com/v3/deals';
|
||
|
||
$post = array(
|
||
'id' => strval($requisitionId),
|
||
'responsibleUserId' => strval($userId),
|
||
'name' => $requisitionName,
|
||
'contacts' => array_values(array(strval($clientId))),
|
||
'uri' => '#req',
|
||
'closed' => false
|
||
);
|
||
|
||
$header = array('Content-type:application/json', 'Authorization: Bearer ' . $api_key);
|
||
|
||
$data = Wazzap::sendCurl($url, "[" . json_encode($post) . "]", $header);
|
||
|
||
if ($data[1] == 200) {
|
||
return "done";
|
||
} else {
|
||
$json = json_decode($data[0], true);
|
||
return "Ошибка: " . $json['error'];
|
||
}
|
||
} else {
|
||
return "no_key";
|
||
}
|
||
}
|
||
|
||
public function sendMessage($channelId, $phone, $text, $fileUrl, $crmMessageId) {
|
||
$api_key = $this->get_key();
|
||
|
||
if ($api_key) {
|
||
$url = 'https://api.wazzup24.com/v3/message';
|
||
|
||
$post = array(
|
||
'channelId' => strval($channelId),
|
||
'chatType' => strval('whatsapp'),
|
||
'chatId' => strval($phone),
|
||
'text' => strval($text),
|
||
'contentUri' => strval($fileUrl),
|
||
'crmMessageId' => strval($crmMessageId)
|
||
);
|
||
|
||
$header = array('Content-type:application/json', 'Authorization: Bearer ' . $api_key);
|
||
|
||
$data = Wazzap::sendCurl($url, json_encode($post), $header);
|
||
|
||
if ($data[1] == 201) {
|
||
return "done";
|
||
} else {
|
||
$json = json_decode($data[0], true);
|
||
return "Ошибка: " . $json['error'] . ", текст: " . $data[0];
|
||
}
|
||
} else {
|
||
return "no_key";
|
||
}
|
||
}
|
||
|
||
private static function sendCurl($url, $post = false, $header = false, $delete = false){
|
||
$ch = curl_init($url);
|
||
//debug
|
||
curl_setopt($ch, CURLOPT_VERBOSE, true);
|
||
curl_setopt($ch, CURLOPT_STDERR, fopen($_SERVER['DOCUMENT_ROOT'] . '/curl_wazzap.txt', 'a+'));
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
|
||
|
||
if ($header) {
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
||
curl_setopt($ch, CURLOPT_HEADER,0);
|
||
}
|
||
if ($post) {
|
||
curl_setopt($ch, CURLOPT_POST, 1);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
|
||
}
|
||
|
||
if ($delete) {
|
||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
|
||
}
|
||
|
||
$response = curl_exec($ch);
|
||
|
||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
||
curl_close($ch);
|
||
|
||
return [$response, $http_code];
|
||
}
|
||
|
||
} |