首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有telegraf.js的电报机器人:不能用flickr发送随机照片来聊天

带有telegraf.js的电报机器人:不能用flickr发送随机照片来聊天
EN

Stack Overflow用户
提问于 2021-01-29 16:59:28
回答 1查看 423关注 0票数 1

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

代码语言:javascript
复制
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是可以的-我得到照片阵列(photosArray),然后从它获取一个随机照片对象(photoObject),然后我将它放到必要的照片URL (https://live.staticflickr.com/${server}/${id}_${secret}_q.jpg)中,它也会生成。

问题是,它总是完全相同的照片,我必须总是重新启动机器人来生成一个新的照片URL。我做错了什么,如何避免它和发送随机照片,每次用户打电话给指挥歌手?任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2021-02-10 09:52:20

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

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

只要把它改成

telegram.command('singer', getSingerPhoto);

编辑:

我不熟悉telegraf,但我也看到您在axios的响应中注册了操作,这就是为什么缓存照片的原因

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

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

edit2:完成代码:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65958710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档