Skip to content

Commit

Permalink
Add source code of @libtgvoipbot
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Aug 19, 2023
1 parent 1d989ee commit cd29d7e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/contacts.importContactToken.html" name="contacts.importContactToken">Obtain user info from a temporary profile link: contacts.importContactToken</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/stats.getMessagePublicForwards.html" name="stats.getMessagePublicForwards">Obtains a list of messages, indicating to which other public channels was a channel message forwarded. : stats.getMessagePublicForwards</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.getSendAs.html" name="channels.getSendAs">Obtains a list of peers that can be used to send messages in a specific group: channels.getSendAs</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getstreampipe-amp-bytestream-pipe" name="getStreamPipe">Obtains a pipe that can be used to upload a file from a stream: getStreamPipe</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.checkHistoryImport.html" name="messages.checkHistoryImport">Obtains information about a chat export file, generated by a foreign chat app, click here for more info about imported chats »: messages.checkHistoryImport</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.setBotPrecheckoutResults.html" name="messages.setBotPrecheckoutResults">Once the user has confirmed their payment and shipping details, the bot receives an updateBotPrecheckoutQuery update. : messages.setBotPrecheckoutResults</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getupdates-array-offset-int-limit-int-timeout-float-params-list-array-update_id-mixed-update-mixed" name="getUpdates">Only useful when consuming MadelineProto updates through an API in another language (like Javascript), **absolutely not recommended when directly writing MadelineProto bots**: getUpdates</a>
Expand Down
56 changes: 56 additions & 0 deletions examples/libtgvoipbot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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;
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
{
#[FilterCommand('start')]
public function startCmd(Incoming&Message $message): void
{
$message->reply("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.");
}
#[Handler]
public function convertCmd((Incoming&Message&HasAudio)|(Incoming&Message&HasDocument) $message): void
{
$pipe = self::getStreamPipe();
$sink = $pipe->getSink();
async(
Ogg::convert(...),
$message->media->getStream(),
$sink
)->finally($sink->close(...));

$this->sendDocument(
$message->chatId,
file: $pipe->getSource(),
fileName: $message->media->fileName.".ogg"
);
}
}

if (!getenv('TOKEN')) {
throw new AssertionError("You must define a TOKEN environment variable with the token of the bot!");
}

MyEventHandler::startAndLoopBot('libtgvoipbot.madeline', getenv('TOKEN'));
28 changes: 28 additions & 0 deletions src/StreamEof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/**
* NothingInTheSocketException module.
*
* 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 <[email protected]>
* @copyright 2016-2023 Daniil Gentili <[email protected]>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/

namespace danog\MadelineProto;

use Exception;

/** @internal */
final class StreamEof extends Exception
{
}

0 comments on commit cd29d7e

Please sign in to comment.