from vk_api.longpoll import VkLongPoll, VkEventType
import vk_api
from vk_api.utils import get_random_id
import vk_api.exceptions
import threading
import requests
import time
import os
# токен страницы
token_str = "#"
# токен грппы
group_token = "#"
file = "group_id.txt"
f = open(file, "a")
def LongPoll(token):
try:
vk_session = vk_api.VkApi(
token=token)
longpoll = VkLongPoll(vk_session)
try:
for event in longpoll.listen():
if event.attachments != {}:
if "attach1_type" in event.attachments:
if event.attachments['attach1_type'] == "wall":
group_id = event.attachments['attach1'].split("_")[0]
t = open(file, "r")
for fs in t.readlines():
if fs == group_id:
continue
f.write(str(group_id)+"\n")
f.flush()
except requests.exceptions.ReadTimeout:
print("\n Переподключение к серверам ВК \n")
time.sleep(3)
threading.Thread(target=LongPoll, args=(token,)).start()
except vk_api.VkApiError as err:
print("Ошибка! Токен странцы:", token, "Ошибка:", err)
msg_post = "в"
def add_wall():
file = open("group_id.txt", "r").read().splitlines()
vk_session = vk_api.VkApi(
token=token_str)
api = vk_session.get_api()
at = upload_file()
for id in file:
try:
for w in at:
print(at[w])
attach = [d for d in at[w] if type(d) != dict]
text = [d for d in at[w] if type(d) == dict]
api.wall.post(owner_id=int(id), message=text[0]['text'], attachments=",".join(attach))
time.sleep(3)
except vk_api.exceptions.ApiError as err:
print(err)
continue
def upload_file():
vk_session = vk_api.VkApi(
token=token_str)
upload = vk_api.VkUpload(vk_session)
directory = {}
r = {}
for root, dirs, _ in os.walk("./walls"):
for dir in dirs:
directory.update({dir: []})
for _, _, files in os.walk(f"./walls/{dir}"):
for f in files:
directory[dir].append(f)
for dir in directory:
r.update({dir:[]})
for f in directory[dir]:
format = f.split(".")[1]
if format == "png" or format == "jpeg" or format == "jpg":
c = upload.photo( # Подставьте свои данные
f'./walls/{dir}/{f}',
album_id=277852923,
)
r[dir].append('photo{}_{}'.format(c[0]['owner_id'], c[0]['id']))
elif format == "mp3":
spl = f.replace(".mp3", '').split('-')
c = upload.audio( # Подставьте свои данные
f'./walls/{dir}/{f}',
title=spl[1],
artist=spl[0]
)
r[dir].append('audio{}_{}'.format(c['owner_id'], c['id']))
elif format == "txt":
r[dir].append({"text": open(f'./walls/{dir}/{f}', "r", encoding="utf-8").read()})
return r
def bot(token):
vk_session = vk_api.VkApi(
token=token)
longpoll = VkLongPoll(vk_session)
api = vk_session.get_api()
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
msg_spl = event.message.split(" ")
if len(msg_spl) <= 1:
continue
if msg_spl[1] == "12321":
if msg_spl[0] == "/start":
api.messages.send(user_id=event.user_id, message="Запускаем флуд", random_id=get_random_id())
add_wall()
api.messages.send(user_id=event.user_id, message="Флуд окончен", random_id=get_random_id())
if msg_spl[0] == "/add_text":
global msg_post
msg_post = " ".join(msg_spl).replace("/add_text", "").replace("12321", "")
def send_message(user_id, msg, api):
api.messages.send(user_id=user_id, message=msg, random_id=get_random_id())
t = threading.Thread(target=LongPoll, args=(token_str, ))
t.start()
bot(group_token)