const fs = require('fs')
const Telegraf = require('../')

const bot = new Telegraf(process.env.BOT_TOKEN)

const downloadPhotoMiddleware = (ctx, next) => {
  return bot.telegram.getFileLink(ctx.message.photo[0])
    .then((link) => {
      console.log(link)
      ctx.state.fileLink = link
      return next()
    })
}

bot.on('photo', downloadPhotoMiddleware, (ctx, next) => {
  // return ctx.replyWithPhoto({ source: '/directory/file.jpeg' })
})

bot.on('text', (ctx) => {
  return Promise.all([
    // file
    ctx.replyWithPhoto({
      source: '/example/cat.jpeg'
    }),

    // Stream
    ctx.replyWithPhoto({
      source: fs.createReadStream('/example/cat2.jpeg')
    }),

    // Buffer
    ctx.replyWithPhoto({
      source: fs.readFileSync('/example/cat3.jpeg')
    }),

    // url
    ctx.replyWithPhoto({
      url: 'http://lorempixel.com/400/200/cats/'
    }),

    // url via Telegram servers
    ctx.replyWithPhoto('http://lorempixel.com/400/200/cats/')
  ])
})

bot.startPolling()

@@ -231,32 +231,8 @@
 %3E %7B%0A
-      console.log(link)%0A
     
@@ -349,18 +349,63 @@
  =%3E %7B%0A  
-//
+console.log('Photo url:', ctx.state.fileLink)%0A 
  return 

update getFileLink example