|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\Providers;
|
|
|
|
|
|
|
|
|
|
use app\Interfaces\IApiMedia;
|
|
|
|
|
use app\Providers\RequestURL;
|
|
|
|
|
|
|
|
|
|
class ApiTelegram implements IApiMedia
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private $token;
|
|
|
|
|
private $url;
|
|
|
|
|
private $metodo;
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
## VARIAVEIS DA CLASSE ##
|
|
|
|
|
########################################################################
|
|
|
|
|
private $query;
|
|
|
|
|
private $requestType;
|
|
|
|
|
private $request;
|
|
|
|
|
private $params = array();
|
|
|
|
|
private $hook;
|
|
|
|
|
private $storage = "/var/www/html/aplicativo/audio/";
|
|
|
|
|
public $channel = CONF_TELEGRAM_CHANNEL;
|
|
|
|
|
public $timeout_client_resposta = CONF_TELEGRAM_TIMEOUT_CLIENT_RESPOSTA;
|
|
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->token = CONF_TELEGRAM_AUTH_TOKEN;
|
|
|
|
|
$this->url = CONF_TELEGRAM_AUTH_URL;
|
|
|
|
|
$this->request = new RequestURL();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function convertToWebsocket($msg)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviarMsgIterativaLista($telegram, $mensagem, $nomeButton, $lista, $prex = '')
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"text" => array("body" => utf8_encode("$mensagem")),
|
|
|
|
|
"options" => array(
|
|
|
|
|
"reply_markup" => array(
|
|
|
|
|
"inline_keyboard" => $this->montaOp($lista, $prex)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendMessage');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
function montaOp($lista, $prex = '')
|
|
|
|
|
{
|
|
|
|
|
$botoes = [];
|
|
|
|
|
for ($i = 0; $i < count($lista); $i++) {
|
|
|
|
|
array_push(
|
|
|
|
|
$botoes,
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
"text" => utf8_encode("{$lista[$i]['title']}"),
|
|
|
|
|
"callback_data" => $prex == "C" ? utf8_encode("{$lista[$i]['title']}") : utf8_encode("{$lista[$i]['sub']}")
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $botoes;
|
|
|
|
|
}
|
|
|
|
|
function enviarMsgIterativaBotao($telegram, $mensagem, $buttons)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"text" => array("body" => utf8_encode("$mensagem")),
|
|
|
|
|
"options" => array(
|
|
|
|
|
"reply_markup" => array(
|
|
|
|
|
"inline_keyboard" => array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
"text" => utf8_encode("/presente"),
|
|
|
|
|
"callback_data" => utf8_encode("/presente")
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendMessage');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
function enviarMsg($telegram, $mensagem)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"text" => array("body" => "$mensagem")
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendMessage');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviarContato($telegram, $nome, $contato)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"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('sendContact');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviarLocalizacao($telegram, $longitude, $latitude, $nome = null, $endereco = null)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"longitude" => "$longitude",
|
|
|
|
|
"latitude" => "$latitude",
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendLocation');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviaDocumento($telegram, $link, $titulo = null)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"file" => "$link",
|
|
|
|
|
"options" => array(
|
|
|
|
|
"caption" => "$titulo"
|
|
|
|
|
),
|
|
|
|
|
"fileOptions" => array(
|
|
|
|
|
"filename" => "$titulo"
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendDocument');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviaImagem($telegram, $link, $titulo = null)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"file" => "$link",
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendPhoto');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviaSticker($telegram, $link)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"file" => "$link",
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendSticker');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviaVideo($telegram, $link, $titulo = null)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"file" => "$link",
|
|
|
|
|
"options" => array(
|
|
|
|
|
"caption" => "$titulo"
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendVideo');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enviaAudio($telegram, $link)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->params = array(
|
|
|
|
|
"chatId" => "$telegram",
|
|
|
|
|
"file" => "$link",
|
|
|
|
|
);
|
|
|
|
|
$this->requestType("POST");
|
|
|
|
|
$this->setMetodo('sendVoice');
|
|
|
|
|
return $this->exec();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function baixarMidia($name)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
if ($this->getArgs(func_get_args())) {
|
|
|
|
|
$this->requestType("GET");
|
|
|
|
|
$this->setMetodo('media/' . $name);
|
|
|
|
|
$file = $this->storage . $name;
|
|
|
|
|
file_put_contents($file, $this->exec());
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
return $file;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setStorage($storage)
|
|
|
|
|
{
|
|
|
|
|
$this->storage = $storage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Profile WhatsApp
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getProfile()
|
|
|
|
|
{
|
|
|
|
|
return $this->hook['from']['first_name'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Phone WhatsApp
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getPhone()
|
|
|
|
|
{
|
|
|
|
|
if ($this->hook['chat']['id']) {
|
|
|
|
|
return $this->hook['chat']['id'];
|
|
|
|
|
} else {
|
|
|
|
|
return $this->hook['from']['id'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the type of the message
|
|
|
|
|
* @return string|boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getType()
|
|
|
|
|
{
|
|
|
|
|
if ($this->hook) {
|
|
|
|
|
if (array_key_exists("contact", $this->hook)) {
|
|
|
|
|
return 'contacts';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("location", $this->hook)) {
|
|
|
|
|
return 'location';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("document", $this->hook)) {
|
|
|
|
|
return 'document';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("audio", $this->hook)) {
|
|
|
|
|
return 'audio';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("video", $this->hook)) {
|
|
|
|
|
return 'video';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("sticker", $this->hook)) {
|
|
|
|
|
return 'sticker';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("photo", $this->hook)) {
|
|
|
|
|
return 'image';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("text", $this->hook)) {
|
|
|
|
|
return 'text';
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists("data", $this->hook)) {
|
|
|
|
|
return 'text';
|
|
|
|
|
}
|
|
|
|
|
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 base64_encode($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()) {
|
|
|
|
|
return $this->hook['messages'][0][$this->getType()]['id'];
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Returns the (text, body) of the message
|
|
|
|
|
* @return string|boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getIsValidMessage()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Returns the (text, body) of the message
|
|
|
|
|
* @return string|boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getMessage()
|
|
|
|
|
{
|
|
|
|
|
if ($this->hook['data']) {
|
|
|
|
|
return $this->hook['data'];
|
|
|
|
|
}
|
|
|
|
|
if ($this->hook['text']) {
|
|
|
|
|
return $this->hook['text'];
|
|
|
|
|
}
|
|
|
|
|
$message = $this->hook['data'];
|
|
|
|
|
return ($message ? $message : false);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 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['location'][strtolower($type)];
|
|
|
|
|
if ($geolocation) {
|
|
|
|
|
return $geolocation;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
function setHook($hook)
|
|
|
|
|
{
|
|
|
|
|
$this->hook = $hook;
|
|
|
|
|
}
|
|
|
|
|
########################################################################
|
|
|
|
|
## FUNCOES DO SISTEMA ##
|
|
|
|
|
########################################################################
|
|
|
|
|
function setMetodo($metodo)
|
|
|
|
|
{
|
|
|
|
|
$this->metodo = $metodo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Escreve a query para ser passada para o curl
|
|
|
|
|
*
|
|
|
|
|
* @param string $query
|
|
|
|
|
*/
|
|
|
|
|
function setQuery($query)
|
|
|
|
|
{
|
|
|
|
|
return $this->query .= $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* retorna a string pronta da query do curl e limpa a variavel.
|
|
|
|
|
*
|
|
|
|
|
* @return string $query
|
|
|
|
|
*/
|
|
|
|
|
function getQuery()
|
|
|
|
|
{
|
|
|
|
|
$query = $this->query;
|
|
|
|
|
unset($this->query);
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verifica se todos os parametros passados foram completados.
|
|
|
|
|
*
|
|
|
|
|
* @param array $args
|
|
|
|
|
* @return true|false
|
|
|
|
|
*/
|
|
|
|
|
function getArgs($args)
|
|
|
|
|
{
|
|
|
|
|
foreach ($args as $value) {
|
|
|
|
|
if (!$value) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recebe o tipo de Requisi<EFBFBD><EFBFBD>o GET/POST
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
function requestType($req = null)
|
|
|
|
|
{
|
|
|
|
|
if (!$req) {
|
|
|
|
|
return $this->requestType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strtoupper($req) == "GET") {
|
|
|
|
|
return $this->requestType = "GET";
|
|
|
|
|
} else if (strtoupper($req) == "POST") {
|
|
|
|
|
return $this->requestType = "POST";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function exec()
|
|
|
|
|
{
|
|
|
|
|
$this->setQuery(json_encode($this->params)); //SET QUERY
|
|
|
|
|
$this->request->setUrl($this->url . $this->metodo);
|
|
|
|
|
|
|
|
|
|
$header = array();
|
|
|
|
|
//$header[] = "Authorization: Bearer {$this->token}";
|
|
|
|
|
if ($this->requestType == 'POST') {
|
|
|
|
|
$header[] = 'Content-Type: application/json';
|
|
|
|
|
$this->request->post_field($this->getQuery(), true);
|
|
|
|
|
}
|
|
|
|
|
$this->request->header($header);
|
|
|
|
|
$this->request->method_request($this->requestType);
|
|
|
|
|
|
|
|
|
|
$response = $this->request->exec_request();
|
|
|
|
|
return $this->response($response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function response($result)
|
|
|
|
|
{
|
|
|
|
|
if ($result) {
|
|
|
|
|
if (json_decode($result, true) !== null) {
|
|
|
|
|
return json_decode($result, true);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getLinkDownload($host)
|
|
|
|
|
{
|
|
|
|
|
if ($this->hook['photo']) {
|
|
|
|
|
return end($this->hook['photo'])['file_id'];
|
|
|
|
|
}
|
|
|
|
|
if ($this->hook['sticker']) {
|
|
|
|
|
return $this->hook['sticker']['file_id'];
|
|
|
|
|
}
|
|
|
|
|
if ($this->hook['video']) {
|
|
|
|
|
return $this->hook['video']['file_id'];
|
|
|
|
|
}
|
|
|
|
|
if ($this->hook['audio']) {
|
|
|
|
|
return $this->hook['audio']['file_id'];
|
|
|
|
|
}
|
|
|
|
|
if ($this->hook['document']) {
|
|
|
|
|
return $this->hook['document']['file_id'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public function retornaTituloDocument($msg)
|
|
|
|
|
{
|
|
|
|
|
return $this->hook['document']['file_name'];
|
|
|
|
|
}
|
|
|
|
|
}
|