more async and finaliy +stability
This commit is contained in:
parent
6e06f80ec9
commit
715219448a
|
@ -1,5 +1,6 @@
|
|||
import vk_api
|
||||
import datetime
|
||||
import time
|
||||
import requests
|
||||
import logging
|
||||
import pyowm
|
||||
|
@ -110,7 +111,7 @@ class VkBot:
|
|||
answer = "Такой статьи не существует"
|
||||
return answer
|
||||
|
||||
def exchange_rates(self):
|
||||
async def exchange_rates(self):
|
||||
try:
|
||||
rates_USD = json.loads(requests.get("https://www.nbrb.by/api/exrates/rates/145?periodicity=0", timeout=10).text)
|
||||
rates_EUR = json.loads(requests.get("https://www.nbrb.by/api/exrates/rates/292?periodicity=0", timeout=10).text)
|
||||
|
@ -156,6 +157,6 @@ class VkBot:
|
|||
respond['text'] = "Отсуствует аргумент"
|
||||
|
||||
elif message[0] == self._COMMANDS[8]:
|
||||
respond['text'] = self.exchange_rates()
|
||||
|
||||
return respond
|
||||
respond['text'] = await self.exchange_rates()
|
||||
message = vk.method('messages.send', {'peer_id': self._CHAT_ID, 'message': respond['text'], 'random_id': time.time(), 'attachment': respond['attachment']})
|
||||
bot_logger.info(f'Ответ бота: {respond}')
|
||||
|
|
24
start.py
24
start.py
|
@ -15,25 +15,23 @@ root_logger.addHandler(handler)
|
|||
|
||||
longpoll = VkBotLongPoll(vk, group_id)
|
||||
|
||||
def listening():
|
||||
while True:
|
||||
try:
|
||||
event = longpoll.listen()
|
||||
return event
|
||||
except Exception as mda:
|
||||
logging.warning("Беды с ВК: "+str(mda))
|
||||
continue
|
||||
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
|
||||
|
||||
async def main():
|
||||
for event in listening():
|
||||
for event in MyVkLongPoll.listen(longpoll):
|
||||
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)
|
||||
result = await bot.new_message(event.message.text)
|
||||
message = vk.method('messages.send', {'peer_id': event.message.peer_id, 'message': result['text'], 'random_id': time.time(), 'attachment': result['attachment']})
|
||||
logging.info(f'Ответ бота: {result}')
|
||||
logging.info(f'Отправлено методом messages.send: {message}')
|
||||
await bot.new_message(event.message.text)
|
||||
except Exception as kek:
|
||||
logging.warning("Беды с ботом: "+str(kek))
|
||||
continue
|
||||
|
|
Loading…
Reference in New Issue