Esempio n. 1
0
def telegram_poller(data, check_id):
    if 'token' not in data:
        util.die('checks.telegram_poller: missing token')

    token = data['token']
    update_id = int(extract(data, 'update_id', 0))

    from telegram import Bot
    from common.sql import getdb
    import urlparse
    import urllib

    db = getdb()

    bot = Bot(token)
    if not bot: return {'status': 'fail'}
    updates = bot.getUpdates(offset=update_id)
    for update in updates:
        if int(update.update_id) >= update_id:
            update_id = int(update.update_id)
        chat_id = update.message.chat_id
        if '/' in update.message.text and ' ' in update.message.text:
            cmd, msg_data = update.message.text.split(' ', 1)
            if cmd == '/start':
                contact = db.getOne('contacts', '*', ('id = %s', msg_data))
                if contact:
                    data_decoded = urlparse.parse_qs(contact.data)
                    data_decoded['token'] = data_decoded['token'][0]
                    data_decoded['chat_id'] = chat_id
                    data_encoded = urllib.urlencode(data_decoded)
                    db.update('contacts', dict(data=data_encoded),
                              ('id = %s', [contact.id]))
                    db.commit()

    check = db.getOne('checks', '*', ('id = %s', [check_id]))
    check_data = urlparse.parse_qs(check.data)
    check_data = urllib.urlencode(
        dict(token=check_data['token'][0], update_id=update_id + 1))
    db.update('checks', dict(data=check_data), ('id = %s', [check_id]))
    db.commit()
    return {'status': 'success'}
Esempio n. 2
0
def telegram_poller(data, check_id):
    if 'token' not in data:
        util.die('checks.telegram_poller: missing token')

    token = data['token']
    update_id = int(extract(data, 'update_id', 0))

    from telegram import Bot
    from common.sql import getdb
    import urlparse
    import urllib

    db = getdb()

    bot = Bot(token)
    if not bot: return { 'status' : 'fail' }
    updates = bot.getUpdates(offset = update_id)
    for update in updates:
        if int(update.update_id) >= update_id: update_id = int(update.update_id)
        chat_id =  update.message.chat_id
        if '/' in update.message.text and ' ' in update.message.text:
            cmd, msg_data = update.message.text.split(' ', 1)
            if cmd == '/start':
                contact = db.getOne('contacts', '*', ('id = %s', msg_data))
                if contact:
                    data_decoded = urlparse.parse_qs(contact.data)
                    data_decoded['token'] = data_decoded['token'][0]
                    data_decoded['chat_id'] = chat_id
                    data_encoded = urllib.urlencode(data_decoded)
                    db.update('contacts', dict(data = data_encoded), ('id = %s', [ contact.id ]))
                    db.commit()

    check = db.getOne('checks', '*', ('id = %s', [ check_id] ))
    check_data = urlparse.parse_qs(check.data)
    check_data = urllib.urlencode(dict( token = check_data['token'][0], update_id = update_id + 1))
    db.update('checks', dict( data = check_data ), ('id = %s', [ check_id ]))
    db.commit()
    return {'status': 'success'}
Esempio n. 3
0
def index():
    db = getdb()
    list_checks = db.getAll("checks", "*", ["public = 1"], limit=[0, 10])
    return render_template("index.html", checks=list_checks if list_checks else [])
Esempio n. 4
0
def index():
    db = getdb()
    list_checks = db.getAll('checks', '*', ['public = 1'], limit=[0, 10])
    return render_template('index.html',
                           checks=list_checks if list_checks else [])