mirror of
https://github.com/danog/pipesbot.git
synced 2024-11-30 04:19:02 +01:00
Cleanup
This commit is contained in:
parent
a8e5d660b6
commit
1b75572b9f
2
.gitignore
vendored
2
.gitignore
vendored
@ -99,7 +99,7 @@ composer.lock
|
|||||||
b.php
|
b.php
|
||||||
*.log
|
*.log
|
||||||
telegram-cli*
|
telegram-cli*
|
||||||
src/danog/MadelineProto/Fuzzer.php
|
src/Fuzzer.php
|
||||||
fuzzer.php
|
fuzzer.php
|
||||||
tests/500mb
|
tests/500mb
|
||||||
*.save
|
*.save
|
||||||
|
115
pipesbot.php
115
pipesbot.php
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pipes bot.
|
* Pipes bot.
|
||||||
*
|
*
|
||||||
@ -15,20 +16,35 @@
|
|||||||
* @author Daniil Gentili <daniil@daniil.it>
|
* @author Daniil Gentili <daniil@daniil.it>
|
||||||
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
|
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
|
||||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||||
*
|
|
||||||
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
||||||
*/
|
*/
|
||||||
|
use danog\MadelineProto\API;
|
||||||
|
use danog\MadelineProto\EventHandler;
|
||||||
|
use danog\MadelineProto\Exception;
|
||||||
|
use danog\MadelineProto\RPCErrorException;
|
||||||
|
|
||||||
if (!\file_exists('madeline.php')) {
|
if (!file_exists('madeline.php')) {
|
||||||
\copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
|
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
|
||||||
}
|
}
|
||||||
include 'madeline.php';
|
include 'madeline.php';
|
||||||
|
|
||||||
|
// Login as a user
|
||||||
|
$u = new API('pipesuser.madeline');
|
||||||
|
if (!$u->getSelf()) {
|
||||||
|
if (!$_GET) {
|
||||||
|
$u->echo("Please login as a user!");
|
||||||
|
}
|
||||||
|
$u->start();
|
||||||
|
}
|
||||||
|
if (!$u->isSelfUser()) {
|
||||||
|
throw new AssertionError("You must login as a user! Please delete the user.madeline folder to continue.");
|
||||||
|
}
|
||||||
|
unset($u);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event handler class.
|
* Event handler class.
|
||||||
*/
|
*/
|
||||||
class pipesbot extends \danog\MadelineProto\EventHandler
|
class pipesbot extends EventHandler
|
||||||
{
|
{
|
||||||
const WELCOME = "This bot can create a pipeline between inline bots.
|
const WELCOME = "This bot can create a pipeline between inline bots.
|
||||||
To use it, simply type an inline query with the following syntax:
|
To use it, simply type an inline query with the following syntax:
|
||||||
@ -58,18 +74,21 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* User instance of MadelineProto.
|
* User instance of MadelineProto.
|
||||||
*
|
|
||||||
* @var \danog\MadelineProto\API
|
|
||||||
*/
|
*/
|
||||||
private $u;
|
private API $u;
|
||||||
|
|
||||||
|
public function onStart(): void
|
||||||
|
{
|
||||||
|
$this->u = new API('pipesuser.madeline');
|
||||||
|
}
|
||||||
|
|
||||||
private function inputify(&$stuff)
|
private function inputify(&$stuff)
|
||||||
{
|
{
|
||||||
$stuff['_'] = 'input'.\ucfirst($stuff['_']);
|
$stuff['_'] = 'input'.ucfirst($stuff['_']);
|
||||||
|
|
||||||
return $stuff;
|
return $stuff;
|
||||||
}
|
}
|
||||||
private function translatetext(&$value)
|
private function translatetext(&$value): void
|
||||||
{
|
{
|
||||||
$this->inputify($value);
|
$this->inputify($value);
|
||||||
if (isset($value['entities'])) {
|
if (isset($value['entities'])) {
|
||||||
@ -93,7 +112,7 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
return $value;
|
return $value;
|
||||||
case 'botInlineMediaResult':
|
case 'botInlineMediaResult':
|
||||||
if (isset($value['game'])) {
|
if (isset($value['game'])) {
|
||||||
throw new \danog\MadelineProto\Exception('Games are not supported.');
|
throw new Exception('Games are not supported.');
|
||||||
}
|
}
|
||||||
if (isset($value['photo'])) {
|
if (isset($value['photo'])) {
|
||||||
$value['_'] = 'inputBotInlineResultPhoto';
|
$value['_'] = 'inputBotInlineResultPhoto';
|
||||||
@ -108,7 +127,7 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
}
|
}
|
||||||
public function onUpdateNewChannelMessage($update)
|
public function onUpdateNewChannelMessage($update)
|
||||||
{
|
{
|
||||||
yield $this->onUpdateNewMessage($update);
|
$this->onUpdateNewMessage($update);
|
||||||
}
|
}
|
||||||
public function onUpdateNewMessage($update)
|
public function onUpdateNewMessage($update)
|
||||||
{
|
{
|
||||||
@ -116,10 +135,10 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (\strpos($update['message']['message'], '/start') === 0) {
|
if (strpos($update['message']['message'], '/start') === 0) {
|
||||||
yield $this->messages->sendMessage(['peer' => $update, 'message' => self::WELCOME, 'reply_to_msg_id' => $update['message']['id'], 'parse_mode' => 'markdown']);
|
$this->messages->sendMessage(['peer' => $update, 'message' => self::WELCOME, 'reply_to_msg_id' => $update['message']['id'], 'parse_mode' => 'markdown']);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$this->logger($e);
|
$this->logger($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,26 +149,26 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
$result = ['query_id' => $update['query_id'], 'results' => [], 'cache_time' => 0];
|
$result = ['query_id' => $update['query_id'], 'results' => [], 'cache_time' => 0];
|
||||||
|
|
||||||
if ($update['query'] === '') {
|
if ($update['query'] === '') {
|
||||||
yield $this->messages->setInlineBotResults($result + self::SWITCH_PM);
|
$this->messages->setInlineBotResults($result + self::SWITCH_PM);
|
||||||
} else {
|
} else {
|
||||||
$result['private'] = true;
|
$result['private'] = true;
|
||||||
yield $this->messages->setInlineBotResults(yield $this->processNonEmptyQuery($update['query'], $update['user_id'], $result));
|
$this->messages->setInlineBotResults($this->processNonEmptyQuery($update['query'], $update['user_id'], $result));
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
try {
|
try {
|
||||||
yield $this->messages->sendMessage(['peer' => self::ADMIN, 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
|
$this->messages->sendMessage(['peer' => self::ADMIN, 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
|
||||||
//yield $this->messages->sendMessage(['peer' => $update['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
|
//$this->messages->sendMessage(['peer' => $update['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
|
||||||
} catch (\danog\MadelineProto\RPCErrorException $e) {
|
} catch (RPCErrorException $e) {
|
||||||
$this->logger($e);
|
$this->logger($e);
|
||||||
} catch (\danog\MadelineProto\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->logger($e);
|
$this->logger($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
yield $this->messages->setInlineBotResults($result + self::SWITCH_PM);
|
$this->messages->setInlineBotResults($result + self::SWITCH_PM);
|
||||||
} catch (\danog\MadelineProto\RPCErrorException $e) {
|
} catch (RPCErrorException $e) {
|
||||||
$this->logger($e);
|
$this->logger($e);
|
||||||
} catch (\danog\MadelineProto\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->logger($e);
|
$this->logger($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,19 +176,19 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
|
|
||||||
private function processNonEmptyQuery($query, $user_id, $toset)
|
private function processNonEmptyQuery($query, $user_id, $toset)
|
||||||
{
|
{
|
||||||
if (\preg_match('|(.*)\$\s*$|', $query, $content)) {
|
if (preg_match('|(.*)\$\s*$|', $query, $content)) {
|
||||||
$exploded = \array_map('trim', \explode('|', $content[1]));
|
$exploded = array_map('trim', explode('|', $content[1]));
|
||||||
$query = \array_shift($exploded);
|
$query = array_shift($exploded);
|
||||||
|
|
||||||
foreach ($exploded as $current => $botSelector) {
|
foreach ($exploded as $current => $botSelector) {
|
||||||
if (\strpos($botSelector, ':') === false) {
|
if (strpos($botSelector, ':') === false) {
|
||||||
$botSelector .= ':';
|
$botSelector .= ':';
|
||||||
}
|
}
|
||||||
list($bot, $selector) = \explode(':', $botSelector);
|
[$bot, $selector] = explode(':', $botSelector);
|
||||||
if ($bot === '' || yield $this->u->getInfo($bot)['bot_api_id'] === yield $this->getSelf()['id']) {
|
if ($bot === '' || $this->u->getInfo($bot)['bot_api_id'] === $this->getSelf()['id']) {
|
||||||
return $toset + self::SWITCH_PM;
|
return $toset + self::SWITCH_PM;
|
||||||
}
|
}
|
||||||
$results = yield $this->u->messages->getInlineBotResults(['bot' => $bot, 'peer' => $user_id, 'query' => $query, 'offset' => '0']);
|
$results = $this->u->messages->getInlineBotResults(['bot' => $bot, 'peer' => $user_id, 'query' => $query, 'offset' => '0']);
|
||||||
$this->logger($results);
|
$this->logger($results);
|
||||||
if (isset($results['switch_pm'])) {
|
if (isset($results['switch_pm'])) {
|
||||||
$toset['switch_pm'] = $results['switch_pm'];
|
$toset['switch_pm'] = $results['switch_pm'];
|
||||||
@ -178,13 +197,13 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
$toset['gallery'] = $results['gallery'];
|
$toset['gallery'] = $results['gallery'];
|
||||||
$toset['results'] = [];
|
$toset['results'] = [];
|
||||||
|
|
||||||
if (\is_numeric($selector)) {
|
if (is_numeric($selector)) {
|
||||||
$toset['results'][0] = $results['results'][$selector - 1];
|
$toset['results'][0] = $results['results'][$selector - 1];
|
||||||
} elseif ($selector === '') {
|
} elseif ($selector === '') {
|
||||||
$toset['results'] = $results['results'];
|
$toset['results'] = $results['results'];
|
||||||
} else {
|
} else {
|
||||||
foreach ($results['results'] as $result) {
|
foreach ($results['results'] as $result) {
|
||||||
if (isset($result['send_message']['message']) && \preg_match('|'.$select.'|', $result['send_message']['message'])) {
|
if (isset($result['send_message']['message']) && preg_match('|'.$select.'|', $result['send_message']['message'])) {
|
||||||
$toset['results'][0] = $result;
|
$toset['results'][0] = $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -192,7 +211,7 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
if (!isset($toset['results'][0])) {
|
if (!isset($toset['results'][0])) {
|
||||||
$toset['results'] = $results['results'];
|
$toset['results'] = $results['results'];
|
||||||
}
|
}
|
||||||
if (\count($exploded) - 1 === $current || !isset($toset['results'][0]['send_message']['message'])) {
|
if (count($exploded) - 1 === $current || !isset($toset['results'][0]['send_message']['message'])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$query = $toset['results'][0]['send_message']['message'];
|
$query = $toset['results'][0]['send_message']['message'];
|
||||||
@ -201,34 +220,10 @@ Created by @daniilgentili using @MadelineProto (https://docs.madelineproto.xyz).
|
|||||||
if (empty($toset['results'])) {
|
if (empty($toset['results'])) {
|
||||||
$toset += self::SWITCH_PM;
|
$toset += self::SWITCH_PM;
|
||||||
} else {
|
} else {
|
||||||
\array_walk($toset['results'], [$this, 'translate']);
|
array_walk($toset['results'], [$this, 'translate']);
|
||||||
}
|
}
|
||||||
return $toset;
|
return $toset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUMadelineProto($uMadelineProto)
|
|
||||||
{
|
|
||||||
// This is some crazy bug I still don't get
|
|
||||||
$this->u = $uMadelineProto[0];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PipesBot::startAndLoopBot('pipesbot.madeline', '<token>');
|
||||||
$uMadelineProto = new \danog\MadelineProto\API('pipesuser.madeline');
|
|
||||||
$uMadelineProto->async(true);
|
|
||||||
|
|
||||||
$uMadelineProto->loop(function () use ($uMadelineProto) {
|
|
||||||
yield $uMadelineProto->echo("User login: ".PHP_EOL);
|
|
||||||
yield $uMadelineProto->start();
|
|
||||||
});
|
|
||||||
|
|
||||||
$MadelineProto = new \danog\MadelineProto\API('pipesbot.madeline');
|
|
||||||
$MadelineProto->async(true);
|
|
||||||
$MadelineProto->loop(function () use ($MadelineProto, $uMadelineProto) {
|
|
||||||
yield $MadelineProto->echo("Bot login: ".PHP_EOL);
|
|
||||||
yield $MadelineProto->start();
|
|
||||||
yield $MadelineProto->setEventHandler(PipesBot::class);
|
|
||||||
$MadelineProto->getEventHandler()->setUMadelineProto($uMadelineProto);
|
|
||||||
});
|
|
||||||
|
|
||||||
$MadelineProto->loop();
|
|
Loading…
Reference in New Issue
Block a user