如何将图像上传到 Telegram 服务器
How do I upload an image to Telegram's sever
我正在编写一个发送图像的电报机器人(在 Python 中)。我将每张图片都发送了几次,文档建议发送 file_id 已存储在 Telegram 的服务器中的文件。
但我找不到任何关于在服务器中存储文件并获取 file_id 的文档。我可以尝试发送图像(给自己?给机器人?)并获取它 file_id,但它看起来太老套了。
好的,我知道了..你必须发送一次图像,但是很容易得到 file_id
:
msg = bot.send_photo(chat_id=chat_id, photo=open("filename", "rb"))
file_id = msg.photo[0].file_id
...
bot.send_photo(photo=file_id)
好的!您可以从磁盘或 URL.
发送照片
在其他情况下,如果您从客户端向机器人发送照片,您将在 update.message.photo
中收到 file_id
和 file_path
不同照片尺寸。
然后你可以通过link获取真实文件:https://api.telegram.org/file/bot:TOKEN:/photos/file_3.jpg
您还可以使用 file_id
获取照片阵列:https://api.telegram.org/bot:TOKEN:/getFile?file_id=AAAAAA
带照片的接收数据示例:
{'update_id': 773777337,
'message': {'message_id': 737,
........
'date': 7373377337,
'photo': [{'file_id': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'file_size': 962,
'file_path': 'photos/file_3.jpg',
'width': 90,
'height': 48},
{'file_id': 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
'file_size': 17399,
'width': 320,
'height': 172},
{'file_id': 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
'file_size': 88245,
'width': 800,
'height': 431},
{'file_id': 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD',
'file_size': 168202,
'width': 1174,
'height': 633}],
'caption': 'comment_to_my_uploaded_photo'}}
最后,我的简单机器人示例从 URL 发送随机图像:
#!/usr/bin/python3
from telegram.ext import Updater, Filters
from telegram.ext import CommandHandler, CallbackQueryHandler, MessageHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import requests
def start(bot, update):
update.message.reply_text('Hello! I can show an awesome images for you!',
reply_markup=main_keyboard())
def meow(bot, update):
query = update.callback_query
photo = requests.get('http://aws.random.cat/meow').json()
bot.send_photo(caption="Meow! :)",
photo=photo['file'],
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=action_keyboard())
def wooff(bot, update):
query = update.callback_query
photo = requests.get('https://dog.ceo/api/breed/husky/images/random').json()
bot.send_photo(caption="Bwoof! :)",
photo=photo['message'],
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=action_keyboard())
def like(bot, update):
query = update.callback_query
bot.send_message(text='{}Likewise!'.format(u'\U0001F60A'),
chat_id=query.message.chat_id,
reply_markup=main_keyboard())
def great(bot, update):
query = update.callback_query
bot.send_message(text='{}Great!'.format(u'\U0001F60B'),
chat_id=query.message.chat_id,
reply_markup=main_keyboard())
def main_keyboard():
keyboard = [[InlineKeyboardButton("Get Meow", callback_data='meow`'),
InlineKeyboardButton("Get Wooff", callback_data='wooff')]]
return InlineKeyboardMarkup(keyboard)
def action_keyboard():
keyboard = [[InlineKeyboardButton("Like!", callback_data='like'),
InlineKeyboardButton("Great!", callback_data='great')]]
return InlineKeyboardMarkup(keyboard)
updater = Updater('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(meow, pattern='meow'))
updater.dispatcher.add_handler(CallbackQueryHandler(wooff, pattern='wooff'))
updater.dispatcher.add_handler(CallbackQueryHandler(like, pattern='like'))
updater.dispatcher.add_handler(CallbackQueryHandler(great, pattern='great'))
updater.dispatcher.add_handler(MessageHandler(Filters.text|Filters.photo, start))
updater.start_polling()
尽情享受吧!
我正在编写一个发送图像的电报机器人(在 Python 中)。我将每张图片都发送了几次,文档建议发送 file_id 已存储在 Telegram 的服务器中的文件。
但我找不到任何关于在服务器中存储文件并获取 file_id 的文档。我可以尝试发送图像(给自己?给机器人?)并获取它 file_id,但它看起来太老套了。
好的,我知道了..你必须发送一次图像,但是很容易得到 file_id
:
msg = bot.send_photo(chat_id=chat_id, photo=open("filename", "rb"))
file_id = msg.photo[0].file_id
...
bot.send_photo(photo=file_id)
好的!您可以从磁盘或 URL.
发送照片
在其他情况下,如果您从客户端向机器人发送照片,您将在 update.message.photo
中收到 file_id
和 file_path
不同照片尺寸。
然后你可以通过link获取真实文件:https://api.telegram.org/file/bot:TOKEN:/photos/file_3.jpg
您还可以使用 file_id
获取照片阵列:https://api.telegram.org/bot:TOKEN:/getFile?file_id=AAAAAA
带照片的接收数据示例:
{'update_id': 773777337,
'message': {'message_id': 737,
........
'date': 7373377337,
'photo': [{'file_id': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'file_size': 962,
'file_path': 'photos/file_3.jpg',
'width': 90,
'height': 48},
{'file_id': 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
'file_size': 17399,
'width': 320,
'height': 172},
{'file_id': 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
'file_size': 88245,
'width': 800,
'height': 431},
{'file_id': 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD',
'file_size': 168202,
'width': 1174,
'height': 633}],
'caption': 'comment_to_my_uploaded_photo'}}
最后,我的简单机器人示例从 URL 发送随机图像:
#!/usr/bin/python3
from telegram.ext import Updater, Filters
from telegram.ext import CommandHandler, CallbackQueryHandler, MessageHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import requests
def start(bot, update):
update.message.reply_text('Hello! I can show an awesome images for you!',
reply_markup=main_keyboard())
def meow(bot, update):
query = update.callback_query
photo = requests.get('http://aws.random.cat/meow').json()
bot.send_photo(caption="Meow! :)",
photo=photo['file'],
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=action_keyboard())
def wooff(bot, update):
query = update.callback_query
photo = requests.get('https://dog.ceo/api/breed/husky/images/random').json()
bot.send_photo(caption="Bwoof! :)",
photo=photo['message'],
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=action_keyboard())
def like(bot, update):
query = update.callback_query
bot.send_message(text='{}Likewise!'.format(u'\U0001F60A'),
chat_id=query.message.chat_id,
reply_markup=main_keyboard())
def great(bot, update):
query = update.callback_query
bot.send_message(text='{}Great!'.format(u'\U0001F60B'),
chat_id=query.message.chat_id,
reply_markup=main_keyboard())
def main_keyboard():
keyboard = [[InlineKeyboardButton("Get Meow", callback_data='meow`'),
InlineKeyboardButton("Get Wooff", callback_data='wooff')]]
return InlineKeyboardMarkup(keyboard)
def action_keyboard():
keyboard = [[InlineKeyboardButton("Like!", callback_data='like'),
InlineKeyboardButton("Great!", callback_data='great')]]
return InlineKeyboardMarkup(keyboard)
updater = Updater('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(meow, pattern='meow'))
updater.dispatcher.add_handler(CallbackQueryHandler(wooff, pattern='wooff'))
updater.dispatcher.add_handler(CallbackQueryHandler(like, pattern='like'))
updater.dispatcher.add_handler(CallbackQueryHandler(great, pattern='great'))
updater.dispatcher.add_handler(MessageHandler(Filters.text|Filters.photo, start))
updater.start_polling()
尽情享受吧!