Added logger
This commit is contained in:
parent
878cba67b5
commit
a86536f928
|
@ -1,3 +1,4 @@
|
|||
config.py
|
||||
*.pyc
|
||||
*.json
|
||||
*.log
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
import vk_api
|
||||
import datetime
|
||||
import requests
|
||||
import logging
|
||||
from config import vk
|
||||
from bs4 import BeautifulSoup
|
||||
from vk_api.longpoll import VkLongPoll, VkEventType
|
||||
|
||||
bot_logger = logging.getLogger("dan63047bot")
|
||||
|
||||
class VkBot:
|
||||
|
||||
def __init__(self, peer_id):
|
||||
|
||||
print("\nСоздан объект бота!")
|
||||
bot_logger.info("Создан объект бота!")
|
||||
self._USER_ID = peer_id
|
||||
|
||||
self._COMMANDS = ["!image", "!my_id", "!h", "!user_id", "!group_id", "!help"]
|
||||
|
@ -31,12 +33,15 @@ class VkBot:
|
|||
return result
|
||||
|
||||
def get_info_user(self, id):
|
||||
logger = logging.getLogger("dan63047bot.get_info_user")
|
||||
try:
|
||||
user_info = vk.method('users.get', {'user_ids': id, 'fields': 'verified,last_seen,sex'})
|
||||
except vk_api.exceptions.ApiError as lol:
|
||||
answer = "Пользователь не найден<br>"+str(lol)
|
||||
logger.warning(answer)
|
||||
return answer
|
||||
|
||||
logger.info("Результат метода API users.get: "+str(user_info))
|
||||
if user_info[0]['is_closed']:
|
||||
is_closed = "Да"
|
||||
else:
|
||||
|
@ -73,12 +78,15 @@ class VkBot:
|
|||
return answer
|
||||
|
||||
def get_info_group(self, id):
|
||||
logger = logging.getLogger("dan63047bot.get_info_group")
|
||||
try:
|
||||
group_info = vk.method('groups.getById', {'group_id': id, 'fields': 'description,members_count'})
|
||||
except vk_api.exceptions.ApiError as lol:
|
||||
answer = "Группа не найдена<br>"+str(lol)
|
||||
logger.warning(answer)
|
||||
return answer
|
||||
|
||||
logger.info("Результат метода API groups.getById: "+str(group_info))
|
||||
if group_info[0]['description'] == "":
|
||||
description = "Отсутствует"
|
||||
else:
|
||||
|
|
18
start.py
18
start.py
|
@ -1,11 +1,17 @@
|
|||
import vk_api
|
||||
import time
|
||||
import requests
|
||||
import logging
|
||||
from config import vk
|
||||
from bs4 import BeautifulSoup
|
||||
from dan63047bot import VkBot
|
||||
from vk_api.longpoll import VkLongPoll, VkEventType
|
||||
|
||||
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)
|
||||
|
||||
def write_msg(peer_id, message, attachment=None):
|
||||
vk.method('messages.send', {'peer_id': peer_id,
|
||||
|
@ -14,16 +20,16 @@ def write_msg(peer_id, message, attachment=None):
|
|||
'attachment': attachment})
|
||||
|
||||
longpoll = VkLongPoll(vk) # Работа с сообщениями
|
||||
print("Server started")
|
||||
logging.info("Бот начал работу")
|
||||
for event in longpoll.listen():
|
||||
if event.type == VkEventType.MESSAGE_NEW:
|
||||
if event.to_me:
|
||||
|
||||
print('New message:')
|
||||
print(f'For me by: {event.peer_id}', end='')
|
||||
logging.info(f'Получено сообщение от id{event.peer_id}: {event.text}')
|
||||
|
||||
bot = VkBot(event.peer_id)
|
||||
if bot.new_message(event.text)['text']:
|
||||
write_msg(event.peer_id, bot.new_message(event.text)['text'], bot.new_message(event.text)['attachment'])
|
||||
bot_answer = bot.new_message(event.text)
|
||||
if bot_answer['text']:
|
||||
write_msg(event.peer_id, bot_answer['text'], bot_answer['attachment'])
|
||||
|
||||
print('Text: ', event.text)
|
||||
logging.info(f'Ответ бота: {bot_answer}')
|
Loading…
Reference in New Issue