1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 07:18:57 +01:00
MadelineProto/examples/bot.php

318 lines
11 KiB
PHP
Raw Normal View History

2023-01-20 19:21:13 +01:00
<?php declare(strict_types=1);
2019-10-28 22:39:23 +01:00
/**
* Example bot.
*
2024-01-08 19:26:40 +01:00
* PHP 8.2.4+ is required.
2023-06-26 18:24:49 +02:00
*
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
*/
use danog\MadelineProto\API;
2023-05-07 21:06:34 +02:00
use danog\MadelineProto\Broadcast\Progress;
use danog\MadelineProto\Broadcast\Status;
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;
2023-07-22 20:52:57 +02:00
use danog\MadelineProto\EventHandler\Filter\FilterTextCaseInsensitive;
2023-07-10 10:12:46 +02:00
use danog\MadelineProto\EventHandler\Message;
2023-12-12 17:57:48 +01:00
use danog\MadelineProto\EventHandler\Message\ChannelMessage;
2023-07-21 17:46:36 +02:00
use danog\MadelineProto\EventHandler\Message\Service\DialogPhotoChanged;
2023-10-26 19:31:22 +02:00
use danog\MadelineProto\EventHandler\Plugin\RestartPlugin;
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;
use danog\MadelineProto\EventHandler\SimpleFilter\IsReply;
2020-02-26 14:14:26 +01:00
use danog\MadelineProto\Logger;
2023-07-16 17:24:34 +02:00
use danog\MadelineProto\ParseMode;
2023-08-14 20:54:16 +02:00
use danog\MadelineProto\RemoteUrl;
use danog\MadelineProto\Settings;
2023-01-19 15:48:56 +01:00
use danog\MadelineProto\Settings\Database\Mysql;
use danog\MadelineProto\Settings\Database\Postgres;
use danog\MadelineProto\Settings\Database\Redis;
2023-07-11 20:23:34 +02:00
use danog\MadelineProto\SimpleEventHandler;
2023-08-12 16:34:46 +02:00
use danog\MadelineProto\VoIP;
// 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')) {
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
}
require_once 'madeline.php';
2019-10-28 22:39:23 +01:00
}
/**
* Event handler class.
2023-06-25 15:33:14 +02:00
*
2023-08-12 16:34:46 +02:00
* NOTE: ALL of the following methods are OPTIONAL.
* You can even provide an empty event handler if you want.
*
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
{
/**
* @var int|string Username or ID of bot admin
*/
2023-11-11 16:55:29 +01:00
public 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-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'];
}
/**
* 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-10-26 19:31:22 +02:00
/**
* Returns a set of plugins to activate.
*/
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
];
}
/**
* This cron function will be executed forever, every 60 seconds.
*/
#[Cron(period: 60.0)]
2023-07-09 18:10:58 +02:00
public function cron1(): void
{
2023-07-13 16:40:56 +02:00
$this->sendMessageToAdmins("The bot is online, current time ".date(DATE_RFC850)."!");
}
2020-11-25 19:38:24 +01:00
/**
2023-07-10 10:12:46 +02:00
* Handle incoming updates from users, chats and channels.
*/
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-07-10 10:12:46 +02:00
}
2023-01-19 17:14:04 +01:00
2023-07-21 19:21:34 +02:00
/**
* Reposts a media file as a Telegram story.
*/
#[FilterCommand('story')]
public function storyCommand(Message & FromAdmin $message): void
{
2023-07-21 20:02:01 +02:00
if ($this->isSelfBot()) {
2023-07-21 19:21:34 +02:00
$message->reply("Only users can post Telegram Stories!");
return;
}
$media = $message->getReply(Message::class)?->media;
if (!$media) {
$message->reply("You should reply to a photo or video to repost it as a story!");
return;
}
$this->stories->sendStory(
2023-10-10 19:01:38 +02:00
peer: 'me',
2023-07-21 19:21:34 +02:00
media: $media,
caption: "This story was posted using [MadelineProto](https://t.me/MadelineProto)!",
parse_mode: ParseMode::MARKDOWN,
privacy_rules: [['_' => 'inputPrivacyValueAllowAll']]
);
}
/**
2023-12-12 17:57:48 +01:00
* Automatically sends a comment to all new incoming channel posts.
2023-07-21 19:21:34 +02:00
*/
2023-12-12 17:57:48 +01:00
#[Handler]
public function makeComment(Incoming&ChannelMessage $message): void
2023-07-21 19:21:34 +02:00
{
2023-12-12 17:57:48 +01:00
if ($this->isSelfBot()) {
2023-07-21 19:21:34 +02:00
return;
}
2023-12-12 17:57:48 +01:00
$message->getDiscussion()->reply(
message: "This comment is powered by [MadelineProto](https://t.me/MadelineProto)!",
parseMode: ParseMode::MARKDOWN
);
2023-07-21 19:21:34 +02:00
}
2023-07-10 10:12:46 +02:00
#[FilterCommand('broadcast')]
2023-07-21 19:21:34 +02:00
public function broadcastCommand(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,
);
}
private int $lastLog = 0;
/**
* Handles updates to an in-progress broadcast.
*/
#[Handler]
public function handleBroadcastProgress(Progress $progress): void
{
if (time() - $this->lastLog > 5 || $progress->status === Status::FINISHED) {
$this->lastLog = time();
$this->sendMessageToAdmins((string) $progress);
}
}
2023-07-14 18:29:20 +02:00
#[FilterCommand('echo')]
2023-07-21 19:21:34 +02:00
public function echoCmd(Message $message): void
2023-07-14 18:29:20 +02:00
{
// Contains the arguments of the command
$args = $message->commandArgs;
$message->reply($args[0] ?? '');
}
2023-08-12 16:34:46 +02:00
#[FilterRegex('/.*(mt?proto)[^.]?.*/i')]
2023-07-14 18:29:20 +02:00
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-22 15:31:22 +02:00
#[FilterText('test')]
public function pingCommand(Message $message): void
{
$message->reply('test reply');
}
#[FilterCommand('react')]
public function reactCommand(Message&IsReply $message): void
{
$message->getReply(Message::class)->addReaction('👌');
}
#[FilterCommand('unreact')]
public function unreactCommand(Message&IsReply $message): void
{
$message->getReply(Message::class)->delReaction('👌');
}
2023-07-22 20:52:57 +02:00
#[FilterTextCaseInsensitive('hi')]
2023-07-22 15:31:22 +02:00
public function pingCommandCaseInsensitive(Message $message): void
2023-07-10 10:12:46 +02:00
{
2023-07-13 16:19:36 +02:00
$message->reply('hello');
2019-10-28 22:39:23 +01:00
}
2023-07-21 17:46:36 +02:00
/**
* Called when the dialog photo of a chat or channel changes.
*/
#[Handler]
public function logPhotoChanged(Incoming&DialogPhotoChanged $message): void
{
if ($message->photo) {
2023-07-21 17:49:55 +02:00
$message->reply("Nice! Here's a download link for the photo: ".$message->photo->getDownloadLink());
2023-07-21 17:46:36 +02:00
}
2023-07-21 17:49:55 +02:00
// The group photo was deleted
2023-07-21 17:46:36 +02:00
}
2023-07-16 14:55:00 +02:00
/**
* Gets a download link for any file up to 4GB!
*
* The bot must be started via web for this command to work.
*
* You can also start it via CLI but you'll have to specify a download script URL in the settings: https://docs.madelineproto.xyz/docs/FILES.html#getting-a-download-link-cli-bots.
2023-07-16 14:55:00 +02:00
*/
#[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-23 16:47:19 +02:00
$reply = $message->getReply(Message::class);
if (!$reply?->media) {
2023-07-16 14:55:00 +02:00
$message->reply("This command must reply to a media message!");
return;
}
2023-07-23 16:47:19 +02:00
$reply->reply("Download link: ".$reply->media->getDownloadLink());
2023-07-16 14:55:00 +02:00
}
2023-08-12 16:34:46 +02:00
#[FilterCommand('call')]
public function callVoip(Incoming&Message $message): void
{
2023-08-14 20:54:16 +02:00
$this->requestCall($message->senderId)->play(new RemoteUrl('http://icestreaming.rai.it/1.mp3'));
2023-08-12 16:34:46 +02:00
}
#[Handler]
2023-08-12 17:29:00 +02:00
public function handleIncomingCall(VoIP&Incoming $call): void
2023-08-12 16:34:46 +02:00
{
2023-08-16 13:52:10 +02:00
$call->accept()->play(new RemoteUrl('http://icestreaming.rai.it/1.mp3'));
2023-08-12 16:34:46 +02:00
}
public static function getPluginPaths(): string|array|null
{
return 'plugins/';
}
2019-10-28 22:39:23 +01:00
}
$settings = new Settings;
$settings->getLogger()->setLevel(Logger::LEVEL_ULTRA_VERBOSE);
// You can also use Redis, MySQL or PostgreSQL.
// Data is migrated automatically.
//
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
// For users or bots
MyEventHandler::startAndLoop('bot.madeline', $settings);
// For bots only
// MyEventHandler::startAndLoopBot('bot.madeline', 'bot token', $settings);