'Posts a message on Facebook
https://developers.facebook.com/docs/graph-api
@ToDo: Log messages in msg_facebook'

def post_to_facebook(self, text='', channel_id=None):

    table = current.s3db.msg_facebook_channel
    if (not channel_id):
        query = (table.enabled == True)
    else:
        query = (table.channel_id == channel_id)
    c = current.db(query).select(table.app_id, table.app_secret, table.page_id, table.page_access_token, limitby=(0, 1)).first()
    import facebook
    try:
        app_access_token = facebook.get_app_access_token(c.app_id, c.app_secret)
    except:
        import sys
        message = sys.exc_info()[1]
        current.log.error(('S3MSG:    %s' % message))
        return
    graph = facebook.GraphAPI(app_access_token)
    page_id = c.page_id
    if page_id:
        graph = facebook.GraphAPI(c.page_access_token)
        graph.put_object(page_id, 'feed', message=text)
    else:
        graph.put_object(user_id, 'feed', message=text)
