I want to send a message with gramJS to a telegram channel having a file_id or file_unique_id instead.
const mediaId = 'AgACAgIAAx0CT3U93QAAAAAAAAAAAAAA-AAAAAAAAAAAAAA-AAAAAAAAAA'
userbot.sendFile(-100123123123, {
file: mediaId,
message: "Hello there!",
randomId: BigInt("-4156887774564")
})
and gramjs always tries to upload the file from my local system. In documentation it says "file can be How can I send that file_id/file_unique id. I am logged in as a normal user, and I want not to download and reupload the media file if possible
I also tried different InputMedia with filerReference = mediaId, this also does not work
Related
Here's the problem: I am trying to send multiple files from google storage to my express server and from there, the user of my website.
Google Storage >> Express Server >> Website User
To send the files to the end user, I will have to zip them. But since I can't write any of the files to the hard drive, AppEngine does not allow it. I will have to store the files in memory until they all arrive from Google storage, and then zip it and stream the zipped file to the user.
The files are large so this is not possible. Is there a solution where I can pipe the streams for files into something that zips them, and then pipe that stream to the response? Or some other solution?
const downloadVideoFromCloud = (videoId) => {
const location = `videos/${videoId}.mp4`;
const file = myBucket.file(location);
file.createReadStream().pipe(fs.createWriteStream(`./videos/${videoId}.mp4`));
};
const downloadPlaylistFromCloud = async (playlistId) => {
const playlist = await getPlaylist(playlistId);
for (const video of playlist) {
downloadVideoFromCloud(video.videoId);
}
};
This is the code I am using to save the videos to local storage while testing.
I'm trying to download files from MongoDB to the user's device (the user's local file system).
I'm using Gridfs-stream to create a readStream to read files from MongoDB, and pipe it into Node.js fs.createWriteStream(downloadDirectory) (downloadDirectory is the directory the user wanted it to be).
Now, I'm coding on localhost, so it downloads to wherever I point it to.
My question is: after I deployed my backend and frontend, is my approach going to download the file to the downloadDirectory on my server or the downloadDirectory on the user's device?
const readstream = gfs.createReadStream({
_id: file._id
})
const fileStream = fs.createWriteStream(downloadDirectory + file.name)
const writeStream = readstream.pipe(fileStream)
writeStream.on('finish', (returnedFile) => {
res.json(returnedFile)
})
Solution:
I spent over 12 hours debugging how to download attachment files from the Node.js server on the client-side.
This post saved my life.
Node.js - createWriteStream is writing a different file than writeFile
For those of you who are still struggling on can't open a downloaded attachment or downloaded file doubled the size and corrupted. Check the Encoding!
I make a simple audio creating web app using Node.js server. I would like to create audio using Cloud text to speech API and then upload that audio to Cloud storage.
(I use Windows 10, Windows Subsystems for Linux, Debian 10.3 and Google Chrome browser. )
This is the code in Node.js server.
const client = new textToSpeech.TextToSpeechClient();
async function quickStart() {
// The text to synthesize
const text = 'hello, world!';
// Construct the request
const request = {
input: {text: text},
// Select the language and SSML voice gender (optional)
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
// select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
console.log(response);
I would like to upload response to Cloud Storage.
Can I upload response to Cloud Storage directly? Or Do I have to save response in Node.js server and upload it to Cloud Storage?
I searched the Internet, but couldn't find the way to upload response to Cloud Storage directly. So, if you have a hint, please tell me. Thank you in advance.
You should be able to do that, with all your code in the same file. The best way for you to achieve that, it's by using a Cloud Function, that will be the one sending the file to your Cloud Storage. But, yes, you will need to save your file using Node.js, so then, you will upload it to Clou Storage.
To achieve that, you will need to save your file locally and then, upload it to Cloud Storage. As you can check in a complete tutorial in this other post here, you need to construct the file, save it locally and then, upload it. Below code is the main part you will need to add in your code.
...
const options = { // construct the file to write
metadata: {
contentType: 'audio/mpeg',
metadata: {
source: 'Google Text-to-Speech'
}
}
};
// copied from https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-usage-nodejs
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
// response.audioContent is the downloaded file
return await file.save(response.audioContent, options)
.then(() => {
console.log("File written to Firebase Storage.")
return;
})
.catch((error) => {
console.error(error);
});
...
Once you have this part implemented, you will have the file that is saved locally downloaded and ready to be uploaded. I would recommend you to take a better look at the other post I mentioned, in case you have more doubts on how to achieve it.
Let me know if the information helped you!
I need to code a REST API that can check if a PDF file exist in a specific folder in the server.
The client send GET request and server should wait before send response, until the PDF file exist.
When the PDF file appears in the folder, the server need to response filename to client.
I think using node.js with express and socket.io to do this.
Do you think it's the right way ?
Have you got a code example for sync wait and file check response ?
Thanks
Before coding REST API routes, i prefer in a first step to code file checking function.
I tested fs.existsSync not really good
const fs = require('fs')
const path = './*.pdf'
if (fs.existsSync(path)) {
//file exists
}
and i am going to test maybe with glob.sync or glob-fs
I don't know what the good way for this first step
Update :
Glob-fs seems to be ok, but I need a wait time until .PDF file arrived on the server fs.
var glob = require('glob-fs')({ gitignore: true });
glob.readdir('**/*.pdf', function(err, files) {
console.log(files);
});
REST API is not what you are looking for. You should not stall your node.js server.
You should use Websocket: You can register your application as interested to know when a file appears in a directory. Then, when that event occurs, the server sends you a notification. No waiting.
Check https://www.tutorialspoint.com/websockets/index.htm for more info about Websockets.
Check https://nodejs.org/api/fs.html#fs_fs_watchfile_filename_options_listener for watching file modifications
Here a code using Chokidar to watch PDF file creation :
var fileWatcher = require("chokidar");
// Initialize watcher.
var watcher = fileWatcher.watch("./*.pdf", {
ignored: /[\/\\]\./,
persistent: true
});
// Add event listeners.
watcher
.on('add', function(path) {
console.log('File', path, 'has been added');
})
I'm using node-telegram-bot-api package
and I have two telegram bots like this:
const TelegramBot = require('node-telegram-bot-api');
let bot1 = new TelegramBot(token1, { polling: true });
let bot2 = new TelegramBot(token2, { polling: true });
I'm kind of making a connection between these two bots, In a way that messages that are recieved from bot1 should be sent to user by bot2 and vice versa
something like:
bot1.on('message', (msg) => {
if (msg.video) { // video in this case
bot2.sendVideo(user2_chatid, msg.video.file_id);
}
});
In the above code I am trying to send the video to user2 using bot2, once bot1 receives a video
using file_id to reference sticker is all right, but for photos, videos, voices etc I get this error
wrong file identifier/HTTP URL specified
Looks like telegram is kind of private about the bots and the data they can share, means that file_id is something private and only accessible by the bot it self
How can I share these data between telegram bots?
and I strongly prefer a way other than saving photos and videos somewhere on the server then sending them
https://core.telegram.org/bots/api#sending-files
file_id is unique for each individual bot and can't be transferred from one bot to another.
so I guess the answer Is no, unless I would save the file somewhere, upload it again using bot2 and then send it
for me a better solution was to use the same bot (bot1) for both users, so I can easily send the received file to any user who has started a chat with the bot