Last active
April 16, 2016 12:00
-
-
Save witnessmenow/d472db846073bf72b5b673ea4d15852c to your computer and use it in GitHub Desktop.
Example of a getting a file from Telegram without exposing your Bot Token using nodeJS and express. TelegramClient.getFileDetails is a call to the Telegram getFile api. The file is then requested and the response is pipped into response sent back to the user
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
router.get('/:documentId/*', (req, res) => { | |
var documentId = req.params.documentId; | |
if(documentId){ | |
TelegramClient.getFileDetails(documentId).then((fileDetails) => { | |
var fileUrl = TelegramClient.getTelegramFileUrl(fileDetails.result.file_path); | |
request(fileUrl) | |
.on('response', (response) => { | |
if (response.statusCode === 200) { | |
res.writeHead(200, { | |
'Content-Type': response.headers['content-type'] | |
}); | |
response.pipe(res); | |
} else { | |
res.writeHead(response.statusCode); | |
res.end(); | |
} | |
}); | |
}); | |
} | |
else{ | |
res.statusCode = 500; | |
res.json({ | |
error: 'Not a valid file ID', | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment