gpt4 book ai didi

python - 如何从 Telegram Bot 接收图像

转载 作者:行者123 更新时间:2023-12-01 09:17:27 24 4
gpt4 key购买 nike

无法从我的电报机器人接收图像,请尝试如下操作:

import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler
from telegram.ext import Filters

def photo_handler(bot, update):
file = bot.getFile(update.message.photo.file_id)
print ("file_id: " + str(update.message.photo.file_id))
file.download('photo.jpg')

updater = Updater(token='my token')
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.photo, photo_handler))

运行时没有任何错误

最佳答案

我用它来发送由 matplotlib 生成的图像。您可以根据您的需要进行调整。

import telegram
from telegram.bot import Bot
import yachain
import cStringIO
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

cfg = yachain.Config().load("telegram.cfg")

token = cfg["token"]
chat_id = cfg["chatID"]

bot = Bot(token=token)

y = [2,4,6,8,10,12,14,16,18,20]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()

buffer = cStringIO.StringIO()
fig.savefig(buffer, format='png')

buffer.seek(0)
bot.send_photo(chat_id=chat_id,
photo=buffer)

关于python - 如何从 Telegram Bot 接收图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51113062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com