在电报机器人中从联机模式发送本地照片



我使用Python电报机器人程序API作为我的机器人程序。

我想在本地生成照片并将其作为内联结果发送,但InlineQueryResultPhoto只接受照片URL。

假设我的项目结构如下:

main.py
photo.jpg

如何将photo.jpg作为内联结果发送?

这是main.py:的代码

from uuid import uuid4
from telegram.ext import InlineQueryHandler, Updater
from telegram import InlineQueryResultPhoto

def handle_inline_request(update, context):
update.inline_query.answer([
InlineQueryResultPhoto(
id=uuid4(),
photo_url='',  # WHAT DO I PUT HERE?
thumb_url='',  # AND HERE?
)
])

updater = Updater('TELEGRAM_TOKEN', use_context=True)
updater.dispatcher.add_handler(InlineQueryHandler(handle_inline_request))
updater.start_polling()
updater.idle()

由于Telegram Bot API不提供,因此没有直接答案。但有两种解决方法:您可以使用上传照片到电报服务器,然后使用InlineQueryResultCachedPhoto,也可以上传到任何图像服务器,然后再使用InlineQueryResultPhoto

InlineQueryResultCachedPhoto

第一个选项要求您在创建结果列表之前先将照片上传到电报服务器。你有哪些选择?机器人可以给你发照片信息,获取信息并使用你需要的东西。另一种选择是创建一个私人频道,你的机器人可以在那里发布它将重复使用的照片。该方法的唯一细节是了解channel_id(如何获得私人Telegram频道的chat_id?(。

现在让我们看看一些代码:

from config import tgtoken, privchannID
from uuid import uuid4
from telegram import Bot, InlineQueryResultCachedPhoto
bot = Bot(tgtoken)
def inlinecachedphoto(update, context):
query = update.inline_query.query
if query == "/CachedPhoto":
infophoto = bot.sendPhoto(chat_id=privchannID,photo=open('logo.png','rb'),caption="some caption")
thumbphoto = infophoto["photo"][0]["file_id"]
originalphoto = infophoto["photo"][-1]["file_id"]
results = [
InlineQueryResultCachedPhoto(
id=uuid4(),
title="CachedPhoto",
photo_file_id=originalphoto)
]
update.inline_query.answer(results)

当你把照片发送到聊天室/群组/频道时,你可以获得file_id、缩略图的file_id,标题和其他我要跳过的细节。怎么了?如果你没有过滤正确的查询,你可能会多次将照片发送到你的私人频道。这也意味着自动完成无法工作。

InlineQueryResultPhoto

另一种选择是将照片上传到互联网,然后使用网址。除了自己的主机之类的选项,您可以使用一些免费的提供API的映像主机(例如:imgur、imgbb(。对于这段代码,在imgbb中生成自己的密钥比imgur更简单。一旦生成:

import requests
import json
import base64
from uuid import uuid4
from config import tgtoken, key_imgbb
from telegram import InlineQueryResultPhoto
def uploadphoto():
with open("figure.jpg", "rb") as file:
url = "https://api.imgbb.com/1/upload"
payload = {
"key": key_imgbb,
"image": base64.b64encode(file.read()),
}
response = requests.post(url, payload)
if response.status_code == 200:
return {"photo_url":response.json()["data"]["url"], "thumb_url":response.json()["data"]["thumb"]["url"]}
return None
def inlinephoto(update, context):
query = update.inline_query.query
if query == "/URLPhoto":
upphoto = uploadphoto()
if upphoto:
results = [
InlineQueryResultPhoto(
id=uuid4(),
title="URLPhoto",
photo_url=upphoto["photo_url"],
thumb_url=upphoto["thumb_url"])
]
update.inline_query.answer(results)

这段代码类似于前面的方法(也包括同样的问题(:如果不过滤查询,则多次上传,并且在编写内联时不会自动完成。

免责声明

这两个代码都是在收到查询的那一刻生成的,否则你可以在收到查询之前完成工作,将信息保存在数据库中。

奖金

您可以使用pyTelegramBotAPI 运行自己的机器人来获取私人频道的channel_id

import telebot
bot = telebot.TeleBot(bottoken)
@bot.channel_post_handler(commands=["getchannelid"])
def chatid(message):
bot.reply_to(message,'channel_id = {!s}'.format(message.chat.id))
bot.polling()

要获得id,您需要在通道/getchannelid@botname中写入

最新更新