From e21ec5796bc445afd29b2a0b44b756f23a622d78 Mon Sep 17 00:00:00 2001 From: dan63047 Date: Sun, 5 Apr 2020 19:24:24 +0300 Subject: [PATCH] Now bot work in group chats --- dan63047bot.py | 5 +++-- start.py | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dan63047bot.py b/dan63047bot.py index 41d8262..6a13fa6 100644 --- a/dan63047bot.py +++ b/dan63047bot.py @@ -11,10 +11,11 @@ bot_logger = logging.getLogger("dan63047bot") class VkBot: - def __init__(self, peer_id): + def __init__(self, peer_id, user_id): bot_logger.info("Создан объект бота!") - self._USER_ID = peer_id + self._USER_ID = user_id + self._CHAT_ID = peer_id self._COMMANDS = ["!image", "!my_id", "!h", "!user_id", "!group_id", "!help", "!weather"] diff --git a/start.py b/start.py index 227d78d..5e52bda 100644 --- a/start.py +++ b/start.py @@ -5,7 +5,7 @@ import logging from config import vk from bs4 import BeautifulSoup from dan63047bot import VkBot -from vk_api.longpoll import VkLongPoll, VkEventType +from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType root_logger= logging.getLogger() root_logger.setLevel(logging.INFO) @@ -19,17 +19,16 @@ def write_msg(peer_id, message, attachment=None): 'random_id': time.time(), 'attachment': attachment}) -longpoll = VkLongPoll(vk) # Работа с сообщениями +longpoll = VkBotLongPoll(vk, 190322075) # Работа с сообщениями logging.info("Бот начал работу") for event in longpoll.listen(): - if event.type == VkEventType.MESSAGE_NEW: - if event.to_me: + if event.type == VkBotEventType.MESSAGE_NEW: - logging.info(f'Получено сообщение от id{event.peer_id}: {event.text}') + logging.info(f'Новое сообщение в чате id{event.message.peer_id}: {event.message.text}') - bot = VkBot(event.peer_id) - bot_answer = bot.new_message(event.text) - if bot_answer['text'] or bot_answer['attachment']: - write_msg(event.peer_id, bot_answer['text'], bot_answer['attachment']) - - logging.info(f'Ответ бота: {bot_answer}') \ No newline at end of file + bot = VkBot(event.message.peer_id, event.message.from_id) + bot_answer = bot.new_message(event.message.text) + if bot_answer['text'] or bot_answer['attachment']: + write_msg(event.message.peer_id, bot_answer['text'], bot_answer['attachment']) + + logging.info(f'Ответ бота: {bot_answer}') \ No newline at end of file