Skip to content

Commit 61eff7a

Browse files
committed
Adapt for pwrtelegram
1 parent 560a91a commit 61eff7a

File tree

9 files changed

+121
-8
lines changed

9 files changed

+121
-8
lines changed

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

composer.json

100644100755
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
{
2-
"name": "zyberspace/telegram-cli-client",
3-
"description": "php-client for telegram-cli",
2+
"name": "pwrtelegram/telegram-cli-client",
3+
"description": "Modified version php-client for telegram-cli used by the PWRTelegram bot API",
44
"type": "library",
5-
"homepage": "https://github.com/zyberspace/php-telegram-cli-client",
5+
"homepage": "https://github.com/pwrtelegram/php-telegram-cli-client",
66
"license": "MPL-2.0",
77
"authors": [
88
{
99
"name": "zyberspace",
1010
"email": "[email protected]"
1111
}
12+
{
13+
"name": "Daniil Gentili",
14+
"email": "[email protected]"
15+
}
1216
],
1317
"require": {
1418
"php": ">=5.3.0,<6.0.0"

discovery-shell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
require('vendor/autoload.php');
1111

12-
$telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck');
12+
$telegram = new \Zyberspace\Telegram\Cli\Client();
1313

1414
$discoverShell = new \Zyberspace\DiscoveryShell($telegram, 'telegram', __DIR__ . '/.developer-shell-history');
1515
$discoverShell->run();

example.php

100644100755
File mode changed.

lib/Zyberspace/Telegram/Cli/Client.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ public function msg($peer, $msg)
7070
return $this->exec('msg ' . $peer . ' ' . $msg);
7171
}
7272

73+
/**
74+
* Replies tp a text message.
75+
*
76+
* @param string $id The message id that we're replying to
77+
* @param string $msg The message to send, gets escaped with escapeStringArgument()
78+
*
79+
* @return boolean true on success, false otherwise
80+
*
81+
* @uses exec()
82+
* @uses escapeStringArgument()
83+
*/
84+
public function replymsg($id, $msg)
85+
{
86+
$msg = $this->escapeStringArgument($msg);
87+
88+
return $this->exec('reply ' . $id . ' ' . $msg);
89+
}
7390
/**
7491
* Sends a text message to several users at once.
7592
*
@@ -410,4 +427,85 @@ public function sendFile($peer, $path)
410427

411428
return $this->exec('send_file ' . $peer . ' ' . $formattedPath);
412429
}
430+
431+
432+
/**
433+
* Send file to peer and return message id
434+
*
435+
* @param string $peer The peer, gets escaped with escapePeer() and escapeshellarg()
436+
* @param string $type The type of the file to be sent, gets escaped with escapePeer() and escapeshellarg()
437+
* @param string $path The file path, gets formatted with formatFileName() and escapeshellarg()
438+
* @return array
439+
*
440+
* @uses exec()
441+
* @uses escapePeer()
442+
* @uses formatFileName()
443+
* @uses escapeshellarg()
444+
*/
445+
public function pwrsendFile($peer, $type, $path, $hash)
446+
{
447+
$peer = $this->escapePeer($peer);
448+
/* $cmd = "msg " . $peer . " " . $hash;
449+
$res = shell_exec($GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -WNRe " . escapeshellarg($cmd) . " 2>&1");
450+
foreach (explode("\n", $res) as $line) {
451+
if(preg_match('|^{|', $line) && !preg_match('|{"result": "SUCCESS"}|', $line)) $newres = json_decode(preg_replace(array('|^[^{]*{|', "|}[^}]*$|"), array("{", "}"), $line), true); else continue;
452+
if($newres["out"] && $newres["text"] == $hash && $newres["from"]["peer_id"] == $GLOBALS["botusername"]) $msgid = $newres["id"];
453+
}
454+
*/
455+
$formattedPath = $this->formatFileName($path);
456+
$cmd = "send_" . $type . " " . $peer . " " . $formattedPath;
457+
$res = shell_exec($GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -U pwrtelegram -WNRe " . escapeshellarg($cmd) . " 2>&1");
458+
$newres = null;
459+
$finalres = null;
460+
foreach (explode("\n", $res) as $line) {
461+
if(preg_match('|^{|', $line) && !preg_match('|{"result": "SUCCESS"}|', $line)) $newres = json_decode(preg_replace(array('|^[^{]*{|', "|}[^}]*$|"), array("{", "}"), $line), true); else continue;
462+
if(isset($newres["out"]) && $newres["out"] && isset($newres["media"]["type"]) && $newres["media"]["type"] == $type && isset($newres["from"]["peer_id"]) && $newres["from"]["peer_id"] == $GLOBALS["botusername"]) $finalres = $newres;
463+
}
464+
return $newres;
465+
}
466+
467+
/**
468+
* Download file from message id
469+
*
470+
* @param string $type The file type (document, audio, photo, video, voice)
471+
* @param string $id The message's id
472+
* @return array|boolean
473+
*
474+
* @uses exec()
475+
* @uses escapePeer()
476+
*/
477+
public function getdFile($id, $type)
478+
{
479+
$res = shell_exec($GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -WNRe 'load_file $id' 2>&1 | sed 's/[>]//g;/{/!d;/{\"event\": \"download\"/!d;/^\s*$/d;s/^[^{]*{/{/;s/}[^}]*$/}/'");
480+
error_log($res);
481+
return json_decode($res);
482+
}
483+
public function getFile($user, $file_id, $type)
484+
{
485+
$script = escapeshellarg($GLOBALS["pwrhomedir"] . "/lua/download.lua");
486+
$res = shell_exec($GLOBALS["homedir"] . "/tg/bin/telegram-cli --json -WNRs " . $script . " --lua-param ".escapeshellarg($user." ".$file_id." ".$type)." 2>&1");
487+
foreach(explode("\n", $res) as $line) {
488+
if(preg_match('|.*{"event":"download", "result"|', $line)) $res = preg_replace(array('|.*{"event":"download", "result"|', "|}.*|"), array('{"event":"download", "result"', "}"), $line);
489+
}
490+
return json_decode($res);
491+
}
492+
493+
public function oldgetFile($user, $id, $type)
494+
{
495+
496+
return $this->exec('load_' . $type . ' ' . $id);
497+
}
498+
499+
/**
500+
* Get info about current user
501+
*
502+
* @return array|boolean
503+
*
504+
* @uses exec()
505+
* @uses escapePeer()
506+
*/
507+
public function getSelf()
508+
{
509+
return $this->exec('get_self');
510+
}
413511
}

lib/Zyberspace/Telegram/Cli/ClientException.php

100644100755
File mode changed.

lib/Zyberspace/Telegram/Cli/RawClient.php

100644100755
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
88
*/
99
namespace Zyberspace\Telegram\Cli;
10-
1110
/**
1211
* Raw part of the php-client for telegram-cli.
1312
* Takes care of the socket-connection and some helper-methods.
@@ -47,7 +46,11 @@ public function __construct($remoteSocket)
4746
{
4847
$this->_fp = stream_socket_client($remoteSocket);
4948
if ($this->_fp === false) {
50-
throw new ClientException('Could not connect to socket "' . $remoteSocket . '"');
49+
shell_exec("pkill telegram-cli; rm /tmp/tg.sck; " . $GLOBALS["homedir"] . "/tg/bin/telegram-cli --json --permanent-msg-ids -dWS /tmp/tg.sck&");
50+
$this->_fp = stream_socket_client($remoteSocket);
51+
if ($this->_fp === false) {
52+
throw new ClientException('Could not connect to socket "' . $remoteSocket . '"');
53+
}
5154
}
5255
}
5356

@@ -69,10 +72,11 @@ public function __destruct()
6972
public function exec($command)
7073
{
7174
$command = implode(' ', func_get_args());
72-
7375
fwrite($this->_fp, str_replace("\n", '\n', $command) . PHP_EOL);
7476

7577
$answer = fgets($this->_fp); //"ANSWER $bytes" or false if an error occurred
78+
79+
7680
if (is_string($answer)) {
7781
if (substr($answer, 0, 7) === 'ANSWER ') {
7882
$bytes = ((int) substr($answer, 7)) + 1; //+1 because the json-return seems to miss one byte
@@ -87,8 +91,8 @@ public function exec($command)
8791
$bytesRead = strlen($jsonString);
8892
} while ($bytesRead < $bytes);
8993

90-
$json = json_decode($jsonString);
9194

95+
$json = json_decode($jsonString);
9296
if (!isset($json->error)) {
9397
//Reset error-message and error-code
9498
$this->_errorMessage = null;
@@ -156,6 +160,13 @@ public function escapePeer($peer)
156160
{
157161
return str_replace(' ', '_', $peer);
158162
}
163+
public function escapeUsername($username)
164+
{
165+
$peer = $this->exec('resolve_username ' . $username);
166+
if(!isset($peer->print_name) || $peer->print_name == "") return false;
167+
$peer = $peer->print_name;
168+
return str_replace(' ', '_', $peer);
169+
}
159170

160171
/**
161172
* Takes a list of peers and turns it into a format needed by the most commands that handle multiple peers.

phpdoc.dist.xml

100644100755
File mode changed.

0 commit comments

Comments
 (0)