1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 00:14:38 +01:00
MadelineProto/examples/secret_bot.php

328 lines
12 KiB
PHP
Raw Normal View History

2023-09-28 19:33:23 +02:00
<?php declare(strict_types=1);
2019-10-28 22:39:23 +01:00
/**
* Secret chat 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
*/
2023-09-28 19:33:23 +02:00
use danog\MadelineProto\EventHandler\Attributes\Handler;
use danog\MadelineProto\EventHandler\Message\SecretMessage;
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
2023-01-20 15:30:13 +01:00
use danog\MadelineProto\Logger;
2021-12-09 13:25:14 +01:00
use danog\MadelineProto\Settings;
2023-09-28 19:33:23 +02:00
use danog\MadelineProto\SimpleEventHandler;
2020-02-25 19:10:49 +01:00
2023-01-20 13:49:44 +01:00
use function Amp\async;
2023-09-28 19:33:23 +02:00
use function Amp\File\read;
2023-01-20 13:49:44 +01:00
use function Amp\Future\await;
2018-03-23 16:39:58 +01:00
/*
* Various ways to load MadelineProto
*/
2022-12-08 20:16:40 +01:00
if (file_exists(__DIR__.'/../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');
}
include 'madeline.php';
2017-06-01 22:21:15 +02:00
}
2023-09-28 19:33:23 +02:00
class SecretHandler extends SimpleEventHandler
{
2023-01-20 13:49:44 +01:00
private array $sent = [];
/**
* @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 users.
*
* @param array $update Update
*/
2023-01-20 13:49:44 +01:00
public function onUpdateNewMessage(array $update): void
{
2023-01-24 14:28:49 +01:00
if (($update['message']['message'] ?? '') === 'request') {
2023-01-20 13:49:44 +01:00
$this->requestSecretChat($update);
}
2023-01-24 14:28:49 +01:00
if (($update['message']['message'] ?? '') === 'ping') {
2023-01-20 13:49:44 +01:00
$this->messages->sendMessage(['message' => 'pong', 'peer' => $update]);
}
}
/**
* Handle secret chat messages.
*/
2023-09-28 19:33:23 +02:00
#[Handler]
public function handle(Incoming&SecretMessage $message): void
2018-03-23 16:39:58 +01:00
{
if(isset($message->media))
{
$path = $message->media->downloadToDir('/tmp');
$this->sendPhoto(
peer: $message->senderId,
file: $message->media
);
}
2023-09-28 19:59:44 +02:00
/*if (isset($this->sent[$update->chatId])) {
2020-10-18 15:41:06 +02:00
return;
}
$secret_media = [];
// Photo uploaded as document, secret chat
2023-01-26 19:30:56 +01:00
$secret_media['document_photo'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/faust.jpg',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/faust.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => mime_content_type('tests/faust.jpg'),
'caption' => 'This file was uploaded using MadelineProto',
'file_name' => 'faust.jpg',
'size' => filesize('tests/faust.jpg'),
'attributes' => [
[
'_' => 'documentAttributeImageSize',
'w' => 1280,
'h' => 914
]
]
]
]
];
2020-10-18 15:41:06 +02:00
// Photo, secret chat
2023-01-26 19:30:56 +01:00
$secret_media['photo'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/faust.jpg',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaPhoto',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/faust.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'caption' => 'This file was uploaded using MadelineProto',
'size' => filesize('tests/faust.jpg'),
'w' => 1280,
'h' => 914
]
]
];
2020-10-18 15:41:06 +02:00
// GIF, secret chat
2023-01-26 19:30:56 +01:00
$secret_media['gif'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/pony.mp4',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/pony.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => mime_content_type('tests/pony.mp4'),
'caption' => 'test',
'file_name' => 'pony.mp4',
'size' => filesize('tests/faust.jpg'),
'attributes' => [
['_' => 'documentAttributeAnimated']
]
]
]
];
2020-10-18 15:41:06 +02:00
// Sticker, secret chat
2023-01-26 19:30:56 +01:00
$secret_media['sticker'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/lel.webp',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/lel.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => mime_content_type('tests/lel.webp'),
'caption' => 'test',
'file_name' => 'lel.webp',
'size' => filesize('tests/lel.webp'),
'attributes' => [
[
'_' => 'documentAttributeSticker',
'alt' => 'LEL',
'stickerset' => ['_' => 'inputStickerSetEmpty']
]
]
]
]
];
2020-10-18 15:41:06 +02:00
// Document, secrey chat
2023-01-26 19:30:56 +01:00
$secret_media['document'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/60',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/faust.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => 'magic/magic',
'caption' => 'test',
'file_name' => 'magic.magic',
'size' => filesize('tests/60'),
'attributes' => [
[
'_' => 'documentAttributeFilename',
'file_name' => 'fairy'
]
]
]
]
];
2020-10-18 15:41:06 +02:00
// Video, secret chat
2023-01-26 19:30:56 +01:00
$secret_media['video'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/swing.mp4',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/swing.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => mime_content_type('tests/swing.mp4'),
'caption' => 'test',
'file_name' => 'swing.mp4',
'size' => filesize('tests/swing.mp4'),
'attributes' => [
[
'_' => 'documentAttributeVideo',
'duration' => 5,
'w' => 1280,
'h' => 720
]
]
]
]
];
2020-10-18 15:41:06 +02:00
// audio, secret chat
2023-01-26 19:30:56 +01:00
$secret_media['audio'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/mosconi.mp3',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/faust.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => mime_content_type('tests/mosconi.mp3'),
'caption' => 'test',
'file_name' => 'mosconi.mp3',
'size' => filesize('tests/mosconi.mp3'),
'attributes' => [
[
'_' => 'documentAttributeAudio',
'voice' => false,
'duration' => 1,
'title' => 'AH NON LO SO IO',
'performer' => 'IL DIO GERMANO MOSCONI'
]
]
]
]
];
2020-10-18 15:41:06 +02:00
2023-01-26 19:30:56 +01:00
$secret_media['voice'] = [
2023-09-28 19:33:23 +02:00
'peer' => $update->chatId,
2023-01-26 19:30:56 +01:00
'file' => 'tests/mosconi.mp3',
'message' => [
'_' => 'decryptedMessage',
'ttl' => 0,
'message' => '',
'media' => [
'_' => 'decryptedMessageMediaDocument',
2023-09-28 19:33:23 +02:00
'thumb' => read('tests/faust.preview.jpg'),
2023-01-26 19:30:56 +01:00
'thumb_w' => 90,
'thumb_h' => 90,
'mime_type' => mime_content_type('tests/mosconi.mp3'),
'caption' => 'test',
'file_name' => 'mosconi.mp3',
'size' => filesize('tests/mosconi.mp3'),
'attributes' => [
[
'_' => 'documentAttributeAudio',
'voice' => true,
'duration' => 1,
'title' => 'AH NON LO SO IO',
'performer' => 'IL DIO GERMANO MOSCONI'
]
]
]
]
];
2020-10-18 15:41:06 +02:00
2023-01-20 13:49:44 +01:00
$promises = [];
2020-10-18 15:41:06 +02:00
foreach ($secret_media as $type => $smessage) {
2023-01-20 13:49:44 +01:00
$promises []= async($this->messages->sendEncryptedFile(...), $smessage);
2020-10-18 15:41:06 +02:00
}
2023-01-20 13:49:44 +01:00
await($promises);
2020-10-18 15:41:06 +02:00
$i = 0;
while ($i < 10) {
2023-09-28 19:33:23 +02:00
$this->logger("SENDING MESSAGE $i TO ".$update->chatId);
2020-10-18 15:41:06 +02:00
// You can also use the sendEncrypted parameter for more options in secret chats
2023-09-28 19:33:23 +02:00
$this->sendMessage(peer: $update->chatId, message: (string) ($i++));
2020-10-18 15:41:06 +02:00
}
2023-09-28 19:59:44 +02:00
$this->sent[$update->chatId] = true;*/
}
2017-06-01 22:21:15 +02:00
}
2021-12-09 13:25:14 +01:00
$settings = new Settings;
2023-01-20 15:30:13 +01:00
$settings->getLogger()->setLevel(Logger::ULTRA_VERBOSE);
2018-03-30 18:07:54 +02:00
2021-12-09 13:25:14 +01:00
SecretHandler::startAndLoop('secret.madeline', $settings);