<commit_before>import os
import json
import sys
import logging
from time import sleep

import bugsnag
import tornado.web
from bugsnag.tornado import BugsnagRequestHandler
from urllib.parse import quote_plus

import telegram
from telegram.error import NetworkError, Unauthorized, RetryAfter

from leonard import Leonard
from libs import shrt

WEBHOOK_HOSTNAME = os.environ.get('WEBHOOK_HOSTNAME', 'https://leonardbot.herokuapp.com')


class WebhookHandler(BugsnagRequestHandler):
    def __init__(self, application, request, **kwargs):
        super().__init__(application, request, **kwargs)

    def initialize(self, **kwargs):
        self.bot = kwargs['bot']

    def post(self):
        if self.request.headers.get('Content-Length') in (None, 0):
            # Nothing to do
            return

        # Read the request body.
        body = self.request.body
        if not body:
            raise tornado.web.HTTPError(400, 'A valid JSON document is required.')

        try:
            content = json.loads(body.decode('utf-8'))

        except (ValueError, UnicodeDecodeError):
            raise tornado.web.HTTPError(753, 'Could not decode the request body. The '
                                             'JSON was incorrect or not encoded as '
                                             'UTF-8.')

        update = telegram.Update.de_json(content, self.bot.telegram)
        self.bot.process_update(update)

        self.write('ok')


class AllUsersHandler(BugsnagRequestHandler):
    def __init__(self, application, request, **kwargs):
        super().__init__(application, request, **kwargs)

    def initialize(self, **kwargs):
        self.bot = kwargs['bot']

    def get(self):
        result = []
        for key in self.bot.redis.scan_iter(match='user:*:registered'):
            result.append(int(key.decode('utf-8').split(':')[1]))

        self.write(str(result))


debug = False
if 'BOT_DEBUG' in os.environ and os.environ['BOT_DEBUG'] == '1':
    debug = True

print('Starting bot')

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('leonard')
logger.setLevel(logging.INFO)

print('Creating bot')
telegram_client = telegram.Bot(os.environ['BOT_TOKEN'])
bot = Leonard(telegram_client, debug)

print('Collecting plugins')
bot.collect_plugins()

print('Setting routes')
bot.tornado.add_handlers(r'.*', [
    (r'/webhook/{}'.format(quote_plus(os.environ['BOT_TOKEN'])), WebhookHandler, {'bot': bot})
])
bot.tornado.add_handlers(r'.*', [
    (r'/l/(.*)', shrt.GetLinkHandler)
])
bot.tornado.add_handlers(r'.*', [<commit_msg>Change all users handler format<commit_after>import os
import json
import sys
import logging
from time import sleep

import bugsnag
import tornado.web
from bugsnag.tornado import BugsnagRequestHandler
from urllib.parse import quote_plus

import telegram
from telegram.error import NetworkError, Unauthorized, RetryAfter

from leonard import Leonard
from libs import shrt

WEBHOOK_HOSTNAME = os.environ.get('WEBHOOK_HOSTNAME', 'https://leonardbot.herokuapp.com')


class WebhookHandler(BugsnagRequestHandler):
    def __init__(self, application, request, **kwargs):
        super().__init__(application, request, **kwargs)

    def initialize(self, **kwargs):
        self.bot = kwargs['bot']

    def post(self):
        if self.request.headers.get('Content-Length') in (None, 0):
            # Nothing to do
            return

        # Read the request body.
        body = self.request.body
        if not body:
            raise tornado.web.HTTPError(400, 'A valid JSON document is required.')

        try:
            content = json.loads(body.decode('utf-8'))

        except (ValueError, UnicodeDecodeError):
            raise tornado.web.HTTPError(753, 'Could not decode the request body. The '
                                             'JSON was incorrect or not encoded as '
                                             'UTF-8.')

        update = telegram.Update.de_json(content, self.bot.telegram)
        self.bot.process_update(update)

        self.write('ok')


class AllUsersHandler(BugsnagRequestHandler):
    def __init__(self, application, request, **kwargs):
        super().__init__(application, request, **kwargs)

    def initialize(self, **kwargs):
        self.bot = kwargs['bot']

    def get(self):
        result = []
        for key in self.bot.redis.scan_iter(match='user:*:registered'):
            result.append(int(key.decode('utf-8').split(':')[1]))

        self.write({'users': result})


debug = False
if 'BOT_DEBUG' in os.environ and os.environ['BOT_DEBUG'] == '1':
    debug = True

print('Starting bot')

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('leonard')
logger.setLevel(logging.INFO)

print('Creating bot')
telegram_client = telegram.Bot(os.environ['BOT_TOKEN'])
bot = Leonard(telegram_client, debug)

print('Collecting plugins')
bot.collect_plugins()

print('Setting routes')
bot.tornado.add_handlers(r'.*', [
    (r'/webhook/{}'.format(quote_plus(os.environ['BOT_TOKEN'])), WebhookHandler, {'bot': bot})
])
bot.tornado.add_handlers(r'.*', [
    (r'/l/(.*)', shrt.GetLinkHandler)
])
bot.tornado.add_handlers(r'.*', [
{'fraction_non_alphanumeric': 0.1347693480747236, 'fraction_numerical': 0.004574914220358368, 'mean_word_length': 3.1380126182965298, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4160937', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1604, 'n_words': 363}