@@ -0,0 +1,2475 @@
+# -*- coding: utf-8 -*-%0A'''%0ABeacon to emit Twilio text messages%0A'''%0A%0A# Import Python libs%0Afrom __future__ import absolute_import%0Afrom datetime import datetime%0Aimport logging%0A%0A# Import 3rd Party libs%0Atry:%0A    from twilio.rest import TwilioRestClient%0A    HAS_TWILIO = True%0Aexcept ImportError:%0A    HAS_TWILIO = False%0A%0Alog = logging.getLogger(__name__)%0A%0A__virtualname__ = 'twilio_txt_msg'%0A%0A%0Adef __virtual__():%0A    if HAS_TWILIO:%0A        return __virtualname__%0A    else:%0A        return False%0A%0Adef beacon(config):%0A    '''%0A    Emit a dict name %22texts%22 whose value is a list%0A    of texts.%0A%0A    code_block:: yaml%0A%0A        beacons:%0A          twilio_txt_msg:%0A            account_sid: %22%3Caccount sid%3E%22%0A            auth_token: %22%3Cauth token%3E%22%0A            twilio_number: %22+15555555555%22%0A            poll_interval: 10%0A%0A    poll_interval defaults to 10 seconds%0A    '''%0A    log.trace('twilio_txt_msg beacon starting')%0A    ret = %5B%5D%0A    if not all(%5Bconfig%5B'account_sid'%5D, config%5B'auth_token'%5D, config%5B'twilio_number'%5D%5D):%0A        return ret%0A    output = %7B%7D%0A    poll_interval = config.get('poll_interval')%0A    if not poll_interval:%0A        # Let's default to polling every 10 secons%0A        poll_interval = 10%0A    now = datetime.now()%0A    if 'twilio_txt_msg' in __context__:%0A        timedelta = now - __context__%5B'twilio_txt_msg'%5D%0A        if timedelta.seconds %3C poll_interval:%0A            log.trace('Twilio beacon poll interval not met.')%0A            log.trace('Twilio polling in %7B0%7D'.format(poll_interval - timedelta.seconds))%0A            return ret%0A%0A    output%5B'texts'%5D = %5B%5D%0A    client = TwilioRestClient(config%5B'account_sid'%5D, config%5B'auth_token'%5D)%0A    messages = client.messages.list(to=config%5B'twilio_number'%5D)%0A    log.trace('Num messages: %7B0%7D'.format(len(messages)))%0A    if len(messages) %3C 1:%0A        log.trace('Twilio beacon has no texts')%0A        __context__%5B'twilio_txt_msg'%5D = now%0A        return ret%0A%0A    for message in messages:%0A        item = %7B%7D%0A        item%5B'id'%5D = str(message.sid)%0A        item%5B'body'%5D = str(message.body)%0A        item%5B'from'%5D = str(message.from_)%0A        item%5B'sent'%5D = str(message.date_sent)%0A        item%5B'images'%5D = %5B%5D%0A%0A        if int(message.num_media):%0A            media = client.media(message.sid).list()%0A            if len(media):%0A                for pic in media:%0A                    item%5B'images'%5D.append(str(pic.uri))%0A        output%5B'texts'%5D.append(item)%0A        message.delete()%0A    __context__%5B'twilio_txt_msg'%5D = now%0A    ret.append(output)%0A    return ret%0A

Add Twilio text message beacon