How to receive images from Telegram Bot [duplicate]


How to receive images from Telegram Bot [duplicate]



This question already has an answer here:



Can't receive images from my telegram bot, trying something like this:


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))



No any errors while running



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





I already answered this question how save photo in telegram python bot?. First line in photo_handler needs to be file = bot.getFile(update.message.photo[-1].file_id).
– dev4Fun
Jun 30 at 16:11




1 Answer
1



I've used this to send images generated by matplotlib. You can adapt it to your needs.


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)





The question about how to receive a photo, not how to send it
– dev4Fun
Jun 30 at 14:46






yes, I noticed that just after I posted. I commented on that, but that comment is gone somehow
– hootnot
Jun 30 at 15:09





so can anyone help me with this issue?
– Yevgen
Jun 30 at 15:17

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux