2020-04-04 16:07:39 +00:00
|
|
|
|
import vk_api
|
|
|
|
|
import time
|
2020-04-04 21:18:12 +00:00
|
|
|
|
import logging
|
2020-04-06 16:31:46 +00:00
|
|
|
|
from config import vk, group_id
|
2020-04-03 21:18:34 +00:00
|
|
|
|
from dan63047bot import VkBot
|
2020-04-05 16:24:24 +00:00
|
|
|
|
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
|
2020-04-03 21:18:34 +00:00
|
|
|
|
|
2020-04-04 21:18:12 +00:00
|
|
|
|
root_logger= logging.getLogger()
|
|
|
|
|
root_logger.setLevel(logging.INFO)
|
|
|
|
|
handler = logging.FileHandler('bot.log', 'w', 'utf-8')
|
|
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
|
|
|
|
root_logger.addHandler(handler)
|
2020-04-03 21:18:34 +00:00
|
|
|
|
|
2020-04-04 16:07:39 +00:00
|
|
|
|
def write_msg(peer_id, message, attachment=None):
|
|
|
|
|
vk.method('messages.send', {'peer_id': peer_id,
|
|
|
|
|
'message': message,
|
|
|
|
|
'random_id': time.time(),
|
|
|
|
|
'attachment': attachment})
|
|
|
|
|
|
2020-04-06 16:31:46 +00:00
|
|
|
|
longpoll = VkBotLongPoll(vk, group_id) # Работа с сообщениями
|
2020-04-04 21:18:12 +00:00
|
|
|
|
logging.info("Бот начал работу")
|
2020-04-03 21:18:34 +00:00
|
|
|
|
for event in longpoll.listen():
|
2020-04-07 16:00:49 +00:00
|
|
|
|
try:
|
|
|
|
|
if event.type == VkBotEventType.MESSAGE_NEW:
|
2020-04-04 16:07:39 +00:00
|
|
|
|
|
2020-04-07 16:00:49 +00:00
|
|
|
|
logging.info(f'Новое сообщение в чате id{event.message.peer_id}: {event.message.text}')
|
2020-04-04 16:07:39 +00:00
|
|
|
|
|
2020-04-07 16:00:49 +00:00
|
|
|
|
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}')
|
|
|
|
|
except Exception as kek:
|
|
|
|
|
logging.warning("Беды с ботом: "+str(kek))
|
|
|
|
|
continue
|