Skip to content

Commit 70400cd

Browse files
authored
chore: replace telegram with teleproto (#22915)
gram-js/gramjs#837
1 parent e116df0 commit 70400cd

7 files changed

Lines changed: 56 additions & 469 deletions

File tree

lib/routes/telegram/channel-media.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { Context } from 'hono';
22
import { stream } from 'hono/streaming';
3-
import type { TelegramClient } from 'telegram';
4-
import { Api } from 'telegram';
5-
import type { IterDownloadFunction } from 'telegram/client/downloads.js';
6-
import { returnBigInt as bigInt } from 'telegram/Helpers.js';
7-
import { getAppropriatedPartSize } from 'telegram/Utils.js';
3+
import type { TelegramClient } from 'teleproto';
4+
import { Api } from 'teleproto';
5+
import { returnBigInt as bigInt } from 'teleproto/Helpers.js';
86

97
import { config } from '@/config';
108
import InvalidParameterError from '@/errors/types/invalid-parameter';
@@ -86,30 +84,32 @@ export async function* streamThumbnail(client: TelegramClient, doc: Api.Document
8684
}
8785

8886
export async function* streamDocument(client: TelegramClient, obj: Api.Document, thumbSize = '', offset?: bigInt.BigInteger, limit?: bigInt.BigInteger) {
89-
const chunkSize = (obj.size ? getAppropriatedPartSize(obj.size) : 64) * 1024;
90-
const iterFileParams: IterDownloadFunction = {
91-
file: new Api.InputDocumentFileLocation({
87+
const requestSize = 512 * 1024; // MAX_CHUNK_SIZE
88+
let skip = offset ? offset.mod(requestSize).toJSNumber() : 0;
89+
const alignedOffset = offset?.subtract(skip);
90+
// console.log('starting iterDownload');
91+
const chunks = client.iterDownload(
92+
new Api.InputDocumentFileLocation({
9293
id: obj.id,
9394
accessHash: obj.accessHash,
9495
fileReference: obj.fileReference,
9596
thumbSize,
9697
}),
97-
chunkSize,
98-
requestSize: 512 * 1024, // MAX_CHUNK_SIZE
99-
dcId: obj.dcId,
100-
offset: undefined,
101-
limit: undefined,
102-
};
103-
if (offset) {
104-
iterFileParams.offset = offset;
105-
}
106-
if (limit) {
107-
iterFileParams.limit = limit.valueOf();
98+
{
99+
requestSize,
100+
dcId: obj.dcId,
101+
offset: alignedOffset,
102+
limit: limit && alignedOffset ? limit.subtract(alignedOffset).add(1).valueOf() : undefined,
103+
}
104+
);
105+
for await (const chunk of chunks) {
106+
if (skip >= chunk.length) {
107+
skip -= chunk.length;
108+
continue;
109+
}
110+
yield skip ? chunk.subarray(skip) : chunk;
111+
skip = 0;
108112
}
109-
// console.log('starting iterDownload');
110-
const stream = client.iterDownload(iterFileParams);
111-
yield* stream;
112-
await stream.close();
113113
}
114114

115115
function parseRange(range: string, length: bigInt.BigInteger) {

lib/routes/telegram/scripts/get-telegram-session.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import readline from 'node:readline';
22

3-
import { TelegramClient } from 'telegram';
4-
import { StringSession } from 'telegram/sessions/index.js';
3+
import { TelegramClient } from 'teleproto';
4+
import { StringSession } from 'teleproto/sessions/index.js';
55
import winston from 'winston';
66

77
function userInput(question) {

lib/routes/telegram/stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-await-in-loop */
22
import type { Context } from 'hono';
3-
import { Api } from 'telegram';
3+
import { Api } from 'teleproto';
44

55
import NotFoundError from '@/errors/types/not-found';
66
import { configureMiddlewares, handleMedia } from '@/routes/telegram/channel-media';

lib/routes/telegram/tglib/channel.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable no-await-in-loop */
22
import type { Context } from 'hono';
3-
import { Api } from 'telegram';
4-
import { HTMLParser } from 'telegram/extensions/html.js';
5-
import { returnBigInt } from 'telegram/Helpers.js';
6-
import { getDisplayName } from 'telegram/Utils.js';
3+
import { Api } from 'teleproto';
4+
import { HTMLParser } from 'teleproto/extensions/html.js';
5+
import { returnBigInt } from 'teleproto/Helpers.js';
6+
import { getDisplayName } from 'teleproto/Utils.js';
77

88
import type { DataItem } from '@/types';
99
import cache from '@/utils/cache';
@@ -22,10 +22,11 @@ export async function getPollResults(client, message, m: Api.MessageMediaPoll) {
2222
}
2323
const txt = `<h4>${m.poll.quiz ? 'Quiz' : 'Poll'}: ${m.poll.question.text}</h4>
2424
<div><ul>${m.poll.answers
25+
.filter((a) => a instanceof Api.PollAnswer)
2526
.map((a) => {
2627
let answerTxt = a.text.text;
2728
const result = results.results?.find((r) => r.option.buffer === a.option.buffer);
28-
if (result && results.totalVoters) {
29+
if (result?.voters !== undefined && results.totalVoters) {
2930
answerTxt = `<strong>${Math.round((result.voters / results.totalVoters) * 100)}%</strong>: ${answerTxt}`;
3031
}
3132
return `<li>${answerTxt}</li>`;

lib/routes/telegram/tglib/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Api, TelegramClient } from 'telegram';
2-
import type { UserAuthParams } from 'telegram/client/auth';
3-
import { StringSession } from 'telegram/sessions/index.js';
1+
import { Api, TelegramClient } from 'teleproto';
2+
import type { UserAuthParams } from 'teleproto/client/auth';
3+
import { StringSession } from 'teleproto/sessions/index.js';
44

55
import { config } from '@/config';
66
import ConfigNotFoundError from '@/errors/types/config-not-found';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"simplecc-wasm": "1.1.1",
127127
"socks-proxy-agent": "10.1.0",
128128
"source-map": "0.8.0",
129-
"telegram": "2.26.22",
129+
"teleproto": "1.228.5",
130130
"title": "4.0.1",
131131
"tldts": "7.4.10",
132132
"tosource": "2.0.0-alpha.3",

0 commit comments

Comments
 (0)