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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### 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.
- Guards, all configurable: quiet hours (optional per-user timezone), cooldown, manual lock, max sends per user, hourly/daily/per-run limits, no double queueing. The send job cancels if an admin messaged the user by hand meanwhile.

### Added (automation, phase B: consent and webhook)
- Consent (`rehla.consent`): required by default. Sources `table` (`Rehla::consent()->optIn/optOut`), `attribute` or a custom `ConsentResolver`. Opting out cancels queued automatic sends; the job re-checks consent before sending. `Rehla::forget()` also deletes consent history. New table `rehla_consents` (run `php artisan rehla:install` or `migrate`).
- Meta webhook (opt-in by config): signature-checked delivery status updates that never move backwards, failure reasons, and STOP keyword handling in English and Arabic. Config: `rehla.webhooks`, `REHLA_META_VERIFY_TOKEN`, `REHLA_META_APP_SECRET`.
Expand Down
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,38 @@ Webhook (delivery status and STOP replies). It is only registered when the meta

In the panel, opted-out users get an "Opted out" mark and the follow-up modal asks for an explicit override before a manual message to them.

Rules, the panel tab and hardening come in the next releases, so do not point automation at real users yet.

### Rules and the engine

Rules say who gets which message and when. Define them in `rehla.automation.rules` (code, always available) or in the `rehla_automation_rules` table (the panel tab comes in the next release). Both run together.

```php
'rules' => [
'stuck-profile' => [
'journey' => 'default',
'step' => 'profile', // optional; omit for "the step they are due on"
'condition' => 'step_started_not_finished_for',
'after_days' => 2, // or after_hours / after_weeks
'message' => 'Hi {name}, finish "{step}" in {journey}', // or 'template_id' => 3
'max_sends_per_user' => 1,
// Meta: approved template name; params are placeholder names filling {{1}}, {{2}}
// 'provider_template' => ['name' => 'nudge_v1', 'language' => 'ar', 'params' => ['name', 'step']],
],
],
```

Conditions: `inactive_for` (no visit for the time), `step_not_started_for` (the step they are due on has not started, counted from their last completed step or registration), `step_started_not_finished_for`. Placeholders: `{name}`, `{step}`, `{journey}`, `{days_inactive}`.

Run it from the scheduler (every 5 to 15 minutes) and start with a dry run:

```php
Schedule::command('rehla:automate')->everyTenMinutes();
```
```bash
php artisan rehla:automate --dry-run # who would be messaged, who is skipped and why
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.
28 changes: 28 additions & 0 deletions config/rehla.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@
],
],

// Safety limits. All changeable; 0 or null turns a limit off.
'cooldown_hours' => 72, // no automatic message within this long after any sent follow-up
'manual_lock_hours' => 2, // an admin's open WhatsApp draft blocks automation this long
'limits' => [
'per_hour' => 60,
'per_day' => 500,
'per_run' => 100,
],
// No automatic sends in this window (local time; may cross midnight).
// user_timezone_attribute: a user column holding an IANA timezone.
'quiet_hours' => [
'start' => '21:00',
'end' => '09:00',
'timezone' => null, // null = app timezone
'user_timezone_attribute' => null,
],
// User column used as "registered at" when nothing else is known.
'registered_attribute' => 'created_at',

// Rules in code. Each: journey, condition (inactive_for,
// step_not_started_for, step_started_not_finished_for), one of
// after_hours / after_days / after_weeks, message (with {name},
// {step}, {journey}, {days_inactive}) or template_id, optional step,
// cooldown_hours, max_sends_per_user (default 1), provider_template
// ['name' => ..., 'language' => ..., 'params' => ['name', 'step']].
// Rules created in the panel (database) run alongside these.
'rules' => [],

// Where queued sends run. null = the app defaults.
'queue' => ['connection' => null, 'name' => null],

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('rehla_automation_rules', function (Blueprint $table): void {
$table->id();
$table->string('name');
$table->boolean('enabled')->default(true);
$table->string('journey');
$table->string('step')->nullable();
$table->string('condition', 48);
$table->unsignedInteger('after_hours');
$table->foreignId('template_id')->nullable();
$table->text('message')->nullable();
$table->json('provider_template')->nullable();
$table->unsignedInteger('cooldown_hours')->nullable();
$table->unsignedSmallInteger('max_sends_per_user')->default(1);
$table->string('created_by')->nullable();
$table->timestamps();

$table->index(['enabled', 'journey'], 'rehla_rules_enabled_journey');
});
}

public function down(): void
{
Schema::dropIfExists('rehla_automation_rules');
}
};
36 changes: 35 additions & 1 deletion docs/ar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,38 @@ Rehla::consent()->optOut($user); // وكمان بيلغي الرسائل

في البانل المستخدمين اللي عملوا إيقاف عليهم علامة "أوقف الرسائل"، والمودال بيطلب تأكيد صريح قبل رسالة يدوية ليهم.

القواعد وتاب البانل والتقوية جايين في الإصدارات الجاية، فلسه متوجّهش الإرسال التلقائي على مستخدمين حقيقيين.

### القواعد والمحرّك

القواعد بتحدد مين ياخد أنهي رسالة وإمتى. عرّفها في `rehla.automation.rules` (في الكود، متاحة دايماً) أو في جدول `rehla_automation_rules` (تاب البانل جاي في الإصدار الجاي). الاتنين بيشتغلوا مع بعض.

```php
'rules' => [
'stuck-profile' => [
'journey' => 'default',
'step' => 'profile', // اختياري؛ من غيره = الخطوة المستحقة عليه
'condition' => 'step_started_not_finished_for',
'after_days' => 2, // أو after_hours / after_weeks
'message' => 'أهلاً {name}، كمّل "{step}" في {journey}', // أو 'template_id' => 3
'max_sends_per_user' => 1,
// ميتا: اسم القالب المعتمد؛ params أسماء المتغيرات اللي بتملا {{1}} و{{2}}
// 'provider_template' => ['name' => 'nudge_v1', 'language' => 'ar', 'params' => ['name', 'step']],
],
],
```

الشروط: `inactive_for` (مدخلش المدة دي)، و`step_not_started_for` (الخطوة المستحقة عليه لسه مبدأهاش، من آخر خطوة خلصها أو من التسجيل)، و`step_started_not_finished_for`. المتغيرات: `{name}` و`{step}` و`{journey}` و`{days_inactive}`.

شغّله من الـscheduler (كل 5 لـ15 دقيقة) وابدأ بتجربة:

```php
Schedule::command('rehla:automate')->everyTenMinutes();
```
```bash
php artisan rehla:automate --dry-run # مين هيتبعتله ومين اتخطّى وليه
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 بيلغي الرسالة لو أدمن بعت للمستخدم يدوي في الوقت ده.

تاب البانل والتقوية جايين في الإصدارات الجاية، فلسه متوجّهش الإرسال التلقائي على مستخدمين حقيقيين.
Loading
Loading