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