def handle(self, *args, **options):
from django.conf import settings
if settings.DJANGO_TELEGRAMBOT.get('MODE', 'WEBHOOK') == 'WEBHOOK':
self.stderr.write("Webhook mode active in settings.py, change in POLLING if you want use polling update")
return
updater = self.get_updater(username=options.get('username'), token=options.get('token'))
if not updater:
self.stderr.write("Bot not found")
return
# Enable Logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger("telegrambot")
logger.setLevel(logging.INFO)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s'))
logger.addHandler(console)
bots_list = settings.DJANGO_TELEGRAMBOT.get('BOTS', [])
b = None
for bot_set in bots_list:
if bot_set.get('TOKEN', None) == updater.bot.token:
b = bot_set
break
if not b:
self.stderr.write("Cannot find bot settings")
return
allowed_updates = b.get('ALLOWED_UPDATES', None)
timeout = b.get('TIMEOUT', 10)
poll_interval = b.get('POLL_INTERVAL', 0.0)
clean = b.get('POLL_CLEAN', False)
bootstrap_retries = b.get('POLL_BOOTSTRAP_RETRIES', 0)
read_latency = b.get('POLL_READ_LATENCY', 2.)
self.stdout.write("Run polling...")
updater.start_polling(poll_interval=poll_interval,
timeout=timeout,
clean=clean,
bootstrap_retries=bootstrap_retries,
read_latency=read_latency,
allowed_updates=allowed_updates)
self.stdout.write("the bot is started and runs until we press Ctrl-C on the command line.")
updater.idle()
评论列表
文章目录