public function commutator()
{
// verificar si tiene una sessi�n
// si no tiene solicitar ID
}
public function closeSesion($chatId){
$close = cerrarSesion($chatId);
$this->sendMessage($message['chatId'], 'Gracias por utilizar nuestros servicios'.$message['chatId'].'@');
}
//this function calls function sendRequest to send a simple message
//@param $chatId [string] [required] - the ID of chat where we send a message
//@param $text [string] [required] - text of the message
public function welcome($chatId, $session)
{
$welcomeString = "Bienvenido a *Gresst Bot*\n";
if(count($session['Sessions'])>0){
$welcomeString .= "Menú\n0. Finalizar sesión\n\n";
$welcomeString .= "Tienes una sesión activa = " . $chatId;
}else{
// Crear sesión step 0
crearSesion($chatId, '0');
$welcomeString .= "Indicanos tu numero de identificación";
}
// Responde al cliente
$this->sendMessage($chatId, $welcomeString);
}
//sends Id of the current chat. it is called when the bot gets the command "chatId"
//@param $chatId [string] [required] - the ID of chat where we send a message
public function showChatId($chatId)
{
$this->sendMessage($chatId, 'ChatID: ' . $chatId);
}
//sends current server time. it is called when the bot gets the command "time"
//@param $chatId [string] [required] - the ID of chat where we send a message
public function time($chatId)
{
$this->sendMessage($chatId, date('d.m.Y H:i:s'));
}
//sends your nickname. it is called when the bot gets the command "me"
//@param $chatId [string] [required] - the ID of chat where we send a message
//@param $name [string] [required] - the "senderName" property of the message
public function me($chatId, $name)
{
$this->sendMessage($chatId, $name);
}
//sends a file. it is called when the bot gets the command "file"
//@param $chatId [string] [required] - the ID of chat where we send a message
//@param $format [string] [required] - file format, from the params in the message body (text[1], etc)
public function file($chatId, $format)
{
$availableFiles = array(
'doc' => 'document.doc',
'gif' => 'gifka.gif',
'jpg' => 'jpgfile.jpg',
'png' => 'pngfile.png',
'pdf' => 'presentation.pdf',
'mp4' => 'video.mp4',
'mp3' => 'mp3file.mp3',
);
if (isset($availableFiles[$format])) {
$data = array(
'chatId' => $chatId,
'body' => 'https://domain.com/PHP/' . $availableFiles[$format],
'filename' => $availableFiles[$format],
'caption' => 'Get your file ' . $availableFiles[$format],
);
$this->sendRequest('sendFile', $data);}}
//sends a voice message. it is called when the bot gets the command "ptt"
//@param $chatId [string] [required] - the ID of chat where we send a message
public function ptt($chatId)
{
$data = array(
'audio' => 'https://domain.com/PHP/ptt.ogg',
'chatId' => $chatId,
);
$this->sendRequest('sendAudio', $data);}
//sends a location. it is called when the bot gets the command "geo"
//@param $chatId [string] [required] - the ID of chat where we send a message
public function geo($chatId)
{
$data = array(
'lat' => 51.51916,
'lng' => -0.139214,
'address' => '??? ?????',
'chatId' => $chatId,
);
$this->sendRequest('sendLocation', $data);}
//creates a group. it is called when the bot gets the command "group"
//@param chatId [string] [required] - the ID of chat where we send a message
//@param author [string] [required] - "author" property of the message
public function group($author)
{
$phone = str_replace('@c.us', '', $author);
$data = array(
'groupName' => 'Group with the bot PHP',
'phones' => array($phone),
'messageText' => 'It is your group. Enjoy',
);
$this->sendRequest('group', $data);}
public function sendMessage($chatId, $text)
{
$data = array('chatId' => $chatId, 'body' => $text);
$this->sendRequest('message', $data);}
public function sendRequest($method, $data)
{
$url = $this->APIurl . $method . '?token=' . $this->token;
if (is_array($data)) {$data = json_encode($data);}
$options = stream_context_create(['http' => [
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $data]]);
$response = file_get_contents($url, false, $options);
file_put_contents('requests.log', $response . PHP_EOL, FILE_APPEND);}