A
A
Alexander Zakharov2021-07-31 09:06:00
Python
Alexander Zakharov, 2021-07-31 09:06:00

Vk_api TypeError: __call__() takes 1 positional argument but 3 were given, an error occurs while getting a name by id, what should I do?

Good afternoon, error right after running the code:

Traceback (most recent call last):
File "C:/Python/lessons/server.py", line 15, in
user = vk.method("users.get", {"user_ids ": 573881719})
TypeError: __call__() takes 1 positional argument but 3 were given

This part of the code is used to get the first name, last name, for example, for the profile function in the bot. That is, you need to take the first and last name from the ID. The question was asked to correct a bug.

The code:

import vk_api, vk
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.utils import get_random_id
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

authorize = vk_api.VkApi(token='ad6d1569fcd231ba7bb3af6d021144dc81a88d111111111111d5f0016a0ff5448c1ca33d649a2789b6975')
longpoll = VkBotLongPoll(authorize, group_id=206090038)
vk = authorize.get_api()


def write_message(sender, message):
    authorize.method('messages.send', {'chat_id': sender, 'message': message, "random_id": get_random_id()})


user = vk.method("users.get", {"user_ids": 573881719})
fullname = user[0]['first_name'] + ' ' + user[0]['last_name']


keyboard = VkKeyboard(one_time=True)
keyboard.add_button('Профиль', color=VkKeyboardColor.NEGATIVE)
keyboard.add_button('Игры', color=VkKeyboardColor.POSITIVE)
keyboard.add_button('Информация', color=VkKeyboardColor.NEGATIVE)
keyboard.add_line()
keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=183415444")

for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat and event.message.get('text') !="":
        reseived_message = event.message.get('text'.lower())
        sender = event.chat_id
        if 'ку' in str(reseived_message) or 'привет' in str(reseived_message):
            write_message(sender, "Добрый день!")
    if 'Клавиатура' in str(event):
        if event.from_chat:
            vk.messages.send(
                keyboard=keyboard.get_keyboard(),
                key=('0a9252b579c0fdd1070832e111bb34f16f38'),
                server=('https://lp.vk.com/wh206090038'),
                ts=('64'),
                random_id=get_random_id(),
                message=fullname + 'Открыта!',
                chat_id=event.chat_id
            )
    if 'Профиль' in str(event):
        if event.from_chat:
            vk.messages.send(
                keyboard=keyboard.get_keyboard(),
                key=('0a9252b579c0fdd123b34f16f38'),  # ВСТАВИТЬ ПАРАМЕТРЫ
                server=('https://lp.vk.com/wh206090038'),
                ts=('64'),
                random_id=get_random_id(),
                message='Открыт!',
                chat_id=event.chat_id
            )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RINAMI, 2021-07-31
@CleRleQ

Good afternoon, you are specifying a variable after using it, try like this:

import vk_api, vk
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.utils import get_random_id
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
vk = authorize.get_api()
authorize = vk_api.VkApi(token='ad6d1569fcd231ba7bb3af6d021144dc81a88d111111111111d5f0016a0ff5448c1ca33d649a2789b6975')
longpoll = VkBotLongPoll(authorize, group_id=206090038)

As I understand it, you need to get the user's first and last name, in which case you should do this:
def take_name(from_id)
     if from_id > 0:
        sender_info = getting_api.users.get(user_ids=from_id)[0]
        full_name = sender_info.get('first_name') + ' ' + sender_info['last_name']
        return full_name
        pass

PS better rename vk to something else, for example getting_api
As a result, you get getting_api = vk.get_api()

A
alternativshik, 2021-07-31
@alternativshik

import vk_api, vk
then the same vk is reassigned vk = authorize.get_api()
And then it is not clear in the end - library calls are necessary or this second vk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question