python - 如何从 Telegram Bot 接收图像
问题描述
无法从我的电报机器人接收图像,尝试这样的事情:
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)
推荐阅读
- snowflake-cloud-data-platform - 将 snowsql 选择查询输出分配给 unix 变量
- intellij-idea - 如何在 IntelliJ Idea 更新之间保留自定义属性?
- flutter - 接受 Flutter 的 android 许可证不起作用
- grafana - grafana 文件夹和仪表板的最大数量是多少?
- laravel - Laravel Eloquent - 从 GrandParents 获取孙子数据的方法
- r - lm 的自定义打印因子/对比度
- c - 分叉后关闭管道文件描述符时出现错误的文件描述符错误
- flutter - 具有渐变背景颜色的动画容器
- python - 如何将数据框汇总为与 ID 相结合的列表?
- vb6 - 使用 VB6 发送多媒体键