Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Added (automation, phase D: panel tab)
- Automation tab in the panel: manage database rules, see config rules, dry-run preview per rule, history of the latest 50 automatic sends. Hide it with `rehla.panel.automation_tab = false`. EN and AR.

### Added (automation, phase C: rules and engine)
- `rehla:automate [--dry-run] [--rule=]`: finds users matching a rule, runs the guards and queues messages. Conditions: `inactive_for`, `step_not_started_for`, `step_started_not_finished_for`, with thresholds in hours, days or weeks.
- Rules in `rehla.automation.rules` (config) and the new `rehla_automation_rules` table (run `rehla:install` or `migrate`). Placeholders `{name}`, `{step}`, `{journey}`, `{days_inactive}`, plus a Meta template parameter mapping.
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,6 @@ php artisan rehla:automate --rule=stuck-profile

Guards, in order, each skip counted with its reason: valid phone, consent (and no opt-out), quiet hours, cooldown, manual lock, already queued, max sends per user, then global limits. All are in `rehla.automation`: `quiet_hours` (default 21:00 to 09:00, app timezone, optional per-user timezone column), `cooldown_hours` (72, or per rule), `manual_lock_hours` (2: an admin's open WhatsApp draft blocks automation), `limits` (`per_hour`, `per_day`, `per_run`; 0 turns one off). Just before sending, the job cancels the message if an admin messaged that user by hand in the meantime.

The panel tab and hardening come in the next releases, so do not point automation at real users yet.
The panel has an **Automation** tab: rules from the database (add, edit, turn on or off, delete), config rules (read-only), a dry-run preview per rule (who would be messaged now, who is skipped and why) and the latest 50 automatic sends with delivery status. It is optional: set `rehla.panel.automation_tab` to `false` to hide it and close its routes. Everything it does is also possible from config and `rehla:automate`.

Hardening and the final checklist come in the next release, so do not point automation at real users yet.
3 changes: 3 additions & 0 deletions config/rehla.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
'path' => 'rehla',
'middleware' => ['web'],
'gate' => 'viewRehla',
// Show the Automation tab (rules, dry-run preview, history). false
// hides it and closes its routes; rules still work from config.
'automation_tab' => true,
],

