Uploading multiple images to Telegraph using Python and Telegram bot

Hey everyone! I’m new to coding and could use some help. I’m trying to figure out how to upload several photos to Telegraph using Python. Is there a way to do this?

I saw there’s a create_page method, but I’m not sure how to use it for multiple images. Can I just send the photo links to the content parameter? Or is there a better way?

Here’s a simple example of what I’m trying to do:

from telegraph import Telegraph

telegraph = Telegraph()

photo_urls = [
    'https://example.com/photo1.jpg',
    'https://example.com/photo2.jpg',
    'https://example.com/photo3.jpg'
]

# How do I upload these photos?
# page = telegraph.create_page(title='My Photos', content=???

print('Photos uploaded successfully!')

Any tips or advice would be super helpful. Thanks in advance!

I’ve worked with Telegraph API before, and there’s a neat trick you can use to simplify the process. Instead of creating the content list manually, you can leverage Telegraph’s upload_file method. Here’s how:

from telegraph import Telegraph
import requests

telegraph = Telegraph()

photo_urls = [
    'https://example.com/photo1.jpg',
    'https://example.com/photo2.jpg',
    'https://example.com/photo3.jpg'
]

content = []
for url in photo_urls:
    image = requests.get(url).content
    upload = telegraph.upload_file(image)
    content.append({"tag": "img", "attrs": {"src": upload[0]}})

page = telegraph.create_page(title='My Photos', content=content)
print(f'Photos uploaded. Page URL: {page[\"url\"]}')

This approach downloads each image and uploads it directly to Telegraph’s servers, ensuring compatibility and avoiding potential issues with external image hosts. It’s a bit more robust and gives you more control over the process.

I’ve actually done something similar recently for a project. Here’s what worked for me:

You’re on the right track with the create_page method, but you need to structure the content properly. Telegraph expects the content as a list of node elements. For images, you can use the ‘img’ tag with the ‘src’ attribute.

Here’s how I modified your code to make it work:

from telegraph import Telegraph

telegraph = Telegraph()

photo_urls = [
    'https://example.com/photo1.jpg',
    'https://example.com/photo2.jpg',
    'https://example.com/photo3.jpg'
]

content = [{'tag': 'img', 'attrs': {'src': url}} for url in photo_urls]

page = telegraph.create_page(title='My Photos', content=content)

print(f'Photos uploaded successfully! Page URL: {page["url"]}')

This approach creates a list of image nodes that Telegraph can understand. Each photo gets its own ‘img’ tag with the source URL.

One thing to keep in mind: make sure your image URLs are direct links to the image files. Some hosting services might not work with Telegraph.

Hope this helps! Let me know if you run into any issues.

hey noah! i’ve done this b4. try this:

content =
for url in photo_urls:
content.append({‘tag’: ‘img’, ‘attrs’: {‘src’: url}})

page = telegraph.create_page(title=‘My Photos’, content=content)

This shud work for ya. it creates a list of img tags with ur urls. lmk if u need more help!