This topic was automatically generated from Slack. You can find the original thread here.
what’s the code to get a file on a Telegram server?
This topic was automatically generated from Slack. You can find the original thread here.
what’s the code to get a file on a Telegram server?
To get a file from a Telegram server, you’ll need to use the Telegram Bot API. First, you need to obtain the file_id
of the file you want to download. You can get this from the message object when a user sends a file to your bot. Then, you can use the getFile
method to get the file path and construct the download URL. Here’s an example Pipedream component that demonstrates how to get a file from a Telegram server using the Telegram Bot API: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { botToken: { type: "string", label: "Telegram Bot Token", }, fileId: { type: "string", label: "File ID", }, }, async run({ $ }) { const getFileUrl = `https://api.telegram.org/bot${this.botToken}/getFile`; const response = await axios($, { method: "GET", url: getFileUrl, params: { file_id: this.fileId, }, }); const filePath = response.result.file_path; const fileDownloadUrl = `https://api.telegram.org/file/bot${this.botToken}/${filePath}`; return { fileDownloadUrl, }; }, });
Replace this.botToken
and this.fileId
with your Telegram bot token and the file ID you want to download, respectively. This component will return the download URL for the file, which you can use to download the file using an HTTP client like axios
.
Mario currently isn’t hooked up to our AI code service (but will be soon). You might also give that a try, as well, and it should produce better code:
> curl -d '{"prompt": "what's the code to get a file on a Telegram server?", "app": "Telegram Bot"}' https://ai.m.pipedream.net
I’m running that now to see what it produces
import { axios } from "@pipedream/platform";
export default defineComponent({
props: {
telegram_bot_api: {
type: "app",
app: "telegram_bot_api",
},
fileId: {
type: "string",
label: "File ID",
description: "The ID of the file to get from the Telegram server",
async options(opts) {
const response = await axios(this, {
url: `https://api.telegram.org/bot${this.telegram_bot_api.$auth.token}/getUpdates`,
});
const fileIds = response.result.flatMap((update) => {
if (update.message && update.message.document) {
return [update.message.document.file_id];
}
return [];
});
return fileIds.map((fileId) => ({ label: fileId, value: fileId }));
},
},
},
async run({ steps, $ }) {
const response = await axios($, {
url: `https://api.telegram.org/bot${this.telegram_bot_api.$auth.token}/getFile`,
params: {
file_id: this.fileId,
},
});
const fileUrl = `https://api.telegram.org/file/bot${this.telegram_bot_api.$auth.token}/${response.result.file_path}`;
return await axios($, {
url: fileUrl,
responseType: "arraybuffer",
});
},
});
Please let me know if that works or fails — I’d love to incorporate any fixes into the AI service
Thanks . The first option provided me with a URL of the audio file, the second a Buffer type which I don’t understand
The second came with the Telegram branding which is great
How do you want to use the file? e.g. do you want to write it to disk so you can read it in a future step?
It’s usually a short audio, 20s max. I want to transcribe it with OpenAI
It comes in OGA format so eventually I’ll need to convert to MP3
ok nice, one moment
import { axios } from "@pipedream/platform";
import ffmpeg from "fluent-ffmpeg";
import { promises as fs } from "fs";
import { createWriteStream } from "fs";
import { join } from "path";
import { promisify } from "util";
import { pipeline } from "stream";
const streamPipeline = promisify(pipeline);
export default defineComponent({
props: {
telegram_bot_api: {
type: "app",
app: "telegram_bot_api",
},
file_id: {
type: "string",
label: "File ID",
description: "The file_id of the OGA file on the Telegram server",
},
},
async run({ steps, $ }) {
// Get the file path on the Telegram server
const { file_path } = await axios($, {
url: `https://api.telegram.org/bot${this.telegram_bot_api.$auth.token}/getFile`,
params: {
file_id: this.file_id,
},
});
// Download the OGA file
const ogaFileUrl = `https://api.telegram.org/file/bot${this.telegram_bot_api.$auth.token}/${file_path}`;
const ogaResponse = await axios($, {
url: ogaFileUrl,
responseType: "stream",
});
// Save the OGA file to the /tmp directory
const ogaFilePath = join("/tmp", `${this.file_id}.oga`);
await streamPipeline(ogaResponse.data, createWriteStream(ogaFilePath));
// Convert the OGA file to MP3 and save it to the /tmp directory
const mp3FilePath = join("/tmp", `${this.file_id}.mp3`);
await new Promise((resolve, reject) => {
ffmpeg(ogaFilePath)
.format("mp3")
.on("error", reject)
.on("end", resolve)
.save(mp3FilePath);
});
// Remove the OGA file
await fs.unlink(ogaFilePath);
// Return the path to the saved MP3 file
return mp3FilePath;
},
});
Can you try that? My prompt was:
❯ curl -d '{"prompt": "I want to download a file on a Telegram server. The file is in OGA format, and I want to convert it to mp3 with ffmpeg, save the file to the /tmp directory, then return the path to the saved
file. ffmpeg is not installed to the environment, so I need to download and include it via an npm package", "app": "Telegram Bot"}' https://ai.m.pipedream.net
It created a code that is giving me
thanks, that’s helpful. It looks like the issue is with the fluent-ffmpeg
package, trying another. Thanks for the help testing
Newest version. I’m still working to upload an OGA file to Telegram to test end-to-end.
import { axios } from "@pipedream/platform";
import ffmpegPath from "@ffmpeg-installer/ffmpeg";
import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
export default defineComponent({
props: {
telegram_bot_api: {
type: "app",
app: "telegram_bot_api",
},
file_id: {
type: "string",
label: "File ID",
description: "The file ID of the OGA file on the Telegram server",
},
},
async run({ steps, $ }) {
const getFileUrl = async () => {
const response = await axios($, {
method: "GET",
url: `https://api.telegram.org/bot${this.telegram_bot_api.$auth.token}/getFile`,
params: {
file_id: this.file_id,
},
});
const filePath = response.result.file_path;
return `https://api.telegram.org/file/bot${this.telegram_bot_api.$auth.token}/${filePath}`;
};
const downloadFile = async (url) => {
const response = await axios($, {
method: "GET",
url,
responseType: "arraybuffer",
});
return response;
};
const convertOgaToMp3 = async (inputPath, outputPath) => {
const command = `${ffmpegPath.path} -i ${inputPath} -codec:a libmp3lame -qscale:a 2 ${outputPath}`;
await execAsync(command);
};
const fileUrl = await getFileUrl();
const ogaData = await downloadFile(fileUrl);
const inputPath = "/tmp/input.oga";
const outputPath = "/tmp/output.mp3";
await fs.promises.writeFile(inputPath, ogaData);
await convertOgaToMp3(inputPath, outputPath);
return outputPath;
},
});