D
D
d00dguy2020-08-31 22:00:04
Python
d00dguy, 2020-08-31 22:00:04

VK Api - why can't I call the method?

Log in as a user using login, password, token, authenticator and captcha. I use the vk_api library with longpool and other libraries. I need to make it so that an image is attached to the message. I briefly read the documentation, googled and found the code that is listed here.

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import requests
import random

vk_session = vk_api.VkApi(login=login, password=password, captcha_handler=c_h, token=token)
longpoll = VkLongPoll(vk_session)
vk = vk_session.get_api()
while True:
    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW and event.from_chat:
             if event.text.lower() == '!бот, табличка':
                    a = vk.method('photos.getMessagesUploadServer')
                    b = requests.post(a['upload_url'], files={'photo': open('image_edited.png', 'rb')}).json()
                    c = vk.method('photos.saveMessagesPhoto',
                                  {'photo': b['photo'], 'server': b['server'], 'hash': b['hash']})[0]
                    d = 'photo{}_{}'.format(c['owner_id'], c['id'])

                    vk.messages.send(
                        chat_id=event.chat_id,
                        random_id=random_id(),
                        message='Картинка здесь',
                        attachment = d
                    )

And nothing works. I write a message and the program crashes with this error:
a = vk.method('photos.getMessagesUploadServer')
TypeError: __call__() takes 1 positional argument but 2 were given
Why is this happening? Indeed, even the documentation indicates that initially you need to call the photos.getMessagesUploadServer method. Maybe it's the authorization through the page, otherwise there is another way to upload an image?

I apologize in advance for a too stupid question, I'm just a complete newbie and have been working with vk_api recently

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BoBaH Gordeev, 2020-09-01
@d00dguy

Your mistake is that you copied only part of the code :)
Your vk variable is declared as:
vk = vk_session.get_api()
On the site where you copied the code from, it was assigned a different value.
Replace all lines like vk.method('amethod.bmethod') with
vk.amethod.bmethod
()
So:
a = vk.method('photos.getMessagesUploadServer')
b = requests.post(a['upload_url'], files={'photo': open('image_edited.png', 'rb')}).json()
c = vk.method('photos.saveMessagesPhoto', {'photo': b['photo'], ' server': b['server'], 'hash': b['hash']})[0]
On
a = vk.photos.getMessagesUploadServer()
b = requests.post(a['
c = vk.photos.saveMessagesPhoto(photo=b['photo'], server=b['server'], hash=b['hash'])[0]

T
Timur Pokrovsky, 2020-08-31
@Makaroshka007

vk.method('photos.saveMessagesPhoto',
      values = {'photo': b['photo'], 'server': b['server'], 'hash': b['hash']})[0]

So?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question