2019-10-28 22:39:23 +01:00
|
|
|
#!/usr/bin/env php
|
2023-01-20 19:21:13 +01:00
|
|
|
<?php declare(strict_types=1);
|
2019-10-28 22:39:23 +01:00
|
|
|
/**
|
|
|
|
* 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>
|
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
|
|
|
*/
|
|
|
|
|
2021-09-05 19:28:22 +02:00
|
|
|
use danog\MadelineProto\Db\DbArray;
|
2020-02-23 19:28:42 +01:00
|
|
|
use danog\MadelineProto\EventHandler;
|
2020-02-26 14:14:26 +01:00
|
|
|
use danog\MadelineProto\Logger;
|
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;
|
2020-02-23 19:28:42 +01:00
|
|
|
|
|
|
|
/*
|
2019-10-28 22:39:23 +01:00
|
|
|
* Various ways to load MadelineProto
|
|
|
|
*/
|
2022-12-08 20:16:40 +01:00
|
|
|
if (file_exists('vendor/autoload.php')) {
|
2019-10-28 22:39:23 +01:00
|
|
|
include 'vendor/autoload.php';
|
|
|
|
} else {
|
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
|
|
|
}
|
2020-04-05 15:33:01 +02:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingFile
|
|
|
|
*/
|
2019-10-28 22:39:23 +01:00
|
|
|
include 'madeline.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler class.
|
|
|
|
*/
|
2020-02-23 19:28:42 +01:00
|
|
|
class MyEventHandler extends EventHandler
|
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
|
|
|
|
*/
|
|
|
|
const ADMIN = "danogentili"; // Change this
|
2021-09-05 19:28:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of properties automatically stored in database (MySQL, Postgres, redis or memory).
|
2022-12-30 21:54:44 +01:00
|
|
|
*
|
2021-09-05 19:28:22 +02:00
|
|
|
* @see https://docs.madelineproto.xyz/docs/DATABASE.html
|
|
|
|
*/
|
|
|
|
protected static array $dbProperties = [
|
2022-12-30 21:54:44 +01:00
|
|
|
'dataStoredOnDb' => 'array',
|
2023-01-19 17:14:04 +01:00
|
|
|
'notifiedChats' => 'array',
|
2021-09-05 19:28:22 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2023-01-19 17:14:04 +01:00
|
|
|
* @var DbArray<array-key, array>
|
2021-09-05 19:28:22 +02:00
|
|
|
*/
|
2023-01-19 16:18:22 +01:00
|
|
|
protected DbArray $dataStoredOnDb;
|
2023-01-19 17:14:04 +01:00
|
|
|
/**
|
|
|
|
* @var DbArray<int, bool>
|
|
|
|
*/
|
|
|
|
protected DbArray $notifiedChats;
|
2021-09-05 19:28:22 +02:00
|
|
|
|
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'));
|
2022-05-01 19:59:07 +02:00
|
|
|
}
|
2020-11-25 19:38:24 +01:00
|
|
|
/**
|
|
|
|
* Handle updates from supergroups and channels.
|
|
|
|
*
|
|
|
|
* @param array $update Update
|
|
|
|
*/
|
2023-01-16 13:17:59 +01:00
|
|
|
public function onUpdateNewChannelMessage(array $update): void
|
2020-11-25 19:38:24 +01:00
|
|
|
{
|
2023-01-16 13:17:59 +01:00
|
|
|
$this->onUpdateNewMessage($update);
|
2019-10-28 22:39:23 +01:00
|
|
|
}
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2020-02-23 19:28:42 +01:00
|
|
|
/**
|
|
|
|
* Handle updates from users.
|
|
|
|
*
|
|
|
|
* @param array $update Update
|
|
|
|
*/
|
2023-01-16 13:17:59 +01:00
|
|
|
public function onUpdateNewMessage(array $update): void
|
2019-10-28 22:39:23 +01:00
|
|
|
{
|
2020-11-25 19:38:24 +01:00
|
|
|
if ($update['message']['_'] === 'messageEmpty' || $update['message']['out'] ?? false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:48:56 +01:00
|
|
|
$this->logger($update);
|
2023-01-19 17:14:04 +01:00
|
|
|
|
|
|
|
// Chat id
|
|
|
|
$id = $this->getId($update);
|
2022-08-09 19:08:24 +02: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-01-19 17:14:04 +01:00
|
|
|
if (!isset($this->notifiedChats[$id])) {
|
2023-01-22 13:00:48 +01:00
|
|
|
$this->notifiedChats[$id] = true;
|
|
|
|
|
2023-01-19 17:14:04 +01:00
|
|
|
$this->messages->sendMessage(
|
|
|
|
peer: $update,
|
|
|
|
message: "This userbot is powered by [MadelineProto](https://t.me/MadelineProto)!",
|
|
|
|
reply_to_msg_id: $update['message']['id'] ?? null,
|
|
|
|
parse_mode: 'Markdown'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($update['message']['media'])
|
|
|
|
&& !in_array($update['message']['media']['_'], [
|
|
|
|
'messageMediaGame',
|
|
|
|
'messageMediaWebPage',
|
|
|
|
'messageMediaPoll'
|
|
|
|
])
|
|
|
|
) {
|
|
|
|
$this->messages->sendMedia(
|
|
|
|
peer: $update,
|
|
|
|
message: $update['message']['message'],
|
|
|
|
media: $update
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2023-01-27 14:20:47 +01:00
|
|
|
// Test MadelineProto's built-in database driver, which automatically maps to MySQL/PostgreSQL/Redis
|
|
|
|
// properties mentioned in the MyEventHandler::$dbProperties property!
|
|
|
|
|
2023-01-19 17:14:04 +01:00
|
|
|
// Can be anything serializable: an array, an int, an object, ...
|
2021-09-05 19:28:22 +02:00
|
|
|
$myData = [];
|
|
|
|
|
2023-01-27 14:20:47 +01:00
|
|
|
if (isset($this->dataStoredOnDb['k1'])) {
|
|
|
|
$myData = $this->dataStoredOnDb['k1'];
|
2021-09-05 19:28:22 +02:00
|
|
|
}
|
2023-01-27 14:20:47 +01:00
|
|
|
$this->dataStoredOnDb['k1'] = $myData + ['moreStuff' => 'yay'];
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2023-01-27 14:20:47 +01:00
|
|
|
$this->dataStoredOnDb['k2'] = 0;
|
|
|
|
unset($this->dataStoredOnDb['k2']);
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2023-01-16 13:17:59 +01:00
|
|
|
$this->logger("Count: ".count($this->dataStoredOnDb));
|
2021-09-05 19:28:22 +02:00
|
|
|
|
2023-01-19 15:48:56 +01:00
|
|
|
// You can even iterate over the data
|
2023-01-08 20:46:16 +01:00
|
|
|
foreach ($this->dataStoredOnDb as $key => $value) {
|
2021-09-05 19:28:22 +02:00
|
|
|
$this->logger($key);
|
|
|
|
$this->logger($value);
|
|
|
|
}
|
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
|
|
|
|
2020-02-23 19:28:42 +01:00
|
|
|
// Reduce boilerplate with new wrapper method.
|
|
|
|
// Also initializes error reporting, catching and reporting all errors surfacing from the event loop.
|
2023-01-16 13:17:59 +01:00
|
|
|
MyEventHandler::startAndLoop('uwu.madeline', $settings);
|