Second Pust, Big Change
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace DBot\Telegram;
|
||||
|
||||
class Base
|
||||
{
|
||||
protected $_fp;
|
||||
public function __construct($Socket)
|
||||
{
|
||||
$this->_fp = stream_socket_client($Socket);
|
||||
if ($this->_fp === false) {
|
||||
throw new ClientException('Could not connect to socket "' . $Socket . '"');
|
||||
}
|
||||
stream_set_timeout($this->_fp, 1);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
fclose($this->_fp);
|
||||
}
|
||||
|
||||
|
||||
public function exec($command)
|
||||
{
|
||||
fwrite($this->_fp, str_replace("\n", '\n', $command) . PHP_EOL);
|
||||
|
||||
$answer = fgets($this->_fp);
|
||||
if (is_string($answer)) {
|
||||
if (substr($answer, 0, 7) === 'ANSWER ') {
|
||||
$bytes = (int) substr($answer, 7);
|
||||
if ($bytes > 0) {
|
||||
$Response = trim(fread($this->_fp, $bytes + 1));
|
||||
return $Response;
|
||||
}
|
||||
}elseif ($answer === PHP_EOL) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function escapeStringArgument($argument)
|
||||
{
|
||||
return '"' . addslashes($argument) . '"';
|
||||
}
|
||||
|
||||
public function escapePeer($peer)
|
||||
{
|
||||
return str_replace(' ', '_', $peer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace DBot\Telegram;
|
||||
|
||||
class Client extends Base
|
||||
{
|
||||
public function setStatusOnline()
|
||||
{
|
||||
return $this->exec('status_online');
|
||||
}
|
||||
|
||||
public function setStatusOffline()
|
||||
{
|
||||
return $this->exec('status_offline');
|
||||
}
|
||||
|
||||
|
||||
public function msg($peer, $msg)
|
||||
{
|
||||
$peer = $this->escapePeer($peer);
|
||||
$msg = $this->escapeStringArgument($msg);
|
||||
return $this->exec('msg ' . $peer . ' ' . $msg);
|
||||
}
|
||||
|
||||
public function addContact($phoneNumber, $firstName, $lastName)
|
||||
{
|
||||
$phoneNumber = preg_replace('%[^0-9]%', '', (string) $phoneNumber);
|
||||
if (empty($phoneNumber)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->exec('add_contact ' . $phoneNumber . ' ' . $this->escapeStringArgument($firstName)
|
||||
. ' ' . $this->escapeStringArgument($lastName));
|
||||
}
|
||||
|
||||
public function renameContact($contact, $firstName, $lastName)
|
||||
{
|
||||
return $this->exec('rename_contact ' . $this->escapePeer($contact)
|
||||
. ' ' . $this->escapeStringArgument($firstName) . ' ' . $this->escapeStringArgument($lastName));
|
||||
}
|
||||
|
||||
public function deleteContact($contact)
|
||||
{
|
||||
return $this->exec('del_contact ' . $this->escapePeer($contact));
|
||||
}
|
||||
|
||||
public function markRead($peer)
|
||||
{
|
||||
return $this->exec('mark_read ' . $this->escapePeer($peer));
|
||||
}
|
||||
|
||||
public function getContactList()
|
||||
{
|
||||
return explode(PHP_EOL, $this->exec('contact_list'));
|
||||
}
|
||||
|
||||
public function getUserInfo($user)
|
||||
{
|
||||
return $this->exec('user_info ' . $this->escapePeer($user));
|
||||
}
|
||||
|
||||
public function getDialogList()
|
||||
{
|
||||
return explode(PHP_EOL, $this->exec('dialog_list'));
|
||||
}
|
||||
|
||||
public function getHistory($peer, $limit = null, $offset = null)
|
||||
{
|
||||
if ($limit !== null) {
|
||||
$limit = (int) $limit;
|
||||
if ($limit < 1) { //if limit is lesser than 1, telegram-cli crashes
|
||||
$limit = 1;
|
||||
}
|
||||
$limit = ' ' . $limit;
|
||||
} else {
|
||||
$limit = '';
|
||||
}
|
||||
if ($offset !== null) {
|
||||
$offset = ' ' . (int) $offset;
|
||||
} else {
|
||||
$offset = '';
|
||||
}
|
||||
|
||||
return $this->exec('history ' . $this->escapePeer($peer) . $limit . $offset);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user