Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Проблема с longpoll #533

Open
@Format404

Description

import vk_api
import re
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.utils import get_random_id # Импортируем get_random_id

Ваш токен доступа

TOKEN = 'Токен'

ID модератора или группы (замените на фактический числовой ID)

MODERATOR_ID = id

Инициализация сессии VK

vk_session = vk_api.VkApi(token=TOKEN)
longpoll = VkLongPoll(vk_session)
vk = vk_session.get_api()

Ваш белый список ссылок

whitelist = [
'https://example.com',
'https://example.com',
'https://example.com',
'https://example.com'
]

url_pattern = re.compile(r'https?://[^\s]+')

def check_message(message, user_id):

urls = url_pattern.findall(message)
for url in urls:
 
 if url not in whitelist:
 
 notify_moderators(user_id)

def notify_moderators(user_id):
message = f'Я удалил сообщение от пользователя {user_id}, так как в его сообщении была подозрительная ссылка. Просьба модераторов проверить.'

try:
 
 vk.messages.send(peer_id=MODERATOR_ID, message=message, random_id=get_random_id())
except Exception as e:
 print(f"Ошибка при отправке сообщения модераторам: {e}")

def main():
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
user_id = event.from_id
text = event.text

 check_message(text, user_id)

if name == 'main':
main()

ошибка: File "C:\Maf\bot.py", line 34, in main
longpoll = vk_api.longpoll.VkLongPoll(vk_session)
^^^^^^^^^^^^^^^
AttributeError: module 'vk_api' has no attribute 'longpoll'

(Все библиотеки скачаны и переустановлены неоднократно..)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /