|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Example bot. |
| 5 | + * |
| 6 | + * Copyright 2016-2019 Daniil Gentili |
| 7 | + * (https://daniil.it) |
| 8 | + * This file is part of MadelineProto. |
| 9 | + * MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | + * MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + * See the GNU Affero General Public License for more details. |
| 12 | + * You should have received a copy of the GNU General Public License along with MadelineProto. |
| 13 | + * If not, see <http://www.gnu.org/licenses/>. |
| 14 | + * |
| 15 | + * @author Daniil Gentili <daniil@daniil.it> |
| 16 | + * @copyright 2016-2019 Daniil Gentili <daniil@daniil.it> |
| 17 | + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 |
| 18 | + * |
| 19 | + * @link https://docs.madelineproto.xyz MadelineProto documentation |
| 20 | + */ |
| 21 | + |
| 22 | + /* |
| 23 | + * Various ways to load MadelineProto |
| 24 | + */ |
| 25 | +if (\file_exists(__DIR__.'/vendor/autoload.php')) { |
| 26 | + include 'vendor/autoload.php'; |
| 27 | +} else { |
| 28 | + if (!\file_exists('madeline.php')) { |
| 29 | + \copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php'); |
| 30 | + } |
| 31 | + include 'madeline.php'; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Event handler class. |
| 36 | + */ |
| 37 | +class EventHandler extends \danog\MadelineProto\EventHandler |
| 38 | +{ |
| 39 | + public function onUpdateNewChannelMessage($update) |
| 40 | + { |
| 41 | + yield $this->onUpdateNewMessage($update); |
| 42 | + } |
| 43 | + public function onUpdateNewMessage($update) |
| 44 | + { |
| 45 | + if (isset($update['message']['out']) && $update['message']['out']) { |
| 46 | + return; |
| 47 | + } |
| 48 | + if ($update['_'] === 'updateReadChannelOutbox') { |
| 49 | + return; |
| 50 | + } |
| 51 | + if (isset($update['message']['_']) && $update['message']['_'] === 'messageEmpty') { |
| 52 | + return; |
| 53 | + } |
| 54 | + $res = \json_encode($update, JSON_PRETTY_PRINT); |
| 55 | + |
| 56 | + try { |
| 57 | + yield $this->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']); |
| 58 | + if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') { |
| 59 | + yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]); |
| 60 | + /* '_' => 'inputMediaUploadedDocument', |
| 61 | + 'file' => $update, |
| 62 | + 'attributes' => [ |
| 63 | + ['_' => 'documentAttributeFilename', 'file_name' => 'document.txt'] |
| 64 | + ] |
| 65 | + ],]);*/ |
| 66 | + //yield $this->download_to_dir($update, '/tmp'); |
| 67 | + } |
| 68 | + } catch (\danog\MadelineProto\RPCErrorException $e) { |
| 69 | + $this->logger((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR); |
| 70 | + } catch (\danog\MadelineProto\Exception $e) { |
| 71 | + if (\stripos($e->getMessage(), 'invalid constructor given') === false) { |
| 72 | + $this->logger((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR); |
| 73 | + } |
| 74 | + //$this->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +$settings = [ |
| 79 | + 'logger' => [ |
| 80 | + 'logger_level' => 5 |
| 81 | + ], |
| 82 | + 'serialization' => [ |
| 83 | + 'serialization_interval' => 30, |
| 84 | + 'cleanup_before_serialization' => true |
| 85 | + ], |
| 86 | +]; |
| 87 | + |
| 88 | +$MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); |
| 89 | +$MadelineProto->async(true); |
| 90 | +$MadelineProto->loop(function () use ($MadelineProto) { |
| 91 | + yield $MadelineProto->start(); |
| 92 | + yield $MadelineProto->setEventHandler('\EventHandler'); |
| 93 | +}); |
| 94 | + |
| 95 | +$MadelineProto->loop(); |
0 commit comments