1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 00:34:40 +01:00
MadelineProto/examples/simpleBot.php

70 lines
2.3 KiB
PHP
Raw Normal View History

2023-08-30 18:14:52 +02:00
<?php declare(strict_types=1);
// Simple example bot.
// PHP 8.1.15+ or 8.2.4+ is required.
// Run via CLI (recommended: `screen php bot.php`) or via web.
// To reduce RAM usage, follow these instructions: https://docs.madelineproto.xyz/docs/DATABASE.html
use danog\MadelineProto\EventHandler\Attributes\Handler;
use danog\MadelineProto\EventHandler\Message;
2023-10-26 19:31:22 +02:00
use danog\MadelineProto\EventHandler\Plugin\RestartPlugin;
2023-08-30 18:14:52 +02:00
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
use danog\MadelineProto\SimpleEventHandler;
2023-08-31 17:18:56 +02:00
// Load via composer (RECOMMENDED, see https://docs.madelineproto.xyz/docs/INSTALLATION.html#composer-from-scratch)
2023-08-30 18:14:52 +02:00
if (file_exists('vendor/autoload.php')) {
require_once 'vendor/autoload.php';
} else {
// Otherwise download an !!! alpha !!! version of MadelineProto via madeline.php
if (!file_exists('madeline.php')) {
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
require_once 'madeline.php';
}
2023-12-03 13:26:18 +01:00
class BasicEventHandler extends SimpleEventHandler
2023-08-30 18:14:52 +02:00
{
// !!! Change this to your username !!!
2023-11-11 16:55:29 +01:00
public const ADMIN = "@me";
2023-08-30 18:14:52 +02:00
/**
* Get peer(s) where to report errors.
*/
public function getReportPeers()
{
return [self::ADMIN];
}
2023-10-26 19:31:22 +02:00
/**
* Returns a set of plugins to activate.
2023-10-26 19:39:23 +02:00
*
* See here for more info on plugins: https://docs.madelineproto.xyz/docs/PLUGINS.html
2023-10-26 19:31:22 +02:00
*/
public static function getPlugins(): array
{
return [
// Offers a /restart command to admins that can be used to restart the bot, applying changes.
2023-10-28 17:57:14 +02:00
// Make sure to run in a bash while loop when running via CLI to allow self-restarts.
2023-11-11 16:55:29 +01:00
RestartPlugin::class,
2023-10-26 19:31:22 +02:00
];
}
2023-08-30 18:14:52 +02:00
/**
* Handle incoming updates from users, chats and channels.
*/
#[Handler]
public function handleMessage(Incoming&Message $message): void
{
// Code that uses $message...
// See the following pages for more examples and documentation:
// - https://github.com/danog/MadelineProto/blob/v8/examples/bot.php
// - https://docs.madelineproto.xyz/docs/UPDATES.html
// - https://docs.madelineproto.xyz/docs/FILTERS.html
// - https://docs.madelineproto.xyz/
}
}
2023-12-03 13:26:18 +01:00
BasicEventHandler::startAndLoop('bot.madeline');