node.jstelegramtelegram-botnode-telegram-bot-api

How should I download received files from telegram api


I just want to download images received by my telegram bot with nodejs but I dont know witch method to use. I'm using node-telegram-bot-api and I tried this code :

bot.on('message', (msg) => {
    const img = bot.getFileLink(msg.photo[0].file_id);

    console.log(img);
});

That's the result:

Promise [Object] {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined,
  _cancellationParent:
   Promise [Object] {
     _bitField: 1,
     _fulfillmentHandler0: [Function],
     _rejectionHandler0: undefined,
     _promise0: [Circular],
     _receiver0: undefined,
     _cancellationParent:
      Promise [Object] {
        _bitField: 1,
        _fulfillmentHandler0: undefined,
        _rejectionHandler0: [Function],
        _promise0: [Circular],
        _receiver0: undefined,
        _cancellationParent: [Promise],
        _branchesRemainingToCancel: 1 },
     _branchesRemainingToCancel: 1 } }

Solution

  • bot.on('message', async (msg) => {
        if (msg.photo && msg.photo[0]) {
            const image = await bot.getFile({ file_id: msg.photo[0].file_id });
            console.log(image);
        }
    });
    

    https://github.com/mast/telegram-bot-api/blob/master/lib/telegram-bot.js#L1407