I'm trying to upload a file which is an image to a Telegraph page via its API. Everything works fine if I send a link, an url to a file which is already uploaded to web.
url_telegraph_API = 'https://api.telegra.ph/'
short_name = 'forecast_bot_help'
access_token = 'my_token'
path = 'Kak-byl-poluchen-prognoz-kursa-12-06-9'
just_image_url = 'https://www.import.io/wp-content/uploads/2021/02/manuel-t-xf1nszrKH_s-
unsplash-scaled.jpg'
content_update = \
[
{'tag': 'strong', 'id': 'Node', 'children': ['''Конечно, никакого сигнала из Космоса не
было.
Да и ИИ, искуственный интеллект, - всего лишь генератор случайных чисел.'''], },
{'tag': 'br'},
{'tag': 'figure', 'children': [
{'tag': 'img', 'attrs': {'src': just_image_url}},
{'tag': 'figcaption'}
]},
{"tag":"p","children":["test"]},
{'tag': 'i', 'children':['''Но точность многих предсказаний курсов имееет такую же
ценность.''',
{'tag': 'i', 'children': [' В чем не сложно убедиться.']}]},
{'tag': 'br'},
{'tag': 'p', 'children': ['Поэтому улыбнитесь и двигайтесь дальше.']}
]
cont = json.dumps(content_update)
command = 'editPage?'
params = {
'access_token': access_token,
'path': path,
'title': title,
'author_name': author_name,
'auth_url': auth_url,
'content': cont,
'return_content': True
}
url = f'{url_telegraph_API}{command}'
create_page = requests.post(url, json=params)
My question is does it exist any method to
- upload a local file to Telegraph via API;
- get its url.
I've been trying to apply
def telegraph_file_upload(file):
url_telegraph_API = 'https://api.telegra.ph/'
command = 'upload'
params = {'file': file}
url = f'{url_telegraph_API}{command}'
response = requests.get(url, params=params)
return response
with open('test_image.jpg', 'rb') as f:
file = f.read()
r = telegraph_file_upload(file)
print(r.content())
But with NO success so far. Any idea?