Skip to content

Instantly share code, notes, and snippets.

@witnessmenow
Last active April 16, 2016 12:00
Show Gist options
  • Save witnessmenow/d472db846073bf72b5b673ea4d15852c to your computer and use it in GitHub Desktop.
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
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