python3 -m pip install telegraphIt is recommended to use Telegraph as a context manager. By doing this, you can make sure that connections are correctly cleaned up when leaving the with block. However, you also have the option to use .close() to explicitly close the connection.
As context manager
from telegraph import Telegraph
def main():
with Telegraph() as telegraph:
telegraph.create_account(short_name='Telegraph')
response = telegraph.create_page(
'Telegraph Python',
html_content='<p>Hello, world!</p>'
)
print(response['url'])
main()As .close()
from telegraph import Telegraph
def main():
telegraph = Telegraph()
try:
telegraph.create_account(short_name='Telegraph')
response = telegraph.create_page(
'Telegraph Python',
html_content='<p>Hello, world!</p>'
)
print(response['url'])
except Exception:
...
finally:
telegraph.close()
main()As context manager
import asyncio
from telegraph import AsyncTelegraph
async def main():
async with AsyncTelegraph() as telegraph:
await telegraph.create_account(short_name='Telegraph')
response = await telegraph.create_page(
'Telegraph Python',
html_content='<p>Hello, world!</p>'
)
print(response['url'])
asyncio.run(main())As .close()
import asyncio
from telegraph import AsyncTelegraph
async def main():
telegraph = AsyncTelegraph()
try:
await telegraph.create_account(short_name='Telegraph')
response = await telegraph.create_page(
'Telegraph Python',
html_content='<p>Hello, world!</p>'
)
print(response['url'])
except Exception:
...
finally:
await telegraph.close()
asyncio.run(main())This is not part of official API, Use at own risk. & Allowed only .jpg, .jpeg, .png, .gif and .mp4 files.
from telegraph import UploadFile
def main():
urls = UploadFile(['ex1.jpg', 'ex2.jpeg', 'ex3.png', 'ex4.gif', 'ex5.mp4'])
for url in urls: print(url)
main()import asyncio
from telegraph import AsyncUploadFile
async def main():
urls = await AsyncUploadFile(['ex1.jpg', 'ex2.jpeg', 'ex3.png', 'ex4.gif', 'ex5.mp4'])
for url in urls: print(url)
asyncio.run(main())