I need to call rev.getOrderById every 30 seconds until it returns 200 status code. This…
- User
I need to call rev.getOrderById every 30 seconds until it returns 200 status code. This is my code: await rev.createOrder().then((order_location) => { const order_id = order_location.split('/').pop() try { rev.getOrderById(order_id).then((data) => { console.log(data) var flag = true while(flag){ setTimeout(() => { rev.getCompiledFile(data[0].id) .then(() => {flag = false}) .catch((err) => console.log(err.message)) }, 30); }
})} catch (err) {console.error(err)} }) But it got out of memory error. How to do it properly
- Assistant
The out of memory error you're encountering is likely due to the way you're using
setTimeoutwithin awhileloop. Your code currently tries to create a new asynchronous task every 30 milliseconds as long as the flag is true, which is causing an uncontrolled accumulation of pending asynchronous tasks. This approach can quickly consume all available memory and lead to the error you're experiencing. - User
I have async function: const autoOrder = async (_audio_link) => { await rev.createOrder(_audio_link).then(async (order_location) => { const order_number = order_location.split('/').pop(); var intervalId;
const checkOrderStatus = async () =>{ rev.getOrderByNumber(order_number) .then( (orderDetails) => { if(orderDetails.length > 1){ rev.getCompiledFile(orderDetails[1].id) .then(() => { clearInterval(intervalId); console.log('Order is ready and file is compiled!'); }) .catch((err) => { console.log(err.message); }); } else{ console.log("Order not complited yet"); } }) .catch((err) => { console.log(err.message); }); } intervalId = setInterval(checkOrderStatus, 30000); });}
When I'm calling it like this: autoOrder(link) .then(async () => {await ctx.replyWithDocument({source: 'content.pdf'})}) ctx.replyWithDocument({source: 'content.pdf'})} works right after the await rev.createOrder completes. But I need ctx.replyWithDocument after all of the function autoOrder completes. So, when rev.getCompiledFile(orderDetails[1].id) .then(() => { clearInterval(intervalId); console.log('Order is ready and file is compiled!'); }) Is done, only then it should ctx.replyWithDocument. How to do it properly?
- Assistant
- User
I'm using Telegraf js library and have this code: bot.on(message('audio'), async (ctx) => { try { // await ctx.reply(JSON.stringify(ctx.message.voice, null, 2)) await ctx.reply(JSON.stringify(ctx.message, null, 2)) const link = await ctx.telegram.getFileLink(ctx.message.audio.file_id) console.log('message link: ', ctx.message.audio.file_id); await ctx.reply("Аудио запись принята, дождитесь окончания транскрипции") console.log('TG Link: ', link); // autoOrder(link) // .then(async () => {await ctx.replyWithDocument({source: 'content.pdf'})}) } catch (e) { console.log(
Error while audio message: ${e.message}) } })So, when I'm sending to bot a file 44mb size, it returns error: Error while audio message: 400: Bad Request: file is too big How to avoid this error, but still send 44mb file and get its link?
- Assistant
- User
is there a way I can local download 40mb file thats been sent to my telegram bot?
- Assistant
- User
But I got error 400: Bad Request: file is too big
- Assistant
If you're encountering the error "400: Bad Request: file is too big" when attempting to download a file from Telegram using a bot, it's likely due to Telegram's limits on file sizes that can be directly accessed by the bot API. As of the latest guidelines, Telegram restricts bot file downloads to a certain size, which may be lower than the maximum file size a user can send through the Telegram app itself.
- User
Telegram has API and some methods for it, like these: upload.file#96a18d5 type:storage.FileType mtime:int bytes:bytes = upload.File; upload.fileCdnRedirect#f18cda44 dc_id:int file_token:bytes encryption_key:bytes encryption_iv:bytes file_hashes:Vector = upload.File;
storage.fileUnknown#aa963b05 = storage.FileType; storage.fileJpeg#7efe0e = storage.FileType; storage.fileGif#cae1aadf = storage.FileType; storage.filePng#a4f63c0 = storage.FileType; storage.fileMp3#528a0677 = storage.FileType; storage.fileMov#4b09ebbc = storage.FileType; storage.filePartial#40bc6f52 = storage.FileType; storage.fileMp4#b3cea0e4 = storage.FileType; storage.fileWebp#1081464c = storage.FileType;
---functions---
upload.getFile#be5335be flags:# precise:flags.0?true cdn_supported:flags.1?true location:InputFileLocation offset:long limit:int = upload.File; How can I call it via node js?
- Assistant
- User
air-trofim:telegram trol$ node mtpProto.js file:///Users/trol/Programs/conversation_bot/src/telegram/mtpProto.js:14 path: path.resolve(__dirname, './data/1.json'), ^
ReferenceError: __dirname is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and '/Users/trol/Programs/conversation_bot/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file:///Users/trol/Programs/conversation_bot/src/telegram/mtpProto.js:14:24 at ModuleJob.run (node:internal/modules/esm/module_job:218:25) at async ModuleLoader.import (node:internal/modules/esm/loader:329:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:113:12)
import {dirname} from 'path' import MTProto from '@mtproto/core' import path from 'path'
storageOptions: { path: path.resolve(__dirname, './data/1.json'), },
- Assistant
The error you're encountering arises from using the
__dirnameglobal variable, which is not available in ES modules (when you specify"type": "module"in yourpackage.json). In ES module syntax, you need an alternative approach to get the current module's directory.Here's how you can resolve this issue using the
pathandurlmodules:Using
import.meta.urlYou can use
import.meta.urlalong with theurlmodule to compute the directory path:import { dirname } from 'path'; import { fileURLToPath } from 'url'; // These imports are for your MTProto setup import MTProto from '@mtproto/core'; import path from 'path'; // Convert `import.meta.url` to a file path const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Now use __dirname to resolve paths const storageOptions = { path: path.resolve(__dirname, './data/1.json'), }; console.log('Storage path:', storageOptions.path); // Rest of your MTProto setup code - User
import {dirname} from 'path' import { fileURLToPath } from 'url'; import MTProto from '@mtproto/core' import path from 'path';
const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename);
const api_id = process.env.TELEGRAM_API_ID; console.log(api_id);
the problem is that console.logs undefined
- Assistant
If
console.log(api_id);is outputtingundefined, it's likely due to theTELEGRAM_API_IDenvironment variable not being set correctly in your environment.Here's how you can resolve this issue:
Steps for Setting Environment Variables