0

我目前正在使用Node API Telegraf为 Telegram 编程机器人,试图从IMDB API中的承诺中获取返回值,并且在某些情况下工作正常。问题是 catch 总是运行,并且在某些情况下,IMDB API 在内联搜索时找不到电影,但在使用 /search 时找到它。

const Telegraf = require( 'telegraf' )
const imdb = require( 'imdb-api' )
const bot = new Telegraf( process.env.BOT_TOKEN )

bot.command( 'search', ctx => {
    const movie = ctx.message.text.split(' ').slice( 1 ).join(' ')

    imdb.get( movie ).then( response => ctx.reply( response.imdburl ) )
    .catch( console.log( 'Reject promise in search function' ) )
} )

function inline_search( movie, callback ) {
    const inline = [ ]

    imdb.get( movie )
    .then( response => {
            inline.push( {
                id: response.title,
                title: response.title,
                type: 'article',
                input_message_content: {
                    message_text: response.imdburl,
                    parse_mode: 'HTML'
                },
                url: response.url,
                description: response.plot,
                thumb_url: response.poster
                }
            )
        callback( inline )
    } )
    .catch( console.log( 'Reject promise in inline search function' ) )
}

bot.on( 'inline_query', ctx => {
    const movie = ctx.inlineQuery.query || ''

    inline_search( movie, response => {
        const result = response
                        .filter( value => value.title.toLowerCase()
                        .indexOf( movie.toLowerCase() ) !== -1 )

        ctx.answerInlineQuery( result )
    } )
} )
4

0 回答 0