pythonbot/start.py

43 lines
1.3 KiB
Python
Raw Normal View History

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-08 09:58:32 +00:00
import requests
2020-04-10 16:06:15 +00:00
import asyncio
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-08 09:58:32 +00:00
longpoll = VkBotLongPoll(vk, group_id)
2020-04-11 14:40:25 +00:00
class MyVkLongPoll(VkBotLongPoll):
def listen(self):
while True:
try:
for event in self.check():
yield event
except Exception as e:
logging.warning("Беды с ВК: "+str(e))
continue
2020-04-08 09:58:32 +00:00
2020-04-10 16:06:15 +00:00
async def main():
2020-04-11 14:40:25 +00:00
for event in MyVkLongPoll.listen(longpoll):
2020-04-10 16:06:15 +00:00
try:
if event.type == VkBotEventType.MESSAGE_NEW:
logging.info(f'Новое сообщение в чате id{event.message.peer_id}: {event.message.text}')
bot = VkBot(event.message.peer_id, event.message.from_id)
2020-04-11 14:40:25 +00:00
await bot.new_message(event.message.text)
2020-04-10 16:06:15 +00:00
except Exception as kek:
logging.warning("Беды с ботом: "+str(kek))
continue
2020-04-08 09:58:32 +00:00
2020-04-10 16:06:15 +00:00
logging.info("Бот начал работу")
2020-04-04 16:07:39 +00:00
2020-04-10 16:06:15 +00:00
asyncio.run(main())
2020-04-04 16:07:39 +00:00
2020-04-08 09:58:32 +00:00