Repository navigation
Expand file tree
/
Copy pathtgstories_dl_bot.php
More file actions
244 lines (222 loc) · 10 KB
/
Copy pathtgstories_dl_bot.php
File metadata and controls
244 lines (222 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php declare(strict_types=1);
/**
* Telegram stories downloader bot.
*
* Copyright 2016-2020 Daniil Gentili
* (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>
* @copyright 2016-2025 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
use danog\MadelineProto\API;
use danog\MadelineProto\Broadcast\Progress;
use danog\MadelineProto\Broadcast\Status;
use danog\MadelineProto\EventHandler\Filter\FilterCommand;
use danog\MadelineProto\EventHandler\Filter\FilterRegex;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Query\ChatButtonQuery;
use danog\MadelineProto\EventHandler\SimpleFilter\FromAdmin;
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
use danog\MadelineProto\ParseMode;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\SimpleEventHandler;
// MadelineProto is already loaded
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';
}
// Login as a user
$u = new API('stories_user.madeline');
if (!$u->getSelf()) {
if (!$_GET) {
$u->echo("Please login as a user!");
}
$u->start();
}
if (!$u->isSelfUser()) {
throw new AssertionError("You must login as a user! Please delete the user.madeline folder to continue.");
}
unset($u);
final class StoriesEventHandler extends SimpleEventHandler
{
private const HELP = "Telegram stories downloader bot, powered by @MadelineProto!\n\nUsage:\n/dlStories @danogentili - Download all the stories of a @username!\n\n[Source code](https://github.com/danog/MadelineProto/blob/v8/examples/tgstories_dl_bot.php) powered by @MadelineProto";
// Username of the admin of the bot
private const ADMIN = "@danogentili";
private const PAGE_SIZE = 10;
private API $userInstance;
public function onStart(): void
{
// Login as a user
$this->userInstance = new API('stories_user.madeline');
}
public function getReportPeers()
{
return self::ADMIN;
}
private int $lastLog = 0;
/**
* Handles updates to an in-progress broadcast.
*/
public function onUpdateBroadcastProgress(Progress $progress): void
{
if (time() - $this->lastLog > 5 || $progress->status === Status::FINISHED) {
$this->lastLog = time();
$this->sendMessageToAdmins((string) $progress);
}
}
#[FilterCommand('broadcast')]
public function broadcastCommand(Message & FromAdmin $message): void
{
// We can broadcast messages to all users with /broadcast
if (!$message->replyToMsgId) {
$message->reply("You should reply to the message you want to broadcast.");
return;
}
$this->broadcastForwardMessages(
from_peer: $message->senderId,
message_ids: [$message->replyToMsgId],
drop_author: true,
pin: true,
);
}
#[FilterCommand('start')]
public function startCmd(Incoming&Message $message): void
{
$message->reply(self::HELP, parseMode: ParseMode::MARKDOWN);
}
/**
* Lists the telegram stories of a user, a page at a time, with download links (except for protected ones).
*
* The bot must be started via web for this command to work.
*
* You can also start it via CLI but you'll have to specify a download script URL in the settings: https://docs.madelineproto.xyz/docs/FILES.html#getting-a-download-link-cli-bots.
*/
#[FilterCommand('dlStories')]
public function dlStoriesCommand(Message $message): void
{
if (!$message->commandArgs) {
$message->reply("You must specify the @username or the Telegram ID of a user to download their stories!");
return;
}
$peer = $this->userInstance->getId($message->commandArgs[0]);
// The active stories that aren't posted to the profile, which stories.getPinnedStories doesn't return
$active = $this->userInstance->stories->getPeerStories(peer: $peer)['stories']['stories'];
// Active stories may be returned as storyItemSkipped, fetch them in full
$skipped = array_column(array_filter($active, static fn (array $s): bool => $s['_'] === 'storyItemSkipped'), 'id');
if ($skipped) {
$active = array_merge(
array_filter($active, static fn (array $s): bool => $s['_'] !== 'storyItemSkipped'),
$this->userInstance->stories->getStoriesByID(peer: $peer, id: $skipped)['stories'],
);
}
$active = array_filter($active, static fn (array $s): bool => $s['_'] === 'storyItem' && !$s['pinned']);
usort($active, static fn (array $a, array $b): int => $b['id'] <=> $a['id']);
[$text, $markup] = $this->renderStoriesPage($peer, 0, $active);
$message->reply($text, parseMode: ParseMode::MARKDOWN, replyMarkup: $markup, noWebpage: true);
}
/**
* Loads the next page of the stories of a user, when its button is pressed.
*/
#[FilterRegex('/^stories:(-?\d+):(\d+)$/')]
public function storiesPageQuery(ChatButtonQuery $query): void
{
// Remove the button first, so that the page is loaded once even if it's pressed again
try {
// Without reply_markup: Telegram rejects an empty inline keyboard (REPLY_MARKUP_INVALID)
$this->messages->editMessage(peer: $query->chatId, id: $query->messageId);
} catch (RPCErrorException $e) {
if ($e->rpc !== 'MESSAGE_NOT_MODIFIED') {
throw $e;
}
// Already pressed
$query->answer(cacheTime: 0);
return;
}
$query->answer(cacheTime: 0);
$peer = (int) $query->matches[1];
$offset = (int) $query->matches[2];
try {
[$text, $markup] = $this->renderStoriesPage($peer, $offset, []);
} catch (Throwable $e) {
// Give the button back, to retry
$query->editReplyMarkup(self::nextPageMarkup($peer, $offset));
throw $e;
}
$this->sendMessage(peer: $query->chatId, message: $text, parseMode: ParseMode::MARKDOWN, replyMarkup: $markup, noWebpage: true);
}
/**
* Renders a page of the profile stories of a peer (newest first, the ones pinned to the top on the first page),
* fetching only that page, after some other stories to list first.
*
* Protected stories are listed without a link.
*
* @param int $offset ID of the last story of the previous page, 0 for the first page
* @param list<array> $before Stories to list before the page
*
* @return array{string, ?array}
*/
private function renderStoriesPage(int $peer, int $offset, array $before): array
{
$res = $this->userInstance->stories->getPinnedStories(peer: $peer, offset_id: $offset, limit: self::PAGE_SIZE);
$pinnedToTop = $res['pinned_to_top'] ?? [];
$stories = $before;
$next = 0;
foreach ($res['stories'] as $story) {
if (in_array($story['id'], $pinnedToTop, true)) {
// Listed first on the first page whatever their ID, they can't be used as offset
if ($offset !== 0) {
continue;
}
} else {
$next = $next === 0 ? $story['id'] : min($next, $story['id']);
}
// Skip deleted stories
if ($story['_'] === 'storyItem') {
$stories[] = $story;
}
}
$result = '';
foreach ($stories as $story) {
$cur = "- ID {$story['id']}, posted ".date(DATE_RFC850, $story['date']);
if (isset($story['caption']) && $story['caption'] !== '') {
$caption = mb_strlen($story['caption']) > 100 ? mb_substr($story['caption'], 0, 100).'…' : $story['caption'];
$cur .= ', "'.self::markdownEscape($caption).'"';
}
$result .= $story['noforwards']
? "$cur; protected, can't be downloaded\n"
: "$cur; [click here to download »]({$this->userInstance->getDownloadLink($story)})\n";
}
if ($offset === 0) {
$result = $result === ''
? "This user has no stories!"
: "Stories ({$res['count']} in the profile):\n\n$result";
} elseif ($result === '') {
$result = "No more stories!";
}
// A full page: there may be more
$more = $next !== 0 && count($res['stories']) >= self::PAGE_SIZE;
return [$result, $more ? self::nextPageMarkup($peer, $next) : null];
}
private static function nextPageMarkup(int $peer, int $offset): array
{
// Bot API style: MadelineProto converts it to the constructors of the current layer
return ['inline_keyboard' => [[['text' => 'Next page »', 'callback_data' => "stories:$peer:$offset"]]]];
}
}
$token = '<token>';
StoriesEventHandler::startAndLoopBot('stories.madeline', $token);