1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-11 21:39:37 +01:00
MadelineProto/examples/libtgvoipbot.php

76 lines
2.7 KiB
PHP
Raw Normal View History

2023-08-19 17:38:33 +02:00
<?php declare(strict_types=1);
use danog\MadelineProto\EventHandler\Attributes\Handler;
use danog\MadelineProto\EventHandler\Filter\FilterCommand;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\SimpleFilter\HasAudio;
use danog\MadelineProto\EventHandler\SimpleFilter\HasDocument;
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
use danog\MadelineProto\Ogg;
2023-08-19 17:44:12 +02:00
use danog\MadelineProto\ParseMode;
2023-08-19 17:38:33 +02:00
use danog\MadelineProto\SimpleEventHandler;
use function Amp\async;
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';
} 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';
}
class MyEventHandler extends SimpleEventHandler
{
2023-08-29 10:05:33 +02:00
// !!! Change this to your username! !!!
private const ADMIN = 'danogentili';
2023-08-29 10:07:32 +02:00
public function getReportPeers(): string
2023-08-29 10:05:33 +02:00
{
return self::ADMIN;
}
2023-08-19 17:38:33 +02:00
#[FilterCommand('start')]
public function startCmd(Incoming&Message $message): void
{
2023-08-19 17:44:12 +02:00
$message->reply(
message: "This bot can be used to convert files to be played by a @MadelineProto Telegram webradio!".
"\n\nSee https://docs.madelineproto.xyz/docs/CALLS.html for more info, and call @magicalcrazypony to hear some nice tunes!".
"\n\nSend me an audio file to start.".
"\n\nPowered by @MadelineProto, [source code](https://github.com/danog/MadelineProto/blob/v8/examples/libtgvoipbot.php).",
parseMode: ParseMode::MARKDOWN
);
2023-08-19 17:38:33 +02:00
}
#[Handler]
public function convertCmd((Incoming&Message&HasAudio)|(Incoming&Message&HasDocument) $message): void
{
2023-08-20 16:05:52 +02:00
$reply = $message->reply("Conversion in progress...");
try {
$pipe = self::getStreamPipe();
$sink = $pipe->getSink();
async(
Ogg::convert(...),
$message->media->getStream(),
$sink
)->finally($sink->close(...));
2023-08-19 17:38:33 +02:00
2023-08-20 16:05:52 +02:00
$this->sendDocument(
peer: $message->chatId,
file: $pipe->getSource(),
2023-08-20 17:15:19 +02:00
fileName: $message->media->fileName.".ogg",
replyToMsgId: $message->id
2023-08-20 16:05:52 +02:00
);
} finally {
$reply->delete();
}
2023-08-19 17:38:33 +02:00
}
}
if (!getenv('TOKEN')) {
throw new AssertionError("You must define a TOKEN environment variable with the token of the bot!");
}
2023-08-20 17:28:35 +02:00
MyEventHandler::startAndLoopBot($argv[1] ?? 'libtgvoipbot.madeline', getenv('TOKEN'));