UNPKG

@anjir/app-novinhub

Version:

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

186 lines 9.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.novinhubCreateAutomationMessageAction = 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.novinhubCreateAutomationMessageAction = (0, apps_framework_1.createAction)({ auth: index_1.novinhubAuth, name: 'create_automation_message', displayName: 'ایجاد پیام خودکار', description: 'ساخت پیام دایرکت هوشمند. نوع پیام را انتخاب کنید و فقط فیلدهای مرتبط را پر کنید.', props: { accountId: apps_framework_1.Property.Number({ displayName: 'شناسه اکانت', description: 'شناسه اکانتی که پیام برای آن ایجاد می‌شود', required: true, }), title: apps_framework_1.Property.ShortText({ displayName: 'عنوان پیام', description: 'عنوان پیام برای شناسایی آسان', required: true, }), type: apps_framework_1.Property.StaticDropdown({ displayName: 'نوع پیام', description: 'Text، Button، Media یا Products', required: true, options: { disabled: false, options: [ { label: 'متن', value: common_1.AUTOMATION_MESSAGE_TYPES.TEXT }, { label: 'دکمه', value: common_1.AUTOMATION_MESSAGE_TYPES.BUTTON }, { label: 'رسانه', value: common_1.AUTOMATION_MESSAGE_TYPES.MEDIA }, { label: 'محصولات', value: common_1.AUTOMATION_MESSAGE_TYPES.PRODUCTS }, ], }, }), text: apps_framework_1.Property.LongText({ displayName: 'متن پیام', description: 'متن اصلی پیام (در صورت نیاز)', required: true, }), // Quick Replies - Always available quick_replies: apps_framework_1.Property.Array({ displayName: 'پاسخ‌های سریع', description: 'گزینه‌های Quick Reply (اختیاری)', required: false, properties: { title: apps_framework_1.Property.ShortText({ displayName: 'عنوان پاسخ', description: 'متن روی دکمه پاسخ سریع', required: true, }), payload: apps_framework_1.Property.ShortText({ displayName: 'محتوای پاسخ', description: 'داده‌ای که هنگام کلیک ارسال می‌شود', required: true, }), }, }), // Dynamic properties based on message type message_content: apps_framework_1.Property.DynamicProperties({ displayName: 'محتوای پیام', description: 'فقط فیلدهای موردنیاز نوع انتخاب‌شده نمایش داده می‌شود', required: false, refreshers: ['type'], props: async ({ type }) => { const properties = {}; const selectedType = type; if (selectedType === common_1.AUTOMATION_MESSAGE_TYPES.BUTTON) { properties.buttons = apps_framework_1.Property.Array({ displayName: 'دکمه‌ها', description: 'لیست دکمه‌های پیام', required: true, properties: { type: apps_framework_1.Property.StaticDropdown({ displayName: 'نوع دکمه', description: 'نوع دکمه', required: true, options: { disabled: false, options: [ { label: 'لینک', value: 'web_url' }, { label: 'عملیات', value: 'postback' }, ], }, }), title: apps_framework_1.Property.ShortText({ displayName: 'عنوان دکمه', description: 'متن روی دکمه', required: true, }), url: apps_framework_1.Property.ShortText({ displayName: 'آدرس لینک', description: 'آدرس URL برای دکمه‌های لینک', required: false, }), payload: apps_framework_1.Property.ShortText({ displayName: 'محتوای عملیات', description: 'داده‌ای که هنگام کلیک ارسال می‌شود', required: false, }), }, }); } else if (selectedType === common_1.AUTOMATION_MESSAGE_TYPES.MEDIA) { properties.file_id = apps_framework_1.Property.Number({ displayName: 'شناسه فایل', description: 'شناسه فایل رسانه‌ای که قبلاً آپلود شده', required: true, }); } else if (selectedType === common_1.AUTOMATION_MESSAGE_TYPES.PRODUCTS) { properties.product_ids = apps_framework_1.Property.Array({ displayName: 'شناسه محصولات', description: 'لیست شناسه محصولات', required: true, properties: { product_id: apps_framework_1.Property.Number({ displayName: 'شناسه محصول', description: 'شناسه محصول', required: true, }), }, }); } return properties; }, }), }, async run(ctx) { const { accountId, title, type, text, quick_replies, message_content } = ctx.propsValue; const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${ctx.auth.access_token}`, }; const body = { title, type, text, }; // Add optional fields based on type if (quick_replies && quick_replies.length > 0) { body.quick_replies = quick_replies; } // Add message type specific content if (message_content && typeof message_content === 'object') { if (type === common_1.AUTOMATION_MESSAGE_TYPES.BUTTON && message_content.buttons) { body.buttons = message_content.buttons; } if (type === common_1.AUTOMATION_MESSAGE_TYPES.MEDIA && message_content.file_id) { body.file_id = message_content.file_id; } if (type === common_1.AUTOMATION_MESSAGE_TYPES.PRODUCTS && message_content.product_ids && message_content.product_ids.length > 0) { body.product_ids = message_content.product_ids.map((item) => item.product_id); } } const url = (0, common_1.buildApiUrl)(`/automation/${accountId}/message`); 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, automation_message: 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-automation-message.js.map