Beispiel #1
0
def main():
    global LAST_UPDATE_ID

    parser = argparse.ArgumentParser(description="Telegram bot for GranumSalis")
    parser.add_argument("--logfile", type=str, default='log', help="Path to log file")
    parser.add_argument("--dbfile", type=str, default='granumsalis.sqlite', help="Path to sqlite DB file")
    args = parser.parse_args()

    granumDB.bind('sqlite', args.dbfile, create_db=True)
    granumDB.generate_mapping(create_tables=True)

    with open('.admin_ids') as f:
        admin_ids = f.read().splitlines() 
    if admin_ids == None:
        admin_ids = list()

    # TODO: use it
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    telegram_token = open('.telegram_token').readline().strip()
    slack_token = open('.slack_token').readline().strip()
    bot = telegram.Bot(telegram_token)
    slackbot = SlackClient(slack_token)

    try:
        LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
    except IndexError:
        LAST_UPDATE_ID = None

    while True:
        try:
            run(bot, admin_ids, args.logfile, slackbot)
        except telegram.TelegramError as error:
            print "TelegramError", error
            time.sleep(1)
        except urllib2.URLError as error:
            print "URLError", error
            time.sleep(1)
        except:
            traceback.print_exc()
            try:
                bot.sendMessage(chat_id=CHAT_ID_ALARM, text=MESSAGE_ALARM)
            except:
                pass
            time.sleep(100) # 100 seconds
Beispiel #2
0
def main():

    parser = argparse.ArgumentParser(description="Collect chats info from log to DB")
    parser.add_argument("--logfile", type=str, default='log', help="Path to log file")
    parser.add_argument("--dbfile", type=str, default='granumsalis.sqlite.collected', help="Path to sqlite DB file")
    args = parser.parse_args()

    # Rename dbfile if exists
    os.system('[ -e {0} ] && mv -f {0} {1}'.format(args.dbfile, args.dbfile + '.old'))
    granumDB.bind('sqlite', args.dbfile, create_db=True)
    granumDB.generate_mapping(create_tables=True)

    with open(args.logfile) as log, db_session:
        for line in log:
            try:
                update = telegram.Update.de_json(json.loads(line.rstrip()))
                update_chat_db(update.message)
            except Exception as e:
                # TODO: process multilines correctly
                print str(e), type(e)
                print(line.rstrip())
                pass