- 1
- 0
Код:
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
access_token = '1'
chat_id = 1
desired_answer = "пук"
vk_session = vk_api.VkApi(token=access_token)
vk = vk_session.get_api()
longpoll = VkLongPoll(vk_session)
user_info = vk.users.get()
user_name = f"{user_info[0]['last_name']} {user_info[0]['first_name']}"
print(f"Бот запущен от имени пользователя: {user_name}")
print("Ожидание новых опросов...")
def process_poll_event(event):
try:
if 'attachments' in event.message and 'poll' in event.message['attachments']:
attachments = event.message['attachments']
for attachment in attachments:
if attachment['type'] == 'poll':
poll_id = attachment['poll']['id']
poll = vk.polls.getById(owner_id=event.peer_id, is_board=False, poll_id=poll_id)
options = poll['answers']
for option in options:
if option['text'] == desired_answer:
if option['id'] is not None:
vk.polls.addVote(owner_id=event.peer_id, poll_id=poll_id, answer_ids=[option['id']])
print("Проголосовал!")
break
except Exception as e:
print(f"Произошла ошибка при обработке опроса: {str(e)}")
while True:
try:
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.peer_id == chat_id:
process_poll_event(event)
except Exception as e:
print(f"Произошла ошибка при обработке опроса: {str(e)}")