UNPKG

@memberjunction/actions-bizapps-social

Version:

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

329 lines 13 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FacebookRespondToCommentsAction = void 0; const global_1 = require("@memberjunction/global"); const facebook_base_action_1 = require("../facebook-base.action"); const core_1 = require("@memberjunction/core"); const axios_1 = __importDefault(require("axios")); const actions_1 = require("@memberjunction/actions"); /** * Responds to comments on Facebook posts or other comments. * Supports replying to top-level comments and nested replies. */ let FacebookRespondToCommentsAction = class FacebookRespondToCommentsAction extends facebook_base_action_1.FacebookBaseAction { /** * Get action description */ get Description() { return 'Responds to comments on Facebook posts, pages, or other comments with text replies or reactions'; } /** * Define the parameters for this action */ get Params() { return [ ...this.commonSocialParams, { Name: 'CommentID', Type: 'Input', Value: null, }, { Name: 'ResponseText', Type: 'Input', Value: null, }, { Name: 'AttachmentURL', Type: 'Input', Value: null, }, { Name: 'LikeComment', Type: 'Input', Value: false, }, { Name: 'HideComment', Type: 'Input', Value: false, }, { Name: 'DeleteComment', Type: 'Input', Value: false, }, { Name: 'PrivateReply', Type: 'Input', Value: false, }, { Name: 'PageID', Type: 'Input', Value: null, } ]; } /** * Execute the action */ async InternalRunAction(params) { const { Params, ContextUser } = params; try { // Validate required parameters const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID'); const commentId = this.getParamValue(Params, 'CommentID'); if (!companyIntegrationId) { return { Success: false, Message: 'CompanyIntegrationID is required', ResultCode: 'INVALID_TOKEN' }; } if (!commentId) { return { Success: false, Message: 'CommentID 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 responseText = this.getParamValue(Params, 'ResponseText'); const attachmentUrl = this.getParamValue(Params, 'AttachmentURL'); const likeComment = this.getParamValue(Params, 'LikeComment'); const hideComment = this.getParamValue(Params, 'HideComment'); const deleteComment = this.getParamValue(Params, 'DeleteComment'); const privateReply = this.getParamValue(Params, 'PrivateReply'); const pageId = this.getParamValue(Params, 'PageID'); // Validate that at least one action is specified if (!responseText && !attachmentUrl && !likeComment && !hideComment && !deleteComment) { return { Success: false, Message: 'At least one action (ResponseText, AttachmentURL, LikeComment, HideComment, or DeleteComment) is required', ResultCode: 'MISSING_ACTION' }; } // Get appropriate access token let accessToken = this.getAccessToken(); if (pageId) { // Use page access token for page actions accessToken = await this.getPageAccessToken(pageId); } (0, core_1.LogStatus)(`Processing comment ${commentId}...`); // Get comment details first const commentDetails = await this.getCommentDetails(commentId, accessToken); if (!commentDetails) { return { Success: false, Message: 'Comment not found or access denied', ResultCode: 'NOT_FOUND' }; } const results = { commentId, originalComment: { message: commentDetails.message, from: commentDetails.from, createdTime: commentDetails.created_time }, actions: [] }; // Handle delete action first (if specified) if (deleteComment) { try { await this.deleteCommentAction(commentId, accessToken); results.actions.push({ action: 'delete', success: true }); (0, core_1.LogStatus)(`Deleted comment ${commentId}`); // If deleted, no other actions can be performed return { Success: true, Message: 'Comment deleted successfully', ResultCode: 'SUCCESS', Params }; } catch (error) { (0, core_1.LogError)(`Failed to delete comment: ${error}`); results.actions.push({ action: 'delete', success: false, error: error instanceof Error ? error.message : 'Unknown error' }); } } // Handle hide action if (hideComment) { try { await this.hideCommentAction(commentId, accessToken, true); results.actions.push({ action: 'hide', success: true }); (0, core_1.LogStatus)(`Hidden comment ${commentId}`); } catch (error) { (0, core_1.LogError)(`Failed to hide comment: ${error}`); results.actions.push({ action: 'hide', success: false, error: error instanceof Error ? error.message : 'Unknown error' }); } } // Handle like action if (likeComment) { try { await this.likeCommentAction(commentId, accessToken); results.actions.push({ action: 'like', success: true }); (0, core_1.LogStatus)(`Liked comment ${commentId}`); } catch (error) { (0, core_1.LogError)(`Failed to like comment: ${error}`); results.actions.push({ action: 'like', success: false, error: error instanceof Error ? error.message : 'Unknown error' }); } } // Handle reply action if (responseText || attachmentUrl) { try { const replyResult = await this.replyToComment(commentId, responseText, attachmentUrl, privateReply, accessToken); results.actions.push({ action: privateReply ? 'private_reply' : 'reply', success: true, replyId: replyResult.id, message: responseText }); (0, core_1.LogStatus)(`Replied to comment ${commentId}`); } catch (error) { (0, core_1.LogError)(`Failed to reply to comment: ${error}`); results.actions.push({ action: privateReply ? 'private_reply' : 'reply', success: false, error: error instanceof Error ? error.message : 'Unknown error' }); } } // Check if any actions succeeded const successfulActions = results.actions.filter((a) => a.success); if (successfulActions.length === 0) { return { Success: false, Message: 'All requested actions failed', ResultCode: 'ERROR' }; } return { Success: true, Message: `Successfully completed ${successfulActions.length} of ${results.actions.length} actions`, ResultCode: 'SUCCESS', Params }; } catch (error) { (0, core_1.LogError)(`Failed to respond to Facebook comment: ${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' }; } } /** * Get comment details */ async getCommentDetails(commentId, accessToken) { try { const response = await axios_1.default.get(`${this.apiBaseUrl}/${commentId}`, { params: { access_token: accessToken, fields: 'id,message,created_time,from,like_count,comment_count,parent' } }); return response.data; } catch (error) { (0, core_1.LogError)(`Failed to get comment details: ${error}`); return null; } } /** * Reply to a comment */ async replyToComment(commentId, message, attachmentUrl, privateReply, accessToken) { const endpoint = privateReply ? `${this.apiBaseUrl}/${commentId}/private_replies` : `${this.apiBaseUrl}/${commentId}/comments`; const data = {}; if (message) { data.message = message; } if (attachmentUrl) { data.attachment_url = attachmentUrl; } const response = await axios_1.default.post(endpoint, data, { params: { access_token: accessToken } }); return response.data; } /** * Like a comment */ async likeCommentAction(commentId, accessToken) { await axios_1.default.post(`${this.apiBaseUrl}/${commentId}/likes`, {}, { params: { access_token: accessToken } }); } /** * Hide or unhide a comment */ async hideCommentAction(commentId, accessToken, hide) { await axios_1.default.post(`${this.apiBaseUrl}/${commentId}`, { is_hidden: hide }, { params: { access_token: accessToken } }); } /** * Delete a comment */ async deleteCommentAction(commentId, accessToken) { await axios_1.default.delete(`${this.apiBaseUrl}/${commentId}`, { params: { access_token: accessToken } }); } }; exports.FacebookRespondToCommentsAction = FacebookRespondToCommentsAction; exports.FacebookRespondToCommentsAction = FacebookRespondToCommentsAction = __decorate([ (0, global_1.RegisterClass)(actions_1.BaseAction, 'FacebookRespondToCommentsAction') ], FacebookRespondToCommentsAction); //# sourceMappingURL=respond-to-comments.action.js.map