UNPKG

@memberjunction/actions-bizapps-social

Version:

Social Media Actions for MemberJunction - Twitter, LinkedIn, Facebook, Instagram, TikTok, YouTube, HootSuite, Buffer

224 lines 9.12 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FacebookCreatePostAction = void 0; const global_1 = require("@memberjunction/global"); const facebook_base_action_1 = require("../facebook-base.action"); const core_1 = require("@memberjunction/core"); const actions_1 = require("@memberjunction/actions"); /** * Creates a new post on a Facebook page. * Supports text, links, images, videos, and scheduling. */ let FacebookCreatePostAction = class FacebookCreatePostAction extends facebook_base_action_1.FacebookBaseAction { /** * Get action description */ get Description() { return 'Creates a new post on a Facebook page with optional media attachments and scheduling'; } /** * Define the parameters for this action */ get Params() { return [ ...this.commonSocialParams, { Name: 'PageID', Type: 'Input', Value: null, }, { Name: 'Content', Type: 'Input', Value: null, }, { Name: 'Link', Type: 'Input', Value: null, }, { Name: 'MediaFiles', Type: 'Input', Value: null, }, { Name: 'ScheduledTime', Type: 'Input', Value: null, }, { Name: 'Tags', Type: 'Input', Value: null, }, { Name: 'PlaceID', Type: 'Input', Value: null, }, { Name: 'Privacy', Type: 'Input', Value: null, }, { Name: 'Published', Type: 'Input', Value: true, } ]; } /** * Execute the action */ async InternalRunAction(params) { const { Params, ContextUser } = params; try { // Validate required parameters const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID'); const pageId = this.getParamValue(Params, 'PageID'); if (!companyIntegrationId) { return { Success: false, Message: 'CompanyIntegrationID is required', ResultCode: 'INVALID_TOKEN' }; } if (!pageId) { return { Success: false, Message: 'PageID is required', ResultCode: 'MISSING_REQUIRED_PARAM' }; } // Initialize OAuth if (!await this.initializeOAuth(companyIntegrationId)) { return { Success: false, Message: 'Failed to initialize Facebook OAuth connection', ResultCode: 'INVALID_TOKEN' }; } // Get parameters const content = this.getParamValue(Params, 'Content'); const link = this.getParamValue(Params, 'Link'); const mediaFiles = this.getParamValue(Params, 'MediaFiles'); const scheduledTime = this.getParamValue(Params, 'ScheduledTime'); const tags = this.getParamValue(Params, 'Tags'); const placeId = this.getParamValue(Params, 'PlaceID'); const privacy = this.getParamValue(Params, 'Privacy'); const published = this.getParamValue(Params, 'Published') !== false; // Validate that we have some content if (!content && !link && (!mediaFiles || mediaFiles.length === 0)) { return { Success: false, Message: 'At least one of Content, Link, or MediaFiles is required', ResultCode: 'MISSING_CONTENT' }; } // Build post data const postData = {}; if (content) { postData.message = content; } if (link) { postData.link = link; } if (placeId) { postData.place = placeId; } if (tags && tags.length > 0) { postData.tags = tags; } if (privacy) { postData.privacy = { value: privacy }; } postData.published = published; // Handle scheduling if (scheduledTime) { const scheduledDate = new Date(scheduledTime); const now = new Date(); const minScheduleTime = new Date(now.getTime() + 10 * 60 * 1000); // 10 minutes from now const maxScheduleTime = new Date(now.getTime() + 180 * 24 * 60 * 60 * 1000); // 6 months from now if (scheduledDate < minScheduleTime) { return { Success: false, Message: 'Scheduled time must be at least 10 minutes in the future', ResultCode: 'INVALID_SCHEDULE_TIME' }; } if (scheduledDate > maxScheduleTime) { return { Success: false, Message: 'Scheduled time cannot be more than 6 months in the future', ResultCode: 'INVALID_SCHEDULE_TIME' }; } postData.scheduled_publish_time = Math.floor(scheduledDate.getTime() / 1000); postData.published = false; // Must be unpublished when scheduling } // Handle media uploads if (mediaFiles && mediaFiles.length > 0) { (0, core_1.LogStatus)(`Uploading ${mediaFiles.length} media files to Facebook...`); const mediaIds = []; for (const file of mediaFiles) { try { const mediaId = await this.uploadMediaToPage(pageId, file); mediaIds.push(mediaId); (0, core_1.LogStatus)(`Uploaded media: ${file.filename}`); } catch (error) { (0, core_1.LogError)(`Failed to upload media ${file.filename}: ${error}`); return { Success: false, Message: `Failed to upload media: ${error instanceof Error ? error.message : 'Unknown error'}`, ResultCode: 'INVALID_MEDIA' }; } } // Attach media to post postData.attached_media = mediaIds.map(id => ({ media_fbid: id })); } // Create the post (0, core_1.LogStatus)('Creating Facebook post...'); const post = await this.createPost(pageId, postData); (0, core_1.LogStatus)(`Facebook post created successfully: ${post.id}`); // Return normalized post data const normalizedPost = this.normalizePost(post); // Update output parameters const outputParams = [...Params]; // TODO: Set output parameters based on result return { Success: true, Message: scheduledTime ? 'Post scheduled successfully' : 'Post created successfully', ResultCode: 'SUCCESS', Params: outputParams }; } catch (error) { (0, core_1.LogError)(`Failed to create Facebook post: ${error instanceof Error ? error.message : 'Unknown error'}`); if (this.isAuthError(error)) { return this.handleOAuthError(error); } return { Success: false, Message: error instanceof Error ? error.message : 'Unknown error occurred', ResultCode: 'ERROR' }; } } }; exports.FacebookCreatePostAction = FacebookCreatePostAction; exports.FacebookCreatePostAction = FacebookCreatePostAction = __decorate([ (0, global_1.RegisterClass)(actions_1.BaseAction, 'FacebookCreatePostAction') ], FacebookCreatePostAction); //# sourceMappingURL=create-post.action.js.map