Emit a dict with a key "msgs" whose value is a list of messages
    sent to the configured bot by one of the allowed usernames.

    .. code-block:: yaml

        beacons:
          telegram_bot_msg:
            - token: "<bot access token>"
            - accept_from:
              - "<valid username>"
            - interval: 10

def beacon(config):
    '''
    Emit a dict with a key "msgs" whose value is a list of messages
    sent to the configured bot by one of the allowed usernames.

    .. code-block:: yaml

        beacons:
          telegram_bot_msg:
            - token: "<bot access token>"
            - accept_from:
              - "<valid username>"
            - interval: 10

    '''

    _config = {}
    list(map(_config.update, config))

    log.debug('telegram_bot_msg beacon starting')
    ret = []
    output = {}
    output['msgs'] = []

    bot = telegram.Bot(_config['token'])
    updates = bot.get_updates(limit=100, timeout=0, network_delay=10)

    log.debug('Num updates: %d', len(updates))
    if not updates:
        log.debug('Telegram Bot beacon has no new messages')
        return ret

    latest_update_id = 0
    for update in updates:

        if update.message:
            message = update.message
        else:
            message = update.edited_message

        if update.update_id > latest_update_id:
            latest_update_id = update.update_id

        if message.chat.username in _config['accept_from']:
            output['msgs'].append(message.to_dict())

    # mark in the server that previous messages are processed
    bot.get_updates(offset=latest_update_id + 1)

    log.debug('Emitting %d messages.', len(output['msgs']))
    if output['msgs']:
        ret.append(output)
    return ret