带有 telegraf.js 的 Telegram bot:无法使用 flickr api

问题描述

我是电报机器人创建的新手,想制作一个简单的机器人,它允许用户在命令中选择歌手或演员的照片,然后使用 flickr API 将其发送到聊天:

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 没问题 - 我获取照片数组 (photosArray),然后从中取出一个随机照片对象 (photoObject),然后将其放入必要的照片 URL (https://live.staticflickr.com/${server}/${id}_${secret}_q.jpg),它生成的也正常.

问题是它总是完全相同的照片,我必须总是重新启动机器人以生成新的照片 URL。我做错了什么,如何避免它并在每次用户调用命令歌手时发送随机照片?任何帮助将不胜感激。

解决方法

正如我在您的代码中看到的,您只执行一次 getSingerPhoto

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

只需将其更改为

telegram.command('singer',getSingerPhoto);

编辑:

我不熟悉 Telegraf api,但我也看到您在 axios 的响应中注册了动作,这就是缓存照片的原因

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

因此,在 getSingerPhoto(ctx) 中添加 ctx 参数,您将从命令/操作中获得,只需在内部调用它并删除您在内部的另一个操作

edit2: 完成代码:

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);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...