@memberjunction/actions-bizapps-social
Version:
Social Media Actions for MemberJunction - Twitter, LinkedIn, Facebook, Instagram, TikTok, YouTube, HootSuite, Buffer
196 lines • 7.86 kB
JavaScript
;
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.BufferCreatePostAction = void 0;
const global_1 = require("@memberjunction/global");
const buffer_base_action_1 = require("../buffer-base.action");
const actions_1 = require("@memberjunction/actions");
/**
* Action to create a new post in Buffer (scheduled or immediate)
*/
let BufferCreatePostAction = class BufferCreatePostAction extends buffer_base_action_1.BufferBaseAction {
/**
* Create a Buffer post
*/
async InternalRunAction(params) {
const { Params, ContextUser } = params;
try {
// Get parameters
const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID');
const profileIds = this.getParamValue(Params, 'ProfileIDs');
const content = this.getParamValue(Params, 'Content');
const mediaFiles = this.getParamValue(Params, 'MediaFiles');
const scheduledTime = this.getParamValue(Params, 'ScheduledTime');
const postNow = this.getParamValue(Params, 'PostNow');
const addToTop = this.getParamValue(Params, 'AddToTop');
const shortenLinks = this.getParamValue(Params, 'ShortenLinks');
const mediaLink = this.getParamValue(Params, 'MediaLink');
const mediaDescription = this.getParamValue(Params, 'MediaDescription');
// Validate required parameters
if (!companyIntegrationId) {
throw new Error('CompanyIntegrationID is required');
}
if (!profileIds || !Array.isArray(profileIds) || profileIds.length === 0) {
throw new Error('ProfileIDs array is required with at least one profile');
}
if (!content && !mediaFiles && !mediaLink) {
throw new Error('Content, MediaFiles, or MediaLink is required');
}
// Initialize OAuth
if (!await this.initializeOAuth(companyIntegrationId)) {
return {
Success: false,
ResultCode: 'INVALID_TOKEN',
Message: 'Failed to initialize Buffer OAuth connection',
Params
};
}
// Prepare media array
const media = [];
// Upload media files if provided
if (mediaFiles && Array.isArray(mediaFiles) && mediaFiles.length > 0) {
const uploadedUrls = await this.uploadMedia(mediaFiles);
uploadedUrls.forEach(url => {
media.push({ picture: url });
});
}
// Add media link if provided
if (mediaLink) {
const mediaItem = { link: mediaLink };
if (mediaDescription) {
mediaItem.description = mediaDescription;
}
media.push(mediaItem);
}
// Create the update
const result = await this.createUpdate(profileIds, content || '', media.length > 0 ? media : undefined, scheduledTime ? new Date(scheduledTime) : undefined, {
now: postNow === true,
top: addToTop === true,
shorten: shortenLinks !== false // Default to true
});
// Format the response
const createdPosts = result.updates || [];
const formattedPosts = createdPosts.map((update) => ({
id: update.id,
profileId: update.profile_id,
service: update.profile_service,
status: update.status,
scheduledAt: update.due_at ? new Date(update.due_at * 1000) : null,
text: update.text,
media: update.media,
createdAt: update.created_at ? new Date(update.created_at * 1000) : new Date()
}));
// Create summary
const summary = {
totalCreated: formattedPosts.length,
profilesPosted: profileIds,
scheduled: !postNow,
scheduledTime: scheduledTime || null,
hasMedia: media.length > 0
};
// Update output parameters
const outputParams = [...Params];
const postsParam = outputParams.find(p => p.Name === 'CreatedPosts');
if (postsParam)
postsParam.Value = formattedPosts;
const summaryParam = outputParams.find(p => p.Name === 'Summary');
if (summaryParam)
summaryParam.Value = summary;
return {
Success: true,
ResultCode: 'SUCCESS',
Message: `Successfully created ${formattedPosts.length} Buffer post(s)`,
Params: outputParams
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
const resultCode = this.mapBufferError(error);
return {
Success: false,
ResultCode: resultCode,
Message: `Failed to create Buffer post: ${errorMessage}`,
Params
};
}
}
/**
* Define the parameters this action expects
*/
get Params() {
return [
...this.commonSocialParams,
{
Name: 'ProfileIDs',
Type: 'Input',
Value: null
},
{
Name: 'Content',
Type: 'Input',
Value: null
},
{
Name: 'MediaFiles',
Type: 'Input',
Value: null
},
{
Name: 'MediaLink',
Type: 'Input',
Value: null
},
{
Name: 'MediaDescription',
Type: 'Input',
Value: null
},
{
Name: 'ScheduledTime',
Type: 'Input',
Value: null
},
{
Name: 'PostNow',
Type: 'Input',
Value: false
},
{
Name: 'AddToTop',
Type: 'Input',
Value: false
},
{
Name: 'ShortenLinks',
Type: 'Input',
Value: true
},
{
Name: 'CreatedPosts',
Type: 'Output',
Value: null
},
{
Name: 'Summary',
Type: 'Output',
Value: null
}
];
}
/**
* Metadata about this action
*/
get Description() {
return 'Creates a new post in Buffer that can be scheduled or posted immediately to one or more social media profiles';
}
};
exports.BufferCreatePostAction = BufferCreatePostAction;
exports.BufferCreatePostAction = BufferCreatePostAction = __decorate([
(0, global_1.RegisterClass)(actions_1.BaseAction, 'BufferCreatePostAction')
], BufferCreatePostAction);
//# sourceMappingURL=create-post.action.js.map