如何在电报bot中实现自动完成搜索电影名称

问题描述:

我在后端使用NODEJS从IMDB NPM api获取电影数据。以下是我的server.js代码。如何在电报bot中实现自动完成搜索电影名称

server.js

var token ="token"; 
var telegramBot= require('node-telegram-bot-api'); 
var bot= new telegramBot(token,{polling:true}); 
var express= require('express'); 
var imdb = require('imdb-api'); 

var app =express(); 

app.listen(8080,function(){ 
    console.log("listening on 8080") 
}) 

bot.onText(/Moviename/, function(msg, match) { 
    data = msg.text; 
    imdb.get(msg.text, function(err, movieList){ 
     console.log(movieList); 
     bot.sendMessage(msg.chat.id,movieList); 
     //how i send data to bot when I type on keyboard 

    }); 
}); 
+0

可能是你需要[在线模式](HTTPS: //core.telegram.org/bots/inline)? – anatol

对于使用autocomplete你可能需要阅读inline mode documentation

工作例如:

// Docs: https://github.com/telegraf/micro-bot 
const { Composer } = require('micro-bot') 
const imdb = require('imdb-api') 

const bot = new Composer() 

bot.on('inline_query', async (ctx) => { 
    const movies = await imdb.get(ctx.inlineQuery.query) 
    const results = movies.map((movie) => ({ 
    type: 'article', 
    id: movie.id, 
    title: movie.title, 
    photo_url: movie.poster 
    })) 
    return ctx.answerInlineQuery(results) 
}) 


module.exports = bot 

Another example of inline bot