<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import os
import click
import telegram
import datetime

from time import sleep
from urllib2 import URLError

from beer import BeerBot


class InvalidCommand(Exception):
    pass


@click.command()
@click.argument('api_token', envvar='BEER_TELEGRAM_API_TOKEN')
def main(api_token):
    bot = telegram.Bot(api_token)
    update_id = 0

    while True:
        try:
            update_id = whatever(bot, update_id)

        except telegram.TelegramError as e:
            # These are network problems with Telegram.
            if e.message in ("Bad Gateway", "Timed out"):
                sleep(1)
            else:
                raise e

        except URLError as e:
            # These are network problems on our end.
            sleep(1)


def whatever(bot, update_id):
    for update in bot.getUpdates(offset=update_id, timeout=10):
        print(update.message)

        try:
            authenticate(update)
            response = get_responses_for(update.message)

            if response:
                bot.sendMessage(chat_id=update.message.chat_id, **response)

        except InvalidCommand as e:
            bot.sendMessage(chat_id=update.message.chat_id, text=e.message)

        update_id = update.update_id + 1

    return update_id


def authenticate(update):
    telegram_user = os.environ.get('BEER_TELEGRAM_USER')

    if telegram_user and not update.message.from_user.username == telegram_user:
        raise InvalidCommand('I do not know you. You shall not pass.')


def get_responses_for(message):
    if message.text == '/when':
        return when()

    if message.text.startswith('/add'):
        return add(message.text)<commit_msg>More sane function name.<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import os
import click
import telegram
import datetime

from time import sleep
from urllib2 import URLError

from beer import BeerBot


class InvalidCommand(Exception):
    pass


@click.command()
@click.argument('api_token', envvar='BEER_TELEGRAM_API_TOKEN')
def main(api_token):
    bot = telegram.Bot(api_token)
    update_id = 0

    while True:
        try:
            update_id = fetch_and_handle_messages(bot, update_id)

        except telegram.TelegramError as e:
            # These are network problems with Telegram.
            if e.message in ("Bad Gateway", "Timed out"):
                sleep(1)
            else:
                raise e

        except URLError as e:
            # These are network problems on our end.
            sleep(1)


def fetch_and_handle_messages(bot, update_id):
    for update in bot.getUpdates(offset=update_id, timeout=10):
        print(update.message)

        try:
            authenticate(update)
            response = get_responses_for(update.message)

            if response:
                bot.sendMessage(chat_id=update.message.chat_id, **response)

        except InvalidCommand as e:
            bot.sendMessage(chat_id=update.message.chat_id, text=e.message)

        update_id = update.update_id + 1

    return update_id


def authenticate(update):
    telegram_user = os.environ.get('BEER_TELEGRAM_USER')

    if telegram_user and not update.message.from_user.username == telegram_user:
        raise InvalidCommand('I do not know you. You shall not pass.')


def get_responses_for(message):
    if message.text == '/when':
        return when()

    if message.text.startswith('/add'):
        return add(message.text)
{'fraction_non_alphanumeric': 0.08672467022172327, 'fraction_numerical': 0.004490597810833568, 'mean_word_length': 2.5675675675675675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2047811', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1030, 'n_words': 278}