Created
January 30, 2023 05:40
-
-
Save djnotes/b22606924837b6d92b6d4e5840bf16ac to your computer and use it in GitHub Desktop.
Get dialogs and display in the form Message - PeerType - PeerID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// include __DIR__ . '/vendor/autoload.php'; | |
if (!file_exists('madeline.php')) { | |
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php'); | |
} | |
require_once 'madeline.php'; | |
use \danog\MadelineProto\API; | |
use \danog\MadelineProto\RPCErrorException; | |
const USER_SESSION = "myuser.madeline"; | |
const BOT_SESSION = "mybot.madeline"; | |
const MAX_DIALOGS = 10; | |
$user = new API(__DIR__ . "/session/" . USER_SESSION); | |
$bot = new API(__DIR__ . "/session/" . BOT_SESSION); | |
print("Welcome to the bot\n"); | |
$user->loop(function() use ($user, $bot) { | |
yield $user->echo( "Starting user session " . USER_SESSION . PHP_EOL); | |
$user->start(); | |
$admin = yield $user->getSelf(); | |
yield $user->echo("Starting bot session " . BOT_SESSION . PHP_EOL); | |
$bot->start(); | |
yield $bot->messages->sendMessage(peer: $admin, message: "Started getting dialogs"); | |
$count = 0; | |
foreach (yield $user->getFullDialogs() as $dialog){ | |
if ($dialog['_'] != 'dialog') continue; //Might also be dialogFolder | |
if($count > MAX_DIALOGS) break; //Break out of loop after retrieving MAX_DIALOGS dialogs | |
print_r($dialog); | |
try{ | |
$info = yield $user->getInfo($dialog['peer']); | |
$peerType = $dialog['peer']['_']; | |
$topMessageId = $dialog['top_message']; | |
switch ($dialog['peer']['_']){ | |
case 'peerUser': | |
$peerId = $dialog['peer']['user_id']; | |
break; | |
case 'peerChannel': | |
$peerId = $dialog['peer']['channel_id']; | |
break; | |
case 'peerChat': | |
$peerId = $dialog['peer']['chat_id']; | |
default: | |
$peerId = ""; | |
} | |
$message = ""; | |
try{ | |
$messages = yield $user->messages->getMessages(id: [$topMessageId]); | |
print_r($messages); | |
$message = $messages['messages'][0]['message']; | |
} catch(\danog\MadelineProto\Exception $e){ | |
echo $e->getMessage(); | |
} | |
yield $bot->messages->sendMessage(peer: $admin, message: "$message - $peerType - $peerId"); | |
} catch(RPCErrorException $e){ | |
yield $user->echo("{$e->getFile()}: {$e->getLine()}: {$e->getMessage()}"); | |
} catch(\danog\MadelineProto\Exception $e){ | |
yield $user->echo($e->getMessage()); | |
} | |
$count++; | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment