@@ -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}
0 commit comments