Skip to content

Instantly share code, notes, and snippets.

@djnotes
Created July 16, 2022 18:58
Show Gist options
  • Save djnotes/a1459ae1bfe823d04b830e1d6b837e8f to your computer and use it in GitHub Desktop.
Save djnotes/a1459ae1bfe823d04b830e1d6b837e8f to your computer and use it in GitHub Desktop.
Small gist to work with chats based on MadelineProto
<?php
include __DIR__ . '/vendor/autoload.php';
/**
* This script does 3 things. Get the last `DIALOGS_LIMIT` dialogs of the current user,
* gets information about a chat,
* get the last `HISTORY_LIMIT` messages within a specific chat
*
* Copyright 2022 (C) Mehdi Haghgoo
*/
define ("DIALOGS_LIMIT", 20);
define ("HISTORY_LIMIT", 30);
$api = new \danog\MadelineProto\API('session/bot.madeline');
$api->start();
readline("Press a Enter to get list of chats: ");
$dialogs = $api->messages->getDialogs(
offset_date: time() - 365 * 24 * 3600, //For the last year
offset_id: 0,
limit: DIALOGS_LIMIT //Get the last 10 chat dialogs
);
echo "list of dialogs: \n";
foreach($dialogs['chats'] as $chat){
echo @$chat['title'] . "\n";
}
promptContinue();
$input = readline("enter a username to get chat information for: ");
$messages = $api->messages->getHistory(
peer : $username,
offset_id : 0,
offset_date : 0,
add_offset : 0,
limit : HISTORY_LIMIT,
max_id : 0,
min_id : 0,
);
echo "Here's the list of the last 30 messages with peer @$username \n";
foreach ($messages['messages'] as $message){
echo $message['message'] . PHP_EOL;
}
function promptContinue(){
$y = readline("Continue (y/N)? ");
if ($y != "y"){
exit(0);
}
}
function getMessage($peer, $messageId){
global $api;
$message = $api->messages->getMessage($peer, $messageId);
return $message['message'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment