Answer the question
In order to leave comments, you need to log in
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
)
Answer the question
In order to leave comments, you need to log in
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]
vk.method('photos.saveMessagesPhoto',
values = {'photo': b['photo'], 'server': b['server'], 'hash': b['hash']})[0]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question