首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Telegram Bot接收图像

如何从Telegram Bot接收图像
EN

Stack Overflow用户
提问于 2018-06-30 16:12:00
回答 1查看 6.3K关注 0票数 2

无法接收来自我的电报机器人的图像,尝试这样的操作:

代码语言:javascript
复制
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))

运行时没有任何错误

EN

回答 1

Stack Overflow用户

发布于 2018-06-30 17:35:54

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

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51113062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档