'Function to tweet.
If a recipient is specified then we send via direct message if the recipient follows us.
- falls back to @mention (leaves less characters for the message).
Breaks long text to chunks if needed.
@ToDo: Option to Send via Tropo'

def send_tweet(self, text='', recipient=None):

    twitter_settings = self.get_twitter_api()
    if (not twitter_settings):
        return False
    import tweepy
    twitter_api = twitter_settings[0]
    twitter_account = twitter_settings[1]
    from_address = twitter_api.me().screen_name
    db = current.db
    s3db = current.s3db
    table = s3db.msg_twitter
    otable = s3db.msg_outbox
    def log_tweet(tweet, recipient, from_address):
        _id = table.insert(body=tweet, from_address=from_address)
        record = db((table.id == _id)).select(table.id, limitby=(0, 1)).first()
        s3db.update_super(table, record)
        message_id = record.message_id
        otable.insert(message_id=message_id, address=recipient, status=2, contact_method='TWITTER')
    if recipient:
        recipient = self._sanitise_twitter_account(recipient)
        try:
            can_dm = ((recipient == twitter_account) or (twitter_api.get_user(recipient).id in twitter_api.followers_ids(twitter_account)))
        except tweepy.TweepError:
            return False
        if can_dm:
            chunks = self._break_to_chunks(text)
            for c in chunks:
                try:
                    if twitter_api.send_direct_message(screen_name=recipient, text=c):
                        log_tweet(c, recipient, from_address)
                except tweepy.TweepError:
                    current.log.error('Unable    to    Tweet    DM')
        else:
            prefix = ('@%s    ' % recipient)
            chunks = self._break_to_chunks(text, (TWITTER_MAX_CHARS - len(prefix)))
            for c in chunks:
                try:
                    twitter_api.update_status(('%s    %s' % prefix), c)
                except tweepy.TweepError:
                    current.log.error('Unable    to    Tweet    @mention')
                else:
                    log_tweet(c, recipient, from_address)
    else:
        chunks = self._break_to_chunks(text)
        for c in chunks:
            try:
                twitter_api.update_status(c)
            except tweepy.TweepError:
                current.log.error('Unable    to    Tweet')
            else:
                log_tweet(c, recipient, from_address)
    return True
