2023-01-20 19:21:13 +01:00
|
|
|
<?php declare(strict_types=1);
|
2019-10-28 22:39:23 +01:00
|
|
|
/**
|
|
|
|
* Example bot.
|
|
|
|
*
|
2023-06-26 18:24:49 +02:00
|
|
|
* PHP 8.1.15+ or 8.2.4+ is required.
|
|
|
|
*
|
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>
|
2023-01-04 12:43:01 +01:00
|
|
|
* @copyright 2016-2023 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
|
|
|
*/
|
|
|
|
|
2023-07-09 17:23:54 +02:00
|
|
|
use danog\MadelineProto\API;
|
2023-05-07 21:06:34 +02:00
|
|
|
use danog\MadelineProto\Broadcast\Progress;
|
|
|
|
use danog\MadelineProto\Broadcast\Status;
|
2023-07-09 18:13:37 +02:00
|
|
|
use danog\MadelineProto\EventHandler\Attributes\Cron;
|
2023-07-10 10:12:46 +02:00
|
|
|
use danog\MadelineProto\EventHandler\Attributes\Handler;
|
|
|
|
use danog\MadelineProto\EventHandler\Filter\FilterCommand;
|
2023-07-14 18:29:20 +02:00
|
|
|
use danog\MadelineProto\EventHandler\Filter\FilterRegex;
|
2023-07-10 10:12:46 +02:00
|
|
|
use danog\MadelineProto\EventHandler\Filter\FilterText;
|
|
|
|
use danog\MadelineProto\EventHandler\Message;
|
2023-07-14 18:29:20 +02:00
|
|
|
use danog\MadelineProto\EventHandler\SimpleFilter\FromAdmin;
|
2023-07-10 10:12:46 +02:00
|
|
|
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
|
2020-02-26 14:14:26 +01:00
|
|
|
use danog\MadelineProto\Logger;
|
2023-07-16 17:24:34 +02:00
|
|
|
use danog\MadelineProto\ParseMode;
|
2020-10-07 16:48:34 +02:00
|
|
|
use danog\MadelineProto\Settings;
|
2023-01-19 15:48:56 +01:00
|
|
|
use danog\MadelineProto\Settings\Database\Mysql;
|
2023-01-19 16:18:22 +01:00
|
|
|
use danog\MadelineProto\Settings\Database\Postgres;
|
|
|
|
use danog\MadelineProto\Settings\Database\Redis;
|
2023-07-11 20:23:34 +02:00
|
|
|
use danog\MadelineProto\SimpleEventHandler;
|
2020-02-23 19:28:42 +01:00
|
|
|
|
2023-07-09 17:23:54 +02:00
|
|
|
// MadelineProto is already loaded
|
|
|
|
if (class_exists(API::class)) {
|
|
|
|
// Otherwise, if a stable version of MadelineProto was installed via composer, load composer autoloader
|
|
|
|
} elseif (file_exists('vendor/autoload.php')) {
|
2023-04-22 20:22:17 +02:00
|
|
|
require_once 'vendor/autoload.php';
|
2019-10-28 22:39:23 +01:00
|
|
|
} else {
|
2023-06-13 20:28:38 +02:00
|
|
|
// Otherwise download an !!! alpha !!! version of MadelineProto via madeline.php
|
2022-12-08 20:16:40 +01:00
|
|
|
if (!file_exists('madeline.php')) {
|
|
|
|
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
|
2019-10-28 22:39:23 +01:00
|
|
|
}
|
2023-04-22 20:22:17 +02:00
|
|
|
require_once 'madeline.php';
|
2019-10-28 22:39:23 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Event handler class.
|
2023-06-25 15:33:14 +02:00
|
|
|
*
|
2023-06-27 19:47:41 +02:00
|
|
|
* All properties returned by __sleep are automatically stored in the database.
|
2019-10-28 22:39:23 +01:00
|
|
|
*/
|
2023-07-11 20:23:34 +02:00
|
|
|
class MyEventHandler extends SimpleEventHandler
|
2019-10-28 22:39:23 +01:00
|
|
|
{
|
2020-02-23 19:28:42 +01:00
|
|
|
/**
|
|
|
|
* @var int|string Username or ID of bot admin
|
|
|
|
*/
|
2023-07-14 20:24:06 +02:00
|
|
|
const ADMIN = "@me"; // !!! Change this to your username !!!
|
2023-07-09 15:18:17 +02:00
|
|
|
|
2021-09-05 19:28:22 +02:00
|
|
|
/**
|
2023-03-26 19:16:50 +02:00
|
|
|
* @var array<int, bool>
|
2023-01-19 17:14:04 +01:00
|
|
|
*/
|
2023-07-09 15:18:17 +02:00
|
|
|
private array $notifiedChats = [];
|
2023-04-22 20:22:17 +02:00
|
|
|
|
2023-07-09 15:18:17 +02:00
|
|
|
/**
|
|
|
|
* Returns a list of names for properties that will be automatically saved to the session database (MySQL/postgres/redis if configured, the session file otherwise).
|
|
|
|
*/
|
2023-06-25 19:52:00 +02:00
|
|
|
public function __sleep(): array
|
|
|
|
{
|
|
|
|
return ['notifiedChats'];
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:28:42 +01:00
|
|
|
/**
|
|
|
|
* Get peer(s) where to report errors.
|
|
|
|
*
|
|
|
|
* @return int|string|array
|
|
|
|
*/
|
|
|
|
public function getReportPeers()
|
|
|
|
{
|
|
|
|
return [self::ADMIN];
|
|
|
|
}
|
2022-05-01 19:59:07 +02:00
|
|
|
/**
|
|
|
|
* Initialization logic.
|
|
|
|
*/
|
2023-01-16 13:17:59 +01:00
|
|
|
public function onStart(): void
|
2022-05-01 19:59:07 +02:00
|
|
|
{
|
|
|
|
$this->logger("The bot was started!");
|
2023-01-16 13:17:59 +01:00
|
|
|
$this->logger($this->getFullInfo('MadelineProto'));
|
2023-04-22 20:32:39 +02:00
|
|
|
|
2023-07-13 16:40:56 +02:00
|
|
|
$this->sendMessageToAdmins("The bot was started!");
|
2022-05-01 19:59:07 +02:00
|
|
|
}
|
2023-05-07 21:06:34 +02:00
|
|
|
|
2023-07-09 18:07:23 +02:00
|
|
|
/**
|
2023-07-09 18:13:37 +02:00
|
|
|
* This cron function will be executed forever, every 60 seconds.
|
2023-07-09 18:07:23 +02:00
|
|
|
*/
|
2023-07-09 18:13:37 +02:00
|
|
|
#[Cron(period: 60.0)]
|
2023-07-09 18:10:58 +02:00
|
|
|
public function cron1(): void
|
2023-07-09 18:07:23 +02:00
|
|
|
{
|
2023-07-13 16:40:56 +02:00
|
|
|
$this->sendMessageToAdmins("The bot is online, current time ".date(DATE_RFC850)."!");
|
2023-07-09 18:07:23 +02:00
|
|
|
}
|
|
|
|
|
2023-05-07 21:06:34 +02:00
|
|
|
private int $lastLog = 0;
|
|
|
|
/**
|
|
|
|
* Handles updates to an in-progress broadcast.
|
|
|
|
*/
|
|
|
|
public function onUpdateBroadcastProgress(Progress $progress): void
|
|
|
|
{
|
|
|
|
if (time() - $this->lastLog > 5 || $progress->status === Status::FINISHED) {
|
|
|
|
$this->lastLog = time();
|
2023-07-13 16:40:56 +02:00
|
|
|
$this->sendMessageToAdmins((string) $progress);
|
2023-05-07 21:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 19:38:24 +01:00
|
|
|
/**
|
2023-07-10 10:12:46 +02:00
|
|
|
* Handle incoming updates from users, chats and channels.
|
2020-02-23 19:28:42 +01:00
|
|
|
*/
|
2023-07-10 10:12:46 +02:00
|
|
|
#[Handler]
|
|
|
|
public function handleMessage(Incoming&Message $message): void
|
2019-10-28 22:39:23 +01:00
|
|
|
{
|
2023-01-27 14:20:47 +01:00
|
|
|
// In this example code, send the "This userbot is powered by MadelineProto!" message only once per chat.
|
|
|
|
// Ignore all further messages coming from this chat.
|
2023-07-10 10:12:46 +02:00
|
|
|
if (!isset($this->notifiedChats[$message->chatId])) {
|
|
|
|
$this->notifiedChats[$message->chatId] = true;
|
2023-01-22 13:00:48 +01:00
|
|
|
|
2023-07-11 20:23:34 +02:00
|
|
|
$message->reply(
|
2023-01-19 17:14:04 +01:00
|
|
|
message: "This userbot is powered by [MadelineProto](https://t.me/MadelineProto)!",
|
2023-07-16 17:24:34 +02:00
|
|
|
parseMode: ParseMode::MARKDOWN
|
2023-01-19 17:14:04 +01:00
|
|
|
);
|
2023-04-22 20:22:17 +02:00
|
|
|
}
|
2023-07-10 10:12:46 +02:00
|
|
|
}
|
2023-01-19 17:14:04 +01:00
|
|
|
|
2023-07-10 10:12:46 +02:00
|
|
|
#[FilterCommand('restart')]
|
2023-07-14 18:29:20 +02:00
|
|
|
public function restartCommand(Incoming & Message & FromAdmin $message): void
|
2023-07-10 10:12:46 +02:00
|
|
|
{
|
2023-04-22 20:22:17 +02:00
|
|
|
// If the message is a /restart command from an admin, restart to reload changes to the event handler code.
|
2023-07-14 18:29:20 +02:00
|
|
|
|
|
|
|
// Make sure to run in a bash while loop when running via CLI to allow self-restarts.
|
|
|
|
$this->restart();
|
2023-07-10 10:12:46 +02:00
|
|
|
}
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2023-07-10 10:12:46 +02:00
|
|
|
#[FilterCommand('broadcast')]
|
2023-07-14 18:29:20 +02:00
|
|
|
public function broadcastCommand(Incoming & Message & FromAdmin $message): void
|
2023-07-10 10:12:46 +02:00
|
|
|
{
|
2023-07-19 12:14:41 +02:00
|
|
|
// We can broadcast messages to all users with /broadcast
|
2023-07-14 18:29:20 +02:00
|
|
|
if (!$message->replyToMsgId) {
|
|
|
|
$message->reply("You should reply to the message you want to broadcast.");
|
2023-05-07 21:06:34 +02:00
|
|
|
return;
|
2021-09-05 19:28:22 +02:00
|
|
|
}
|
2023-07-14 18:29:20 +02:00
|
|
|
$this->broadcastForwardMessages(
|
|
|
|
from_peer: $message->senderId,
|
|
|
|
message_ids: [$message->replyToMsgId],
|
|
|
|
drop_author: true,
|
|
|
|
pin: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[FilterCommand('echo')]
|
|
|
|
public function echoCmd(Incoming & Message $message): void
|
|
|
|
{
|
|
|
|
// Contains the arguments of the command
|
|
|
|
$args = $message->commandArgs;
|
|
|
|
|
|
|
|
$message->reply($args[0] ?? '');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[FilterRegex('/.*(mt?proto).*/i')]
|
|
|
|
public function testRegex(Incoming & Message $message): void
|
|
|
|
{
|
|
|
|
$message->reply("Did you mean to write MadelineProto instead of ".$message->matches[1].'?');
|
2023-07-10 10:12:46 +02:00
|
|
|
}
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2023-07-13 16:19:36 +02:00
|
|
|
#[FilterText('hi')]
|
2023-07-10 10:12:46 +02:00
|
|
|
public function pingCommand(Incoming&Message $message): void
|
|
|
|
{
|
2023-07-13 16:19:36 +02:00
|
|
|
$message->reply('hello');
|
2019-10-28 22:39:23 +01:00
|
|
|
}
|
2023-07-13 16:02:53 +02:00
|
|
|
|
2023-07-16 14:55:00 +02:00
|
|
|
/**
|
|
|
|
* Gets a download link for any file up to 4GB!
|
|
|
|
*/
|
|
|
|
#[FilterCommand('dl')]
|
2023-07-16 15:34:26 +02:00
|
|
|
public function downloadLink(Incoming&Message $message): void
|
2023-07-16 14:55:00 +02:00
|
|
|
{
|
2023-07-16 15:34:26 +02:00
|
|
|
if (!$message->replyToMsgId) {
|
|
|
|
$message->reply("This command must reply to a media message!");
|
|
|
|
return;
|
|
|
|
}
|
2023-07-16 14:55:00 +02:00
|
|
|
$message = $message->getReply();
|
|
|
|
if (!$message instanceof Message || !$message->media) {
|
|
|
|
$message->reply("This command must reply to a media message!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$message->reply("Download link: ".$message->media->getDownloadLink());
|
|
|
|
}
|
|
|
|
|
2023-07-13 16:02:53 +02:00
|
|
|
public static function getPluginPaths(): string|array|null
|
|
|
|
{
|
|
|
|
return 'plugins/';
|
|
|
|
}
|
2019-10-28 22:39:23 +01:00
|
|
|
}
|
2020-10-07 16:48:34 +02:00
|
|
|
|
|
|
|
$settings = new Settings;
|
|
|
|
$settings->getLogger()->setLevel(Logger::LEVEL_ULTRA_VERBOSE);
|
|
|
|
|
2020-10-23 18:07:18 +02:00
|
|
|
// You can also use Redis, MySQL or PostgreSQL
|
2020-11-26 21:50:06 +01:00
|
|
|
// $settings->setDb((new Redis)->setDatabase(0)->setPassword('pony'));
|
2020-10-23 18:07:18 +02:00
|
|
|
// $settings->setDb((new Postgres)->setDatabase('MadelineProto')->setUsername('daniil')->setPassword('pony'));
|
|
|
|
// $settings->setDb((new Mysql)->setDatabase('MadelineProto')->setUsername('daniil')->setPassword('pony'));
|
2019-10-28 22:39:23 +01:00
|
|
|
|
2023-04-22 20:22:17 +02:00
|
|
|
// For users or bots
|
2023-07-09 13:54:41 +02:00
|
|
|
MyEventHandler::startAndLoop('bot.madeline', $settings);
|
2023-04-22 20:22:17 +02:00
|
|
|
|
|
|
|
// For bots only
|
2023-07-09 13:54:41 +02:00
|
|
|
// MyEventHandler::startAndLoopBot('bot.madeline', 'bot token', $settings);
|