UNPKG

@anjir/app-novinhub

Version:

نوین هاب - پلتفرم کامل مدیریت شبکه‌های اجتماعی، دایرکت هوشمند، و خودکارسازی پیام‌رسانی

257 lines 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.novinhubCreatePostEnhancedAction = void 0; const apps_framework_1 = require("@anjir/apps-framework"); const apps_common_1 = require("@anjir/apps-common"); const index_1 = require("../../index"); const common_1 = require("../common"); exports.novinhubCreatePostEnhancedAction = (0, apps_framework_1.createAction)({ auth: index_1.novinhubAuth, name: 'create_post_enhanced', displayName: 'ایجاد پست پیشرفته', description: 'ایجاد پست جدید درحساب‌های شبکه‌های اجتماعی', props: { // Account Selection accounts: apps_framework_1.Property.MultiSelectDropdown({ displayName: 'اکانت‌های انتخابی', description: 'اکانت‌هایی که پست در آن‌ها منتشر شود', required: true, refreshers: [], options: async ({ auth }) => { if (!auth) { return { disabled: true, placeholder: 'ابتدا احراز هویت کنید', options: [], }; } try { const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth.access_token}`, }; const response = await apps_common_1.httpClient.sendRequest({ method: apps_common_1.HttpMethod.GET, url: (0, common_1.buildApiUrl)('/account'), headers: headers, }); if (response.status === 200 && Array.isArray(response.body)) { const options = response.body.map((account) => ({ label: `${account.name} (${(0, common_1.getSocialNetworkDisplayName)(account.type)})`, value: account.id, })); return { disabled: false, options: options, }; } return { disabled: true, placeholder: 'خطا در بارگذاری اکانت‌ها', options: [], }; } catch (error) { return { disabled: true, placeholder: 'خطا در اتصال به سرور', options: [], }; } }, }), // Post content caption: apps_framework_1.Property.LongText({ displayName: 'متن پست', description: 'متن اصلی پست', required: true, }), type: apps_framework_1.Property.StaticDropdown({ displayName: 'نوع پست', description: 'نوع محتوای پست', required: true, options: { disabled: false, options: [ { label: 'متن', value: common_1.POST_TYPES.TEXT }, { label: 'تصویر', value: common_1.POST_TYPES.IMAGE }, { label: 'آلبوم', value: common_1.POST_TYPES.ALBUM }, { label: 'ویدیو', value: common_1.POST_TYPES.VIDEO }, { label: 'سند', value: common_1.POST_TYPES.DOCUMENT }, { label: 'استوری', value: common_1.POST_TYPES.STORY }, { label: 'نظرسنجی', value: common_1.POST_TYPES.POLL }, ], }, }), // Media files media_files: apps_framework_1.Property.MultiSelectDropdown({ displayName: 'فایل‌های رسانه', description: 'فایل‌هایی که به پست اضافه می‌شوند', required: false, refreshers: [], options: async ({ auth }) => { if (!auth) { return { disabled: true, placeholder: 'ابتدا احراز هویت کنید', options: [], }; } try { const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth.access_token}`, }; const response = await apps_common_1.httpClient.sendRequest({ method: apps_common_1.HttpMethod.GET, url: (0, common_1.buildApiUrl)('/file'), headers: headers, }); if (response.status === 200 && Array.isArray(response.body.data)) { const options = response.body.data.map((file) => ({ label: `${file.title} (${file.fileType})`, value: file.id, })); return { disabled: false, options: options, }; } return { disabled: true, placeholder: 'فایلی یافت نشد', options: [], }; } catch (error) { return { disabled: true, placeholder: 'خطا در بارگذاری فایل‌ها', options: [], }; } }, }), // Scheduling is_scheduled: apps_framework_1.Property.Checkbox({ displayName: 'زمان‌بندی شده', description: 'آیا پست در زمان خاصی منتشر شود؟', required: false, defaultValue: false, }), schedule_date: apps_framework_1.Property.DateTime({ displayName: 'زمان انتشار', description: 'زمان دقیق انتشار پست (در صورت زمان‌بندی)', required: false, }), // Draft is_draft: apps_framework_1.Property.Checkbox({ displayName: 'پیش‌نویس', description: 'ذخیره پست به عنوان پیش‌نویس', required: false, defaultValue: false, }), // Hashtags hashtags: apps_framework_1.Property.ShortText({ displayName: 'هشتگ‌ها', description: 'هشتگ‌ها را با کاما جدا کنید', required: false, }), // Social network specific settings network_settings: apps_framework_1.Property.Json({ displayName: 'تنظیمات شبکه‌های اجتماعی', description: 'تنظیمات خاص هر شبکه اجتماعی (telegram, instagram, youtube و...)', required: false, defaultValue: { "telegram_pin_message": false, "telegram_has_signature": false, "telegram_buttons": ["عنوان:https://example.com"], "instagram_reels": false, "instagram_first_comment": "", "youtube_title": "", "youtube_privacy": "public" }, }), }, async run(ctx) { const { accounts, caption, type, media_files, is_scheduled, schedule_date, is_draft, hashtags, network_settings } = ctx.propsValue; const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${ctx.auth.access_token}`, }; // Build request body const body = { caption, type, account_ids: accounts, is_scheduled: is_scheduled ? 1 : 0, is_draft: is_draft ? 1 : 0, }; // Add media files if selected if (media_files && media_files.length > 0) { body.media_ids = media_files; } // Add schedule date if scheduled if (is_scheduled && schedule_date) { body.schedule_date = Math.floor(new Date(schedule_date).getTime() / 1000); } // Add hashtags if (hashtags) { body.hashtag = hashtags.split(',').map(tag => tag.trim()).filter(tag => tag.length > 0); } // Add network-specific settings if (network_settings) { // Telegram settings if (network_settings.telegram_pin_message) { body.telegram_pin_message = 1; } if (network_settings.telegram_has_signature) { body.telegram_has_signature = 1; } if (network_settings.telegram_buttons && Array.isArray(network_settings.telegram_buttons)) { body.telegram_buttons = network_settings.telegram_buttons; } // Instagram settings if (network_settings.instagram_reels) { body.reels = 1; } if (network_settings.instagram_first_comment) { body.first_comment = network_settings.instagram_first_comment; } // YouTube settings if (network_settings.youtube_title) { body.youtube_title = network_settings.youtube_title; } if (network_settings.youtube_privacy) { body.youtube_privacy = network_settings.youtube_privacy; } } const url = (0, common_1.buildApiUrl)('/post'); try { const response = await apps_common_1.httpClient.sendRequest({ method: apps_common_1.HttpMethod.POST, url: url, headers: headers, body: body, }); if (response.status === 200 || response.status === 201) { return { success: true, post: response.body, message: 'پست با موفقیت ایجاد شد', }; } else { throw new Error(`خطا در ایجاد پست: ${response.body?.message || `HTTP ${response.status}`}`); } } catch (error) { const errorMessage = error.message || error.toString() || 'خطا در ایجاد پست'; return { success: false, error: errorMessage, }; } }, }); //# sourceMappingURL=create-post-enhanced.js.map