implement bots
This commit is contained in:
0
bots/management/__init__.py
Normal file
0
bots/management/__init__.py
Normal file
0
bots/management/commands/__init__.py
Normal file
0
bots/management/commands/__init__.py
Normal file
53
bots/management/commands/run_bots.py
Normal file
53
bots/management/commands/run_bots.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import logging
|
||||
import traceback
|
||||
|
||||
import sentry_sdk
|
||||
from django.core.cache import cache
|
||||
from django.core.management import BaseCommand
|
||||
from telegram import TelegramError
|
||||
from telegram.error import TimedOut
|
||||
|
||||
from bots.models import TelegramBot
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
dispatchers = []
|
||||
while True:
|
||||
try:
|
||||
if not dispatchers or cache.get('bots_reset'):
|
||||
logging.warning('Reloading dispatchers')
|
||||
dispatchers = []
|
||||
for bot in TelegramBot.objects.filter(active=True):
|
||||
try:
|
||||
dispatcher = bot.build_dispatcher()
|
||||
dispatcher.last_update_id = 0
|
||||
dispatchers.append(dispatcher)
|
||||
except TelegramError:
|
||||
pass
|
||||
cache.delete('bots_reset')
|
||||
|
||||
for dispatcher in dispatchers:
|
||||
try:
|
||||
updates = dispatcher.bot.get_updates(dispatcher.last_update_id)
|
||||
except TimedOut:
|
||||
continue
|
||||
except TelegramError as e:
|
||||
sentry_sdk.capture_exception(e)
|
||||
traceback.print_exc()
|
||||
updates = []
|
||||
|
||||
for update in updates:
|
||||
try:
|
||||
dispatcher.process_update(update)
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
except Exception as e:
|
||||
sentry_sdk.capture_exception(e)
|
||||
traceback.print_exc()
|
||||
dispatcher.last_update_id = update.update_id + 1
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
except Exception as e:
|
||||
sentry_sdk.capture_exception(e)
|
||||
traceback.print_exc()
|
Reference in New Issue
Block a user