Telegram bot with telegraf.js : can't send random photo to chat using flickr api

652 Views Asked by At

I am a newbie in telegram bots creation and want to make a simple bot which allows a user to choose between a singer or actor photo in the commands and then using the flickr API send it to the chat:

const Telegraf = require('telegraf')
const { Router, Markup } = Telegraf
const axios = require('axios')
const api_key = '123'

const telegram = new Telegraf('123')

const inlineMessageRatingKeyboard = Markup.inlineKeyboard([
    Markup.callbackButton('Singer', 'singer'),
    Markup.callbackButton('Actor', 'actor')
]).extra()


const getSingerPhoto = () => {
    axios.get(`https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&tags=gerard+way&format=json&nojsoncallback=1`)
        .then(photosInfo => {
            const photosArray = (photosInfo.data && photosInfo.data.photos && photosInfo.data.photos.photo) || null;
            const photoObject = (photosArray && photosArray[photosArray.length * Math.random() | 0]) || null;
            let { server, id, secret } = photoObject;
            telegram.action('singer', (ctx) => {
                ctx.replyWithPhoto({
                    url: `https://live.staticflickr.com/${server}/${id}_${secret}_q.jpg`
                })
            })
        })
        .catch(error => console.log(error));
}

telegram.on('message', (ctx) => ctx.telegram.sendMessage(
    ctx.from.id,
    'What kind of photo do you want?',
    inlineMessageRatingKeyboard
)
)

telegram.command('singer', getSingerPhoto());

telegram.action('actor', (ctx) => {
    ctx.replyWithPhoto({
        source: './way.png'
    })
})

telegram.startPolling()

Flickr API is okay - I get the photo array (photosArray) and then take a random photo object (photoObject) from it, then I put it to the necessary photo URL (https://live.staticflickr.com/${server}/${id}_${secret}_q.jpg) and it generates okay too.

The problem is that it's always exactly the same photo, I have to always restart the bot to generate a new photo URL. What am I doing wrong, how to avoid it and send a random photo every time a user calls the command singer? Any help will be appreciated.

1

There are 1 best solutions below

2
On

As i see in your code you executing getSingerPhoto only once

telegram.command('singer', getSingerPhoto());

just change it to

telegram.command('singer', getSingerPhoto);

edit:

i'm not familiar with telegraf api, but i also see that you registering action inside a response of axios, so that's why photo being cached

telegram.action('singer', (ctx) => {
              ctx.replyWithPhoto({
                  url: `https://live.staticflickr.com/${server}/${id}_${secret}_q.jpg`
              })
          })

so, instead add ctx param in getSingerPhoto(ctx) that you will get from command/action, just call it inside and remove another action that you have inside

edit2: completed code:

const getSingerPhoto = (ctx) => {
    axios.get(`https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&tags=gerard+way&format=json&nojsoncallback=1`)
        .then(photosInfo => {
            const photosArray = (photosInfo.data && photosInfo.data.photos && photosInfo.data.photos.photo) || null;
            const photoObject = (photosArray && photosArray[photosArray.length * Math.random() | 0]) || null;
            let { server, id, secret } = photoObject;
            ctx.replyWithPhoto({
                url: `https://live.staticflickr.com/${server}/${id}_${secret}_q.jpg`
            })
        })
        .catch(error => console.log(error));
}

telegram.action('singer', getSingerPhoto);