def fb_put_post(channel, target_id, message, picture_name=None, media=None, link=None, link_description=None): """ Send post to a user feed or a page feed """ from solariat_bottle.tasks.exceptions import FacebookCommunicationException from solariat_bottle.tasks.twitter import get_file_from_media from solariat_bottle.utils.post import get_service_channel api = get_facebook_api(channel) service_channel = get_service_channel(channel) if service_channel: possible_targets = service_channel.all_fb_events + service_channel.facebook_pages matched_targets = [ target for target in possible_targets if target['id'] == target_id ] if matched_targets: token = matched_targets[0]['access_token'] api = get_facebook_api(channel, token=token) if media: filename, f = get_file_from_media(media) kwargs = dict() if message: kwargs['caption'] = message # if target_id: # kwargs['target_id'] = target_id photo = api.put_photo(f, album_path=target_id + "/photos", **kwargs) # attachment_data['link'] = picture_info['link'] if f: try: f.close() os.unlink(f.name) except IOError: pass return photo else: attachment_data = {} if link: # attachment_data['link'] = link attachment_data["picture"] = link if link_description: attachment_data['description'] = link_description try: result = api.put_wall_post(force_bytes(message, 'utf-8', 'replace'), profile_id=target_id, attachment=attachment_data) return result except Exception, ex: er = "Failure on posting to facebook. Channel: %s, Target: %s, Message: %s, Pic_N: %s, Pic_B: %s, Link: %s" logger.error( er % (channel, target_id, message, picture_name, media, link)) raise FacebookCommunicationException(ex.message)
def fb_put_comment_by_channel(channel, object_id, message): from solariat_bottle.settings import LOGGER from solariat_bottle.tasks.exceptions import FacebookCommunicationException try: return get_facebook_api(channel).put_comment( object_id, force_bytes(message, 'utf-8', 'replace')) except Exception, ex: LOGGER.error( "Failure posting comment to facebook. Exc: %s, Channel: %s, Object_id: %s, Message: %s" % (ex, channel, object_id, message)) raise FacebookCommunicationException(ex.message)
def fb_put_comment(channel, object_id, message): """put comment to some object""" # func = lambda: get_facebook_api(channel).put_comment(object_id, message.encode('utf-8', 'replace')) # return __try_execute_safely(func, 5, 3600) from solariat_bottle.settings import LOGGER from solariat_bottle.tasks.exceptions import FacebookCommunicationException from solariat_bottle.db.post.facebook import FacebookPost try: fb_post = FacebookPost.objects.get(_native_id=str(object_id)) except FacebookPost.DoesNotExist: LOGGER.warning( "No mapping post for native_id=%s was found. Defaulting to posting comment as user." % object_id) else: try: return fb_comment_by_page(fb_post, object_id, message) except (FacebookCommunicationException, facebook_driver.facebook.GraphAPIError) as exc: if '#1705' in str( exc ): # GraphAPIError: (#1705) There was an error posting to this wall LOGGER.info("Failed sending comment to post %s with error %s" % (object_id, str(exc))) if fb_post.is_second_level_reply: try: object_id = fb_post.wrapped_data['parent_id'] except KeyError: LOGGER.error("Can't find parent for comment %s" % fb_post) raise exc LOGGER.info( "Sending comment to parent %s of initial post %s %s" % (object_id, fb_post, fb_post.native_id)) return fb_comment_by_page(fb_post, object_id, message) raise exc try: return get_facebook_api(channel).put_comment( object_id, force_bytes(message, 'utf-8', 'replace')) except Exception, ex: LOGGER.error( "Failure posting comment to facebook. Exc: %s, Channel: %s, Object_id: %s, Message: %s" % (ex, channel, object_id, message)) raise FacebookCommunicationException(ex.message)
def fb_answer_pm(post, message): """ 1. Read conversation id from post 2. Get token for the page 3. Send POST request to <conversation_id>/messages """ channels = post.service_channels page_id = post.native_data['page_id'] #this is not safe, but according system logic this data should be in the system, if not - it will be good to #get exception there, it will means that data is corrupted channel = [ ch for ch in channels if (hasattr(ch, 'facebook_page_ids') and page_id in ch.facebook_page_ids) ][0] token = [page for page in channel.facebook_pages if page['id'] == page_id][0]['access_token'] driver = FacebookDriver(token, channel=channel) conversation = post.native_data['conversation_id'] path = str(conversation) + '/messages' driver.post(path, {"message": force_bytes(message, 'utf-8', 'replace')})
def fb_comment_by_page(post, respond_to, message): driver = get_page_based_api(post) return driver.request( respond_to + "/comments", post_args={"message": force_bytes(message, 'utf-8', 'replace')})
def serialize(obj): return force_bytes( json.dumps(_json_convert(obj), default=json_util.default))