Answer the question
In order to leave comments, you need to log in
How to attach a photo to a VK bot message?
Good day! The other day I was puzzled by the idea of attaching a photo to a message. The photo itself is not stored on the computer, but is taken from the request URL.
def getSteamItemImageFromUrl(classID: str):
STEAM_URL = 'https://steamcommunity-a.akamaihd.net/economy/image/class/730/'
response = get(STEAM_URL + classID + '/330fx250f', stream=True).content
image = BytesIO(response).getvalue()
return b64encode(image)
def uploadImageToServer(image) -> str:
"""Загружает изображение на сервер, возвращает строку для attachment"""
upload = vk_api.VkUpload(vk)
photo = upload.photo_messages(image)
return f"photo_{photo['owner_id']}_{photo['id']}"
Answer the question
In order to leave comments, you need to log in
Save the image as a file and already in upload.photo_messages(image)
the image variable specify the path to the file.
import secrets
def getSteamItemImageFromUrl(classID: str):
STEAM_URL = 'https://steamcommunity-a.akamaihd.net/economy/image/class/730/'
response = get(STEAM_URL + classID + '/330fx250f', stream=True).content
filename = secrets.token_hex(16) + '.jpg'
with open(filename, 'wb') as file:
file.write(response)
return filename
def uploadImageToServer(image) -> str:
"""Загружает изображение на сервер, возвращает строку для attachment"""
upload = vk_api.VkUpload(vk)
photo = upload.photo_messages(image)
return f"photo_{photo['owner_id']}_{photo['id']}"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question