Now bot work in group chats
This commit is contained in:
parent
b859745bf0
commit
e21ec5796b
|
@ -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"]
|
||||
|
||||
|
|
15
start.py
15
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)
|
||||
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.peer_id, bot_answer['text'], bot_answer['attachment'])
|
||||
write_msg(event.message.peer_id, bot_answer['text'], bot_answer['attachment'])
|
||||
|
||||
logging.info(f'Ответ бота: {bot_answer}')
|
Loading…
Reference in New Issue