R
R
Renat Ataev2019-12-12 18:53:35
Python
Renat Ataev, 2019-12-12 18:53:35

What to do if it gives this error?

The code throws this error

Traceback (most recent call last):
File "bots.py", line 14, in
longpoll = VkLongPoll(vk_session)
File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site- packages\vk_api\longpoll.py", line 517, in __init__
self.update_longpoll_server()
File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\vk_api\longpoll. py", line 531, in update_longpoll_server
response = self.vk.method('messages.getLongPollServer', values)
File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site-packages \vk_api\vk_api.py", line 640, in method
raise error
vk_api.exceptions.ApiError: [15] Access denied: no access to call this method

The code itself:
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import random
import time
from datetime import datetime, timedelta

login, password = 'Моя почта', 'Мой пароль'
vk_session = vk_api.VkApi(login, password)
vk_session.auth()
vk = vk_session.get_api()

longpoll = VkLongPoll(vk_session)

for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW:
        print('Сообщение пришло в: ' + str(event.datetime + timedelta(hours=3)))
        print('Текст сообщения: ' + str(event.text))
        print(event.peer_id)

        response = event.text.lower()
        if event.from_chat or event.from_user:
            try:
                if response == '/status' or response == '/статус':

                    today = datetime.strftime(datetime.now(), "%d%m%Y")
                    tomorrow = datetime.strftime(datetime.now() + timedelta(days=1), "%d%m%Y")

                    vk.messages.send(
                        peer_id=event.peer_id,
                        random_id=random.randint(-2147483648, +2147483648),
                        message='Всего сообщений в данном диалоге : ' +
                                str(vk.messages.search(peer_id=event.peer_id, date=tomorrow, count=1)['count'] + 1) + '\nЗа сегодня: ' +
                                str( vk.messages.search(peer_id=event.peer_id, date=tomorrow, count=1)['count'] + 1 - vk.messages.search(peer_id=event.peer_id, date=today, count=1)['count'] + 1),
                    )
                elif response.split(' ')[0] == '/add':
                    print(vk.messages.getChat(chat_id=event.chat_id)['admin_id'])
                    if vk.messages.getChat(chat_id=event.chat_id)['admin_id'] == event.user_id:
                        try:
                            vk.messages.addChatUser(chat_id=event.chat_id, user_id=response.split(' ')[1])
                        except:
                            vk.messages.send(
                                peer_id=event.peer_id,
                                random_id=random.randint(-2147483648, +2147483648),
                                message='Ошибка при добавлении пользователя.'
                            )
                    else:
                        vk.messages.send(
                            peer_id=event.peer_id,
                            random_id=random.randint(-2147483648, +2147483648),
                            message='Вы не можете провести добавление участника таким способом, так как не являетесь создателем беседы.'
                        )

                elif response.split(' ')[0] == '/kick':
                    print(vk.messages.getChat(chat_id=event.chat_id)['admin_id'])
                    if vk.messages.getChat(chat_id=event.chat_id)['admin_id'] == event.user_id:
                        try:
                            vk.messages.removeChatUser(chat_id=event.chat_id, user_id=response.split(' ')[1])
                        except:
                            vk.messages.send(
                                peer_id=event.peer_id,
                                random_id=random.randint(-2147483648, +2147483648),
                                message='Ошибка при удалении пользователя.'
                            )
                    else:
                        vk.messages.send(
                            peer_id=event.peer_id,
                            random_id=random.randint(-2147483648, +2147483648),
                            message='Вы не можете провести удаление участника таким способом, так как не являетесь создателем беседы.'
                        )
                elif response == '/time':
                    vk.messages.send(
                        peer_id=event.peer_id,
                        random_id=random.randint(-2147483648, +2147483648),
                        message='Текущее время : ' + str(event.datetime + timedelta(hours=3))
                    )
            except:
                pass

            finally:
                time.sleep(3)

I wanted to take this code as a basis, but it gives an error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Serezha, 2019-12-12
@fanepka

There is no access to the methods of the messages section.
And it won't.
Write a bot for a group, not for a personal page.

R
Renat Ataev, 2019-12-12
@fanepka

Decided to continue, but now this error

File "bots.py", line 13, in
for event in longpoll.listen():
File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\vk_api\longpoll .py", line 621, in listen
for event in self.check():
File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\vk_api\longpoll.py ", line 560, in check
timeout=self.wait + 10
TypeError: can only concatenate str (not "int") to str

Top code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question