MadelineProto/examples/bot.php

110 lines
3.6 KiB
PHP
Raw Normal View History

2019-10-28 22:39:23 +01:00
#!/usr/bin/env php
<?php
/**
* Example bot.
*
2020-02-17 14:13:46 +01:00
* Copyright 2016-2020 Daniil Gentili
2019-10-28 22:39:23 +01:00
* (https://daniil.it)
* This file is part of MadelineProto.
* 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.
* 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.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
2020-02-17 14:13:46 +01:00
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
2019-10-28 22:39:23 +01:00
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
2019-10-31 15:07:35 +01:00
* @link https://docs.madelineproto.xyz MadelineProto documentation
2019-10-28 22:39:23 +01:00
*/
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception;
2020-02-26 14:14:26 +01:00
use danog\MadelineProto\Logger;
use danog\MadelineProto\RPCErrorException;
/*
2019-10-28 22:39:23 +01:00
* Various ways to load MadelineProto
*/
2019-11-01 12:45:15 +01:00
if (\file_exists('vendor/autoload.php')) {
2019-10-28 22:39:23 +01:00
include 'vendor/autoload.php';
} else {
if (!\file_exists('madeline.php')) {
\copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';
}
/**
* Event handler class.
*/
class MyEventHandler extends EventHandler
2019-10-28 22:39:23 +01:00
{
/**
* @var int|string Username or ID of bot admin
*/
const ADMIN = "danogentili"; // Change this
/**
* Get peer(s) where to report errors.
*
* @return int|string|array
*/
public function getReportPeers()
{
return [self::ADMIN];
}
/**
* Handle updates from supergroups and channels.
*
* @param array $update Update
*
* @return void
*/
public function onUpdateNewChannelMessage(array $update): \Generator
2019-10-28 22:39:23 +01:00
{
return $this->onUpdateNewMessage($update);
2019-10-28 22:39:23 +01:00
}
/**
* Handle updates from users.
*
* @param array $update Update
*
* @return \Generator
*/
public function onUpdateNewMessage(array $update): \Generator
2019-10-28 22:39:23 +01:00
{
2020-02-13 20:28:12 +01:00
if ($update['message']['_'] === 'messageEmpty' || $update['message']['out'] ?? false) {
2019-10-28 22:39:23 +01:00
return;
}
$res = \json_encode($update, JSON_PRETTY_PRINT);
try {
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']);
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {
yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]);
}
} catch (RPCErrorException $e) {
$this->report("Surfaced: $e");
} catch (Exception $e) {
2019-10-28 22:39:23 +01:00
if (\stripos($e->getMessage(), 'invalid constructor given') === false) {
$this->report("Surfaced: $e");
2019-10-28 22:39:23 +01:00
}
}
}
}
$settings = [
'logger' => [
2020-02-26 14:14:26 +01:00
'logger_level' => Logger::VERBOSE
2019-10-28 22:39:23 +01:00
],
'serialization' => [
'serialization_interval' => 30,
],
];
$MadelineProto = new API('bot.madeline', $settings);
2019-10-28 22:39:23 +01:00
// Reduce boilerplate with new wrapper method.
// Also initializes error reporting, catching and reporting all errors surfacing from the event loop.
$MadelineProto->startAndLoop(MyEventHandler::class);