ColinDave
ColinDave

Reputation: 530

Python Telegram Bot - Send Image

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.

Upvotes: 12

Views: 26423

Answers (2)

jaromrax
jaromrax

Reputation: 283

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

Upvotes: 4

Saravanan Manoharan
Saravanan Manoharan

Reputation: 371

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.

Upvotes: 10

Related Questions