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