簡體   English   中英

帶有 telegraf.js 的 Telegram 機器人:無法使用 flickr api 發送隨機照片進行聊天

[英]Telegram bot with telegraf.js : can't send random photo to chat using 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 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 ),它也可以生成。

問題是它總是完全相同的照片,我必須總是重新啟動機器人來生成一張新照片 URL。 我在做什么錯,如何避免它並在每次用戶調用命令歌手時發送隨機照片? 任何幫助將不勝感激。

正如我在您的代碼中看到的那樣,您只執行一次 getSingerPhoto

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

只需將其更改為

telegram.command('singer', getSingerPhoto);

編輯:

我不熟悉電報 api,但我也看到您在 axios 的響應中注冊操作,這就是照片被緩存的原因

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

因此,改為在getSingerPhoto(ctx)中添加您將從命令/動作獲得的 ctx 參數,只需在內部調用它並刪除您內部的另一個動作

編輯2:完成的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM