12

I'd like to send a Image (via URL or Path), on request. I use the source code here. The code has already a sample to send an image (via URL or Path), but I don't get it since I'm new to Python.

Here's the sample code snippet:

elif text == '/image':        #request
    img = Image.new('RGB', (512, 512))
    base = random.randint(0, 16777216)
    pixels = [base+i*j for i in range(512) for j in range(512)]  # generate sample image
    img.putdata(pixels)
    output = StringIO.StringIO()
    img.save(output, 'JPEG')
    reply(img=output.getvalue())

Some API infos can be found here.

Thanks for your patience.

azro
  • 53,056
  • 7
  • 34
  • 70
ColinDave
  • 490
  • 1
  • 6
  • 16
  • Please see my answer here: https://stackoverflow.com/a/32441772/1097372 I'm also new to python, so maybe someone could improve my code there. – Iyas Sep 07 '15 at 15:17

2 Answers2

10

To send a photo from URL:

bot.send_photo(chat_id=chat_id, photo='https://telegram.org/img/t_logo.png')

To send a photo from local Drive:

bot.send_photo(chat_id=chat_id, photo=open('tests/test.png', 'rb'))

Here is the reference documentation.

4

I was struggling to connect the python-telegram-bot examples with the above advice. Especially, while having context and update, I couldnt find chatid and bot. Now, my two cents:

pic=os.path.expanduser("~/a.png")
context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(pic,'rb'))
jaromrax
  • 274
  • 1
  • 12