<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import os
import urllib2
import json
from datetime import datetime

CONFIG_FILE = 'unni.cfg'
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILE)

# Enable logging
LOG_DIR = config.get('log', 'dir')
LOG_FILE = config.get('log', 'name')
TELEGRAM_TOKEN = config.get('telegram', 'token')
SOURCE_NAME = config.get('source', 'name')
SOURCE_URL = config.get('source', 'url')

# Top level commands for the bot
START_CMD = 'start'
HELP_CMD = 'help'
TODAY_CMD = 'today'
FUTURE_CMD = 'future'

if not os.path.exists(LOG_DIR):
    os.makedirs(LOG_DIR)
logging.basicConfig(filename=LOG_DIR + "/" + LOG_FILE,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger()
logger.info('Logger initialised')


def getEventTime(time):
    time = time[0:10] + ' ' + time[11:19]
    time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
    return time


def isToday(time):
    return time.date() == datetime.today().date()


def isFuture(time):
    return time.date() > datetime.today().date()


def start_handle(bot, update):
    """Return a welcome message to the user"""
    msg = "Hello {user_name}! I am {bot_name} \n"
    msg += "Type /help for any info on how to interact with me"

    # Send the message
    bot.send_message(chat_id=update.message.chat_id,
                     text=msg.format(
                         user_name=update.message.from_user.first_name,
                         bot_name=bot.name))


def help_handle(bot, update):
    """Return a list of instruction on how to use the bot"""
    help = 'Hello! I am unni_bot :) \n\n'\
        'The available commands are: \n'\
        '- /today \n'\
        '- /future'

    bot.sendMessage(update.message.chat_id, text=help)


def today_handle(bot, update):
    """Return list of events happening today"""
    msg = 'The events of today are: \n'
    for event in events:
        name = event['name']
        starts_at = event['starts_at']
        starts_at_t = getEventTime(starts_at)
        if isToday(starts_at):
            msg += "*" + name + "* @ " + unicode(starts_at_t) + "\n"

    bot.sendMessage(update.message.chat_id, text=msg,
                    parse_mode=telegram.ParseMode.MARKDOWN)


def future_handle(bot, update):
    """Return list of future events"""
    response = urllib2.urlopen(SOURCE_URL)
    events = json.load(response)

    msg = 'The upcoming events are: \n'
    for event in events:
        name = event['name']
        starts_at = event['starts_at']
        starts_at_t = getEventTime(starts_at)
        if isFuture(starts_at):
            msg += "*" + name + "* @ " + unicode(starts_at_t) + "\n"

    bot.sendMessage(update.message.chat_id, text=msg,
                    parse_mode=telegram.ParseMode.MARKDOWN)


def error_handle(bot, update, error):
    """Handle errors"""
    logger.warn('Update "%s" caused error "%s"' % (update, error))


def main():
    logger.info('Starting Bot!')
    updater = Updater(TELEGRAM_TOKEN)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler(START_CMD, start_handle))
    dp.add_handler(CommandHandler(HELP_CMD, help_handle))
    dp.add_handler(CommandHandler(TODAY_CMD, today_handle))
    dp.add_handler(CommandHandler(FUTURE_CMD, future_handle))

    dp.add_error_handler(error_handle)<commit_msg>a first naive working unni bot<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import os
import urllib2
import json
from datetime import datetime

CONFIG_FILE = 'unni.cfg'
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILE)

# Enable logging
LOG_DIR = config.get('log', 'dir')
LOG_FILE = config.get('log', 'name')
TELEGRAM_TOKEN = config.get('telegram', 'token')
SOURCE_NAME = config.get('source', 'name')
SOURCE_URL = config.get('source', 'url')

# Top level commands for the bot
START_CMD = 'start'
HELP_CMD = 'help'
TODAY_CMD = 'today'
FUTURE_CMD = 'future'

if not os.path.exists(LOG_DIR):
    os.makedirs(LOG_DIR)
logging.basicConfig(filename=LOG_DIR + "/" + LOG_FILE,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger()
logger.info('Logger initialised')


def getEventTime(time):
    #time = time[0:10] + ' ' + time[11:19]
    time = time[0:23]
    time = datetime.strptime(time, '%Y-%m-%dT%H:%M:%S.%f')
    return time


def readableTime(time):
    time = time[0:10] + ' ' + time[11:16]
    return unicode(datetime.strptime(time, '%Y-%m-%d %H:%M'))[:-3]


def isToday(time):
    return time.date() == datetime.today().date()


def isFuture(time):
    return time.date() > datetime.today().date()


def start_handle(bot, update):
    """Return a welcome message to the user"""
    msg = "Hello {user_name}! I am {bot_name} \n"
    msg += "Type /help for any info on how to interact with me"

    # Send the message
    bot.send_message(chat_id=update.message.chat_id,
                     text=msg.format(
                         user_name=update.message.from_user.first_name,
                         bot_name=bot.name))


def help_handle(bot, update):
    """Return a list of instruction on how to use the bot"""
    help = 'Hello! I am unni_bot :) \n\n'\
        'The available commands are: \n'\
        '- /today \n'\
        '- /future'

    bot.sendMessage(update.message.chat_id, text=help)


def today_handle(bot, update):
    """Return list of events happening today"""
    msg = 'The events of today are: \n'
    for event in events:
        name = event['name']
        starts_at = event['starts_at']
        starts_at_t = getEventTime(starts_at)
        if isToday(starts_at_t):
            msg += "*" + name + "* @ " + readableTime(starts_at) + "\n"

    bot.sendMessage(update.message.chat_id, text=msg,
                    parse_mode=telegram.ParseMode.MARKDOWN)


def future_handle(bot, update):
    """Return list of future events"""
    response = urllib2.urlopen(SOURCE_URL)
    events = json.load(response)

    msg = 'The upcoming events are: \n'
    for event in events:
        name = event['name']
        starts_at = event['starts_at']
        starts_at_t = getEventTime(starts_at)
        if isFuture(starts_at_t):
            msg += "*" + name + "* @ " + readableTime(starts_at) + "\n"

    bot.sendMessage(update.message.chat_id, text=msg,
                    parse_mode=telegram.ParseMode.MARKDOWN)


def error_handle(bot, update, error):
    """Handle errors"""
    logger.warn('Update "%s" caused error "%s"' % (update, error))


def main():
    logger.info('Starting Bot!')
    updater = Updater(TELEGRAM_TOKEN)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler(START_CMD, start_handle))
    dp.add_handler(CommandHandler(HELP_CMD, help_handle))
    dp.add_handler(CommandHandler(TODAY_CMD, today_handle))
    dp.add_handler(CommandHandler(FUTURE_CMD, future_handle))

    dp.add_error_handler(error_handle)
{'fraction_non_alphanumeric': 0.1380121816168328, 'fraction_numerical': 0.00429125138427464, 'mean_word_length': 3.159470351180196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '577316', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2369, 'n_words': 548}