Programming your Telegram bot to fetch Document content with Telegraf API

szenius profile image Sze Ying 🌻 Updated on ・1 min read

While working on send4me, a message scheduler bot on Telegram, I ran into the question of how my bot can fetch the contents of a document uploaded by a user. I couldn't find a complete example for this use case anywhere, so I put together what worked for me.

A simple demo for fetching Document content

A simple demo for fetching Document content

The following code snippet recreates the demo above. We will assume that you have already created a bot with BotFather and obtained your bot token.

const Telegraf = require('telegraf');
const axios = require('axios');

const BOT_TOKEN = ''; // TODO: get from BotFather

const bot = new Telegraf(BOT_TOKEN, {
  polling: true,

bot.start((ctx) => ctx.reply("Hello! Upload any document and I'll read it for you~"));

bot.on('document', async (ctx) => {
  const {file_id: fileId} = ctx.update.message.document;
  const fileUrl = await ctx.telegram.getFileLink(fileId);
  const response = await axios.get(fileUrl);
  ctx.reply('I read the file for you! The contents were:\n\n' + response.data);


This should work for simple text files. For more complex files, response.data will need to be parsed before their content is usable.

Posted on May 3 by:

szenius profile

Sze Ying 🌻


Hi I am a Software Engineer and I don't wear the same grey shirt to work every day


markdown guide