Unable to Use Telegram(gram.js) Library in Cloudflare Workers

I am encountering an issue while trying to use the Gram.js library within a Cloudflare Workers project. Despite following the necessary steps to install and import the library, I am unable to utilize its features within my Cloudflare Worker code.

this is the first time i am using cloudflare worker for my project so kindly help me :slightly_smiling_face:
Steps I followed:

  1. Set up a new Cloudflare Workers project.
  2. Installed the telegram library using npm install telegram.
  3. Imported the necessary modules from Gram.js into my Cloudflare Worker script using require.

However, when attempting to execute Gram.js functions or interact with the Telegram API, I encounter runtime errors or undefined behavior. It seems that the Gram.js library is not functioning as expected within the Cloudflare Workers environment.

I have verified that the Gram.js library works correctly in other environments outside of Cloudflare Workers, which suggests that the issue is specific to the Cloudflare Workers runtime.

I would greatly appreciate any assistance in resolving this issue or any insights into potential limitations or conflicts between Gram.js and Cloudflare Workers. If there are any additional configuration or compatibility considerations specific to using Gram.js within Cloudflare Workers, please provide guidance on how to overcome them.

Thank you for your help

code

const { StringSession } = require('telegram/sessions');
const { TelegramClient } = require('telegram');

const api_id = 0;
const api_hash = '';
const stringSession = new StringSession(
	''
); // fill this later with the value from session.save()

const client = new TelegramClient(stringSession, api_id, api_hash, {
	downloadRetries: 5,
	connectionRetries: 5,
});

async function startTelegramClient() {
	await client.start({
		phoneNumber: '+',
		phoneCode: '12345',
		onError: console.log,
	});
}

async function getMessages(channelId, messageId) {
	const result = await client.getMessages(channelId, {
		ids: messageId,
	});
	return result;
}

async function downloadMedia(document) {
	const fileBuffer = await client.downloadMedia(document, {
		workers: 1,
		chunkSize: 1024 * 1024,
		onDownloadProgress: (progress) => {
			console.log(`Downloaded: ${progress}%`);
		},
	});
	return fileBuffer;
}
export default {
	async fetch(request, env, ctx) {
		if (request.method === 'POST') {
			const { searchParams } = new URL(request.url);
			const channelId = searchParams.get('channel_id');
			const messageId = searchParams.get('message_id');
			console.log('👉 channelId', channelId);
			console.log('👉 messageId', messageId);
			await startTelegramClient();

			// const result = await getMessages(channelId, messageId);
			// console.log('👉 result', result);
			// const doc = getDocument(result);
			const doc = undefined;
			console.log('👉 doc', doc);

			if (doc) {
				const fileBuffer = await downloadMedia(doc.document);

				const headers = {
					'Content-Disposition': `attachment; filename="${doc.fileName}.${doc.fileType}"`,
					'Content-Type': `application/${doc.fileType}`,
				};

				return new Response(fileBuffer, { headers });
			} else {
				return new Response('No media found in the message.', { status: 404 });
			}
		} else {
			return new Response('Invalid request method.', { status: 400 });
		}
	},
};

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.