forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
343 lines
9.8 KiB
343 lines
9.8 KiB
<?php |
|
|
|
namespace app\Providers; |
|
|
|
use app\Interfaces\IApiMedia; |
|
|
|
class Positus extends Requests implements IApiMedia |
|
{ |
|
/** @var string $hook resposta do webhook */ |
|
private $hook; |
|
|
|
function getContentType() |
|
{ |
|
return "application/json; charset=UTF-8"; |
|
} |
|
|
|
public function getchannel() |
|
{ |
|
return CONF_WHATSAPP_CHANNEL; |
|
} |
|
|
|
function setHook($hook) |
|
{ |
|
$this->hook = json_decode($hook, true); |
|
} |
|
|
|
function enviarMedia($whatsapp, $link, $type, $titulo = null) |
|
{ |
|
$tipos = []; |
|
$tipos['link'] = $link; |
|
if (!empty($titulo)) { |
|
$tipos['caption'] = $titulo; |
|
} |
|
$this->debug = debug_backtrace(); |
|
$this->params = [ |
|
"to" => "+$whatsapp", |
|
"type" => "$type", |
|
"$type" => $tipos |
|
]; |
|
$this->requestType("POST"); |
|
$this->setMetodo('messages'); |
|
return $this->exec(); |
|
} |
|
|
|
function enviarMsg($whatsapp, $mensagem, $encode = true) |
|
{ |
|
if ($encode) { |
|
$mensagem = utf8_encode($mensagem); |
|
} |
|
$this->debug = debug_backtrace(); |
|
$this->params = array( |
|
"to" => "+$whatsapp", |
|
"type" => "text", |
|
"text" => array("body" => "$mensagem") |
|
); |
|
$this->requestType("POST"); |
|
$this->setMetodo('messages'); |
|
return $this->exec(); |
|
} |
|
|
|
function enviarMsgIterativaLista($whatsapp, $mensagem, $nomeButton, $lista, $prex = '') |
|
{ |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
"to" => "+$whatsapp", |
|
"type" => "interactive", |
|
"interactive" => array( |
|
"type" => "list", |
|
"body" => array( |
|
"text" => utf8_encode($mensagem) |
|
), |
|
"action" => array( |
|
"button" => utf8_encode($nomeButton), |
|
"sections" => $this->montaSection($lista, $prex) |
|
) |
|
) |
|
); |
|
$this->requestType("POST"); |
|
$this->setMetodo('messages'); |
|
$ret = $this->exec(); |
|
return $ret; |
|
} |
|
return false; |
|
} |
|
function montaSection($lista, $prex = '') |
|
{ |
|
$sections = []; |
|
for ($i = 0; $i < count($lista); $i++) { |
|
array_push( |
|
$sections, |
|
array( |
|
"id" => "$prex$i", |
|
"title" => utf8_encode("{$lista[$i]['title']}"), |
|
"description" => utf8_encode("{$lista[$i]['sub']}") |
|
) |
|
); |
|
} |
|
return array( |
|
array( |
|
"title" => utf8_encode("Comandos"), |
|
"rows" => $sections |
|
), |
|
|
|
); |
|
} |
|
|
|
function enviarMsgIterativaBotao($whatsapp, $mensagem, $buttons) |
|
{ |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
"to" => "+$whatsapp", |
|
"type" => "interactive", |
|
"recipient_type" => "individual", |
|
"interactive" => array( |
|
"type" => "button", |
|
"body" => array( |
|
"text" => utf8_encode($mensagem) |
|
), |
|
"action" => array( |
|
"buttons" => $buttons |
|
) |
|
) |
|
); |
|
$this->requestType("POST"); |
|
$this->setMetodo('messages'); |
|
$ret = $this->exec(); |
|
return $ret; |
|
} |
|
return false; |
|
} |
|
|
|
|
|
function enviarContato($whatsapp, $nome, $contato) |
|
{ |
|
$this->debug = debug_backtrace(); |
|
if ($whatsapp) { |
|
$this->params = array( |
|
"to" => "+$whatsapp", |
|
"type" => "contacts", |
|
"contacts" => array( |
|
array( |
|
"name" => array( |
|
"first_name" => "$nome", |
|
"formatted_name" => "$nome" |
|
), |
|
"phones" => array( |
|
array( |
|
"phone" => "+$contato", |
|
"type" => "CELL", |
|
"wa_id" => "$contato" |
|
) |
|
) |
|
) |
|
) |
|
); |
|
$this->requestType("POST"); |
|
$this->setMetodo('messages'); |
|
return $this->exec(); |
|
} |
|
return false; |
|
} |
|
|
|
function enviarLocalizacao($whatsapp, $longitude, $latitude, $nome = null, $endereco = null) |
|
{ |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
"to" => "+$whatsapp", |
|
"type" => "location", |
|
"location" => array( |
|
"longitude" => "$longitude", |
|
"latitude" => "$latitude", |
|
"name" => "$nome", |
|
"address" => utf8_encode("$endereco") |
|
) |
|
); |
|
$this->requestType("POST"); |
|
$this->setMetodo('messages'); |
|
return $this->exec(); |
|
} |
|
return false; |
|
} |
|
|
|
function baixarMidia() |
|
{ |
|
if (in_array($this->getType(), ['location', 'contacts', 'text'])) { |
|
return true; |
|
} |
|
$name = $this->getId(); |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->requestType("GET"); |
|
$this->setMetodo('media/' . $name); |
|
$pathfile = $this->storage . $name; |
|
$retorno = $this->exec(); |
|
file_put_contents($pathfile, $retorno); |
|
if (file_exists($pathfile)) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Profile WhatsApp |
|
* @return string |
|
*/ |
|
public function getProfile() |
|
{ |
|
return $this->hook['contacts'][0]['profile']['name']; |
|
} |
|
|
|
/** |
|
* Phone WhatsApp |
|
* @return string |
|
*/ |
|
public function getPhone() |
|
{ |
|
return $this->hook['contacts'][0]['wa_id']; |
|
} |
|
|
|
/** |
|
* Returns the type of the message |
|
* @return string|boolean |
|
*/ |
|
public function getType() |
|
{ |
|
if ($this->hook) { |
|
return $this->hook['messages'][0]['type']; |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Returns the mime of the message |
|
* @return string|boolean |
|
*/ |
|
public function getMimetype() |
|
{ |
|
if ($this->hook['messages'][0][$this->getType()]['mime_type']) { |
|
return $this->hook['messages'][0][$this->getType()]['mime_type']; |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Returns the id of the message |
|
* @return string|boolean |
|
*/ |
|
public function getId() |
|
{ |
|
if ($this->hook && $this->getType()) { |
|
if ($this->getType() == 'text') { |
|
return $this->hook['messages'][0]['id']; |
|
} |
|
return $this->hook['messages'][0][$this->getType()]['id']; |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Returns the (text, body) of the message |
|
* @return string|boolean |
|
*/ |
|
public function getIsValidMessage() |
|
{ |
|
return $this->hook['contacts']; |
|
} |
|
|
|
/** |
|
* Returns the (text, body) of the message |
|
* @return string|boolean |
|
*/ |
|
public function getMessage() |
|
{ |
|
if ($this->hook['messages'][0]['interactive']) { |
|
if ($this->hook['messages'][0]['interactive']['list_reply']) { |
|
$id = $this->hook['messages'][0]['interactive']['list_reply']['id']; |
|
if (strpos($id, 'P') === 0) { |
|
return $this->hook['messages'][0]['interactive']['list_reply']['description']; |
|
} |
|
return $this->hook['messages'][0]['interactive']['list_reply']['title']; |
|
} else { |
|
return $this->hook['messages'][0]['interactive']['button_reply']['title']; |
|
} |
|
} |
|
$message = $this->hook['messages'][0]['text']['body']; |
|
return ($message ? $message : false); |
|
} |
|
public function setMessage($msg) |
|
{ |
|
$this->hook['messages'][0]['text']['body'] = $msg; |
|
} |
|
/** |
|
* Returns the name of the contact |
|
* @return string|boolean |
|
*/ |
|
public function getContactFormatted() |
|
{ |
|
$formatted = $this->hook['messages'][0]['contacts'][0]['name']['formatted_name']; |
|
if ($formatted) { |
|
return $formatted; |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Returns the phone of the contact |
|
* @return string|boolean |
|
*/ |
|
public function getContactPhone() |
|
{ |
|
$contact = $this->hook['messages'][0]['contacts'][0]['phones'][0]['wa_id']; |
|
if ($contact) { |
|
return $contact; |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Returns the latitude|longitude of the location |
|
* @return string|boolean |
|
*/ |
|
public function getGeolocation($type) |
|
{ |
|
$geolocation = $this->hook['messages'][0]['location'][strtolower($type)]; |
|
if ($geolocation) { |
|
return $geolocation; |
|
} |
|
return false; |
|
} |
|
|
|
|
|
public function retornaTituloDocument() |
|
{ |
|
if ($this->hook['messages'][0]['document']) { |
|
return $this->hook['messages'][0]['document']['filename']; |
|
} |
|
return null; |
|
} |
|
} |