/*
Expand Down
4 changes: 3 additions & 1 deletion docs/ar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,6 @@ php artisan rehla:automate --rule=stuck-profile

الحمايات بالترتيب، وكل تخطّي بيتعدّ بسببه: رقم صالح، الموافقة (ومفيش إيقاف)، ساعات الهدوء، فترة الانتظار، قفل اليدوي، مستني في الطابور، أقصى عدد رسائل للمستخدم، وبعدين الحدود العامة. كلها في `rehla.automation`: `quiet_hours` (الافتراضي 21:00 لـ09:00 بتوقيت التطبيق، وعمود توقيت لكل مستخدم اختياري)، و`cooldown_hours` (72، أو لكل قاعدة)، و`manual_lock_hours` (2: مسودة واتساب مفتوحة عند أدمن بتوقّف الأتمتة)، و`limits` (`per_hour` و`per_day` و`per_run`؛ 0 بيقفل الحد). قبل الإرسال مباشرة، الـjob بيلغي الرسالة لو أدمن بعت للمستخدم يدوي في الوقت ده.

تاب البانل والتقوية جايين في الإصدارات الجاية، فلسه متوجّهش الإرسال التلقائي على مستخدمين حقيقيين.
في البانل تاب **الأتمتة**: قواعد من الداتابيز (إضافة وتعديل وتشغيل وإيقاف وحذف)، وقواعد الكونفيج (للقراءة بس)، ومعاينة تجريبية لكل قاعدة (مين هيتبعتله دلوقتي ومين اتخطّى وليه)، وآخر 50 رسالة تلقائية مع حالة التسليم. اختياري: خلّي `rehla.panel.automation_tab` بـ `false` عشان تخفيه وتقفل مساراته. وكل اللي بيعمله ممكن من الكونفيج و`rehla:automate`.

التقوية وقايمة المراجعة الأخيرة جايين في الإصدار الجاي، فلسه متوجّهش الإرسال التلقائي على مستخدمين حقيقيين.
47 changes: 47 additions & 0 deletions resources/lang/ar/rehla.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'panel_title' => 'رحلة المستخدمين',
'nav_dashboard' => 'جدول الرحلة',
'nav_templates' => 'قوالب الرسائل',
'nav_automation' => 'الأتمتة',
'filter_label' => 'لم يزر الموقع منذ',
'days_unit' => 'يوم/أيام',
'filter_apply' => 'تصفية',
Expand Down Expand Up @@ -91,4 +92,50 @@
'unknown_step' => 'هذه الخطوة غير معرّفة في الرحلة المختارة.',
'step_needs_journey' => 'اختر الرحلة عند ربط القالب بخطوة.',
],
'automation' => [
'title' => 'الأتمتة',
'new' => 'قاعدة جديدة',
'create' => 'إنشاء قاعدة',
'edit' => 'تعديل القاعدة',
'rules' => 'القواعد',
'history' => 'الرسائل التلقائية (آخر 50)',
'name' => 'الاسم',
'condition' => 'الشرط',
'status' => 'الحالة',
'when' => 'الوقت',
'user' => 'المستخدم',
'message' => 'الرسالة',
'on' => 'شغّالة',
'off' => 'موقوفة',
'turn_on' => 'شغّلها',
'turn_off' => 'أوقفها',
'enabled' => 'القاعدة شغّالة',
'from_config' => 'من الكونفيج',
'preview' => 'معاينة (تجربة)',
'preview_hint' => 'مفيش حاجة بتتبعت. ده بيوريك مين هيتبعتله دلوقتي ومين اتخطّى وليه.',
'would_send' => 'هيتبعت لـ :count مستخدم',
'nobody' => 'مفيش حد مطابق دلوقتي.',
'skipped' => 'المتخطّين',
'empty' => 'مفيش قواعد لسه.',
'no_history' => 'مفيش رسائل تلقائية لسه.',
'confirm_delete' => 'تمسح القاعدة دي؟',
'disabled_notice' => 'الأتمتة مقفولة (rehla.automation.enabled). القواعد محفوظة بس مفيش حاجة هتتبعت.',
'provider_hint' => 'المزوّد: :provider',
'any_step' => 'الخطوة المستحقة عليه',
'after' => 'بعد',
'template' => 'قالب الرسالة',
'message_or' => 'أو اكتب الرسالة',
'placeholders' => 'تقدر تستخدم {name} و{step} و{journey} و{days_inactive}.',
'cooldown' => 'ساعات الانتظار (فاضي = الافتراضي العام)',
'max_sends' => 'أقصى عدد رسائل للمستخدم من القاعدة دي',
'meta' => 'قالب ميتا (الاسم، اللغة، المتغيرات بالترتيب)',
'meta_hint' => 'مطلوب للرسائل برّه نافذة الـ24 ساعة. المتغيرات بتملا {{1}} و{{2}} بالترتيب.',
'needs_message' => 'اختار قالب أو اكتب رسالة.',
'conditions' => [
'inactive_for' => 'مدخلش من',
'step_not_started_for' => 'الخطوة لسه مبدأهاش من',
'step_started_not_finished_for' => 'بدأ الخطوة ومخلصهاش من',
],
'units' => ['hours' => 'ساعة', 'days' => 'يوم', 'weeks' => 'أسبوع'],
],
];
47 changes: 47 additions & 0 deletions resources/lang/en/rehla.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'panel_title' => 'User journeys',
'nav_dashboard' => 'Journey table',
'nav_templates' => 'Message templates',
'nav_automation' => 'Automation',
'filter_label' => 'No visit for at least',
'days_unit' => 'day(s)',
'filter_apply' => 'Filter',
Expand Down Expand Up @@ -91,4 +92,50 @@
'unknown_step' => 'This step is not defined in the chosen journey.',
'step_needs_journey' => 'Choose a journey when the template is tied to a step.',
],
'automation' => [
'title' => 'Automation',
'new' => 'New rule',
'create' => 'Create rule',
'edit' => 'Edit rule',
'rules' => 'Rules',
'history' => 'Automatic sends (latest 50)',
'name' => 'Name',
'condition' => 'Condition',
'status' => 'Status',
'when' => 'When',
'user' => 'User',
'message' => 'Message',
'on' => 'On',
'off' => 'Off',
'turn_on' => 'Turn on',
'turn_off' => 'Turn off',
'enabled' => 'Rule is on',
'from_config' => 'config',
'preview' => 'Preview (dry run)',
'preview_hint' => 'Nothing is sent. This shows who would be messaged right now and why others are skipped.',
'would_send' => 'Would message :count user(s)',
'nobody' => 'Nobody matches right now.',
'skipped' => 'Skipped',
'empty' => 'No rules yet.',
'no_history' => 'No automatic messages yet.',
'confirm_delete' => 'Delete this rule?',
'disabled_notice' => 'Automation is off (rehla.automation.enabled). Rules are kept but nothing will be sent.',
'provider_hint' => 'Provider: :provider',
'any_step' => 'The step they are due on',
'after' => 'After',
'template' => 'Message template',
'message_or' => 'Or write the message',
'placeholders' => 'You can use {name}, {step}, {journey}, {days_inactive}.',
'cooldown' => 'Cooldown hours (empty = the global default)',
'max_sends' => 'Max messages per user from this rule',
'meta' => 'Meta template (name, language, placeholders in order)',
'meta_hint' => 'Needed to message users outside the 24-hour window. Placeholders fill {{1}}, {{2}} in order.',
'needs_message' => 'Pick a template or write a message.',
'conditions' => [
'inactive_for' => 'No visit for',
'step_not_started_for' => 'Step not started for',
'step_started_not_finished_for' => 'Step started, not finished for',
],
'units' => ['hours' => 'hour(s)', 'days' => 'day(s)', 'weeks' => 'week(s)'],
],
];
98 changes: 98 additions & 0 deletions resources/views/automation/_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
@php($registry = app(\Rehla\Services\JourneyRegistry::class))
@php($templates = \Rehla\Models\RehlaTemplate::query()->orderBy('title')->get())
@php($rule = $rule ?? null)
@php($hours = (int) ($rule->after_hours ?? 48))
@php($unit = $hours % 168 === 0 ? 'weeks' : ($hours % 24 === 0 ? 'days' : 'hours'))
@php($value = $unit === 'weeks' ? intdiv($hours, 168) : ($unit === 'days' ? intdiv($hours, 24) : $hours))
@php($meta = $rule->provider_template ?? [])

@if($errors->any())
<div class="errors">
@foreach($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
</div>
@endif

<div class="field">
<label for="name">{{ __('rehla::rehla.automation.name') }}</label>
<input type="text" id="name" name="name" value="{{ old('name', $rule->name ?? '') }}" required maxlength="190">
</div>

<div class="field">
<label for="journey">{{ __('rehla::rehla.templates.journey') }}</label>
<select id="journey" name="journey" required>
@foreach($registry->keys() as $key)
<option value="{{ $key }}" @selected(old('journey', $rule->journey ?? '') === $key)>{{ $registry->label($key) }}</option>
@endforeach
</select>
</div>

<div class="field">
<label for="step">{{ __('rehla::rehla.templates.step') }} ({{ __('rehla::rehla.templates.optional') }})</label>
<select id="step" name="step">
<option value="">{{ __('rehla::rehla.automation.any_step') }}</option>
@foreach($registry->keys() as $key)
@foreach($registry->steps($key) as $stepKey => $stepLabel)
<option value="{{ $stepKey }}" data-journey="{{ $key }}" @selected(old('step', $rule->step ?? '') === $stepKey)>{{ $stepLabel }}</option>
@endforeach
@endforeach
</select>
</div>

<div class="field">
<label for="condition">{{ __('rehla::rehla.automation.condition') }}</label>
<select id="condition" name="condition" required>
@foreach(\Rehla\Automation\Rule::CONDITIONS as $c)
<option value="{{ $c }}" @selected(old('condition', $rule->condition ?? '') === $c)>{{ __('rehla::rehla.automation.conditions.'.$c) }}</option>
@endforeach
</select>
</div>

<div class="field">
<label for="after_value">{{ __('rehla::rehla.automation.after') }}</label>
<input type="number" id="after_value" name="after_value" min="1" value="{{ old('after_value', $value) }}" required>
<select name="after_unit">
@foreach(['hours', 'days', 'weeks'] as $u)
<option value="{{ $u }}" @selected(old('after_unit', $unit) === $u)>{{ __('rehla::rehla.automation.units.'.$u) }}</option>
@endforeach
</select>
</div>

<div class="field">
<label for="template_id">{{ __('rehla::rehla.automation.template') }}</label>
<select id="template_id" name="template_id">
<option value="">—</option>
@foreach($templates as $template)
<option value="{{ $template->id }}" @selected((string) old('template_id', $rule->template_id ?? '') === (string) $template->id)>{{ $template->title }}</option>
@endforeach
</select>
</div>

<div class="field">
<label for="message">{{ __('rehla::rehla.automation.message_or') }}</label>
<textarea id="message" name="message" maxlength="5000">{{ old('message', $rule->message ?? '') }}</textarea>
<span class="hint">{{ __('rehla::rehla.automation.placeholders') }}</span>
</div>

<div class="field">
<label for="cooldown_hours">{{ __('rehla::rehla.automation.cooldown') }}</label>
<input type="number" id="cooldown_hours" name="cooldown_hours" min="0" value="{{ old('cooldown_hours', $rule->cooldown_hours ?? '') }}">
</div>

<div class="field">
<label for="max_sends_per_user">{{ __('rehla::rehla.automation.max_sends') }}</label>
<input type="number" id="max_sends_per_user" name="max_sends_per_user" min="1" value="{{ old('max_sends_per_user', $rule->max_sends_per_user ?? 1) }}" required>
</div>

<div class="field">
<label>{{ __('rehla::rehla.automation.meta') }} ({{ __('rehla::rehla.templates.optional') }})</label>
<input type="text" name="meta_template" placeholder="template_name" value="{{ old('meta_template', $meta['name'] ?? '') }}">
<input type="text" name="meta_language" placeholder="ar" size="4" value="{{ old('meta_language', $meta['language'] ?? '') }}">
<input type="text" name="meta_params" placeholder="name, step" value="{{ old('meta_params', implode(', ', $meta['params'] ?? [])) }}">
<span class="hint">{{ __('rehla::rehla.automation.meta_hint') }}</span>
</div>

<div class="field">
<label><input type="checkbox" name="enabled" value="1" @checked(old('enabled', $rule->enabled ?? true))> {{ __('rehla::rehla.automation.enabled') }}</label>
</div>
12 changes: 12 additions & 0 deletions resources/views/automation/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@extends('rehla::layout')

@section('title', __('rehla::rehla.automation.create'))

@section('content')
<h1>{{ __('rehla::rehla.automation.create') }}</h1>
<form method="post" action="{{ route('rehla.automation.store') }}">
@csrf
@include('rehla::automation._form')
<button type="submit" class="btn">{{ __('rehla::rehla.templates.save') }}</button>
</form>
@endsection
13 changes: 13 additions & 0 deletions resources/views/automation/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@extends('rehla::layout')

@section('title', __('rehla::rehla.automation.edit'))

@section('content')
<h1>{{ __('rehla::rehla.automation.edit') }}</h1>
<form method="post" action="{{ route('rehla.automation.update', $rule->id) }}">
@method('PUT')
@csrf
@include('rehla::automation._form')
<button type="submit" class="btn">{{ __('rehla::rehla.templates.save') }}</button>
</form>
@endsection
Loading
Loading