I
I
iris_duty_20202020-12-26 08:57:18
Python
iris_duty_2020, 2020-12-26 08:57:18

How to attach a file from a folder to a VK message?

I want the bot to select a random file (photo) from the "citati" folder and attach it to the message. But, instead, when sending a command - it writes the following:

Command:

!m random quote

Python's response, or rather the output of the command:

Random Quote:
<_io.TextIOWrapper name='citati/cb5cee35d576980a28e7128d1be6c616.jpg' mode='r' encoding='cp1251'>


Here is the code:

async def cit(delay, peer_id, command):
    await asyncio.sleep(delay)
    if "!м рандом цитата" in command:
        try:   
        
            P_time = datetime.now().timestamp()
            requests = get(f'https://api.vk.com/method/messages.getConversations?count=20?filter=all?v=5.89&access_token=dba88f4f906b28feee2728472f4')
            DIR = 'citati/'
            F = open(os.path.join(DIR, random.choice(os.listdir(DIR))))
            msg_1 = f"""
            Рандомная Цитата: \n {F}
            """.replace('    ', '')

            messages.write_msg(peer_id, msg_1)

            #time.sleep(3)
        except Exception as Errpeng:
            print(f"Ошибка цитаты: {Errpeng}")


And yes, I put the wrong token on purpose. "os" is already set, as is "random".

I'm a beginner pythonist, so no criticism please)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-12-26
@iris_duty_2020

You are too beginner pythonist to solve such problems intuitively. You need to learn.

F = open(os.path.join(DIR, random.choice(os.listdir(DIR))))

In the variable F, you do not save a picture, not a file, and not its contents, but a special object that allows you to get the contents of a file from disk.
This object has a technical string representation, but it has nothing to do with the contents of the file.
And you substitute the technical name of the object intended for accessing the file in the text of the message to be sent. You get the corresponding result.
Even if you substituted in the text of the message not the technical name of the object for accessing the file, but the contents of the file itself, it would still not help you. The image file is a binary file. There are bytes that cannot be read by the eyes. You misuse the library, you don't understand the difference between a file, its contents, its format and representation.
In the comments to your question, you were told how to upload pictures through the contact API.
Apparently, the contents of the file need to be sent as an http post-request to a special service for storing it, and its identifier should be attached in a special way to the message.
You didn't specify what library you use to work with VK, you don't read the documentation, you try to naively guess how to send a kratinka based on some example from the network.
In view of the foregoing, I can tell you that it is too early for you to solve such problems. Learn a little basic stuff. Find documentation and examples, disassemble them.
Now, taking into account the wording of the problem, the answer how to send a picture will not help you.
Imagine that a "novice surgeon" will ask on the Internet why the patients from whom he cuts out appendicitis do not recover. In the provided photo, he does it in the wrong place and with a kitchen knife.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question