Last active
April 3, 2024 15:16
-
-
Save gilpanal/099ff5fc94366fbaabd5e2fbedc7c86f to your computer and use it in GitHub Desktop.
The following code demonstrates how to get a file from Telegram without exposing Bot Secret Token in the client side. In this case is used to retrieve a MP3, but the example should work for other file types. An intermediate API, (server.js) built using Node.js, get the raw data of the file and forward it to the web client (index.html and app.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TEL_PATH = '/music/file_352.mp3' | |
const API_FILEDONWLOAD = 'http://localhost:3000/fileDownload?' | |
const load = () => { | |
return new Promise((resolve, reject) => { | |
const xhr = new XMLHttpRequest() | |
xhr.open('GET', API_FILEDONWLOAD + TEL_PATH, true) | |
xhr.responseType = 'arraybuffer' | |
xhr.send() | |
xhr.addEventListener('progress', (e) => { | |
console.log(`${e.type}: ${e.loaded} bytes transferred\n`) | |
}) | |
xhr.addEventListener('load', (e) => { | |
const audioData = e.target.response || e.target.result | |
resolve(audioData) | |
}) | |
xhr.addEventListener('error', () => { | |
reject(Error('Track ' + TEL_PATH + ' failed to load')) | |
}) | |
}) | |
} | |
load().then((audiData) => { | |
console.log(audiData) | |
}).catch((err) =>{ | |
console.log(err) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
BODY | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TESTED WITH NODE VERSION 14+ */ | |
const express = require('express') | |
const app = express() | |
const https = require('https') | |
const port = process.env.PORT || 3000 | |
// Use an Environment Variable to Secure Token Value | |
const BOT_TOKEN = <BOT_SECRET_TOKEN> | |
// For better CORS: https://expressjs.com/en/resources/middleware/cors.html | |
app.use( (req, res, next) => { | |
res.header('Access-Control-Allow-Origin', '*') | |
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept') | |
next() | |
}) | |
app.get('/', (req, res) => { | |
res.sendStatus(200) | |
}) | |
// Inspired by: https://stackoverflow.com/a/21024737 | |
app.get('/fileDownload', (req, res) => { | |
let uploadResponse = { ok: false, result: null, error: 404, description: 'Not Found' } | |
if (req._parsedUrl && req._parsedUrl.query) { | |
const tel_file_path = 'https://api.telegram.org/file/bot' + BOT_TOKEN + req._parsedUrl.query | |
https.get(tel_file_path, (response) => { | |
const data = [] | |
response.on('data', (chunk) => { | |
data.push(chunk) | |
}).on('end', () => { | |
const buffer = Buffer.concat(data) | |
res.send(buffer) | |
}) | |
}) | |
} else { | |
res.sendStatus(uploadResponse) | |
} | |
}) | |
app.listen(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment