Exemple #1
0
def process(messages):
    """
    This function processes the updates in the Updates URL at Telegram
    for finding commands, karma changes, config, etc
    """

    logger = logging.getLogger(__name__)

    # check if Log level has changed
    loglevel()

    # Main code for processing the karma updates
    date = 0
    lastupdateid = 0
    logger.info(msg="Initial message at %s" % date)
    texto = ""
    count = 0

    # Process each message available in URL and search for karma operators
    for message in messages:
        # Count messages in each batch
        count += 1

        # Call plugins to process message
        for i in plugins.getPlugins():
            plug = plugins.loadPlugin(i)
            logger.debug(msg="Processing plugin: %s" % i["name"])
            plug.run(message=message)

        msgdetail = getmsgdetail(message)

        # Update last message id to later clear it from the server
        if msgdetail["update_id"] > lastupdateid:
            lastupdateid = msgdetail["update_id"]

        # Write the line for debug
        messageline = "TEXT: %s : %s : %s" % (msgdetail["chat_name"], msgdetail["name"], msgdetail["text"])
        texto = msgdetail["text"]
        logger.debug(msg=messageline)

    logger.info(msg="Last processed message at: %s" % date)
    logger.debug(msg="Last processed update_id : %s" % lastupdateid)
    logger.debug(msg="Last processed text: %s" % texto)
    logger.info(msg="Number of messages in this batch: %s" % count)

    # clear updates (marking messages as read)
    clearupdates(offset=lastupdateid + 1)
Exemple #2
0
def main():
    """
    Main code for the bot
    """

    # Main code
    logger = logging.getLogger(__name__)

    # Set database name in config
    if options.database:
        plugin.config.setconfig(key='database', value=options.database)

    if not logger.handlers:
        conflogging()

    logger.info(msg="Started execution")

    if not plugin.config.config(key='sleep'):
        plugin.config.setconfig(key='sleep', value=10)

    # Check if we've the token required to access or exit
    if not plugin.config.config(key='token'):
        if options.token:
            token = options.token
            plugin.config.setconfig(key='token', value=token)
        else:
            msg = "Token required for operation, please check"
            msg += " https://core.telegram.org/bots"
            logger.critical(msg)
            sys.exit(1)

    # Check if we've URL defined on DB or on cli and store
    if not plugin.config.config(key='url'):
        if options.url:
            plugin.config.setconfig(key='url', value=options.url)

    # Check if we've owner defined in DB or on cli and store
    if not plugin.config.config(key='owner'):
        if options.owner:
            plugin.config.setconfig(key='owner', value=options.owner)

    # Initialize modules
    for i in plugins.getPlugins():
        plug = plugins.loadPlugin(i)
        logger.debug(msg="Processing plugin initialization: %s" % i["name"])
        plug.init()

    # Check operation mode and call process as required
    if options.daemon or plugin.config.config(key='daemon'):
        plugin.config.setconfig(key='daemon', value=True)
        logger.info(msg="Running in daemon mode")
        while plugin.config.config(key='daemon') == 'True':
            process(getupdates())
            sleep(int(plugin.config.config(key='sleep')))
    else:
        logger.info(msg="Running in one-shoot mode")
        process(getupdates())

    logger.info(msg="Stopped execution")
    logging.shutdown()
    sys.exit(0)