From 4d9283a79154677d9f437d718d6eb00f0eed2c46 Mon Sep 17 00:00:00 2001 From: hans-lts Date: Thu, 3 Oct 2024 20:41:44 -0500 Subject: [PATCH 1/4] WIP --- app/Data/SmsCommandType.php | 5 +++ app/Data/SmsParser.php | 3 +- app/Events/SubmittedFyi.php | 44 +++++++++++++++++++ .../Webhooks/TwilioWebhookController.php | 2 + 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 app/Events/SubmittedFyi.php diff --git a/app/Data/SmsCommandType.php b/app/Data/SmsCommandType.php index 28d04cd..bb99f70 100644 --- a/app/Data/SmsCommandType.php +++ b/app/Data/SmsCommandType.php @@ -28,4 +28,9 @@ enum SmsCommandType: string * Future command: check on another person via their phone number */ case Search = 'search'; + + /** + * Resource submission command + */ + case Fyi = 'fyi'; } diff --git a/app/Data/SmsParser.php b/app/Data/SmsParser.php index 1f3e273..08fa209 100644 --- a/app/Data/SmsParser.php +++ b/app/Data/SmsParser.php @@ -4,7 +4,7 @@ class SmsParser { - protected const PREFIXES = ['update', 'search', 'help', 'stop', 'cancel']; + protected const PREFIXES = ['update', 'search', 'help', 'stop', 'cancel', 'fyi']; public static function parse(string $body): SmsCommand { @@ -15,6 +15,7 @@ public static function parse(string $body): SmsCommand 'update' => SmsCommandType::Update, 'search' => SmsCommandType::Search, 'help' => SmsCommandType::Help, + 'fyi' => SmsCommandType::Fyi, 'stop', 'cancel' => SmsCommandType::OptOut, default => SmsCommandType::Invalid, }, diff --git a/app/Events/SubmittedFyi.php b/app/Events/SubmittedFyi.php new file mode 100644 index 0000000..c52a027 --- /dev/null +++ b/app/Events/SubmittedFyi.php @@ -0,0 +1,44 @@ +input('From'), + message: $command->message, + payload: $request->all(), + ); + + return 'Thanks! This information will be reviewed shortly.'; + } + + public function __construct( + public string $phone_number, + public string $message, + public array $payload, + public string $channel = 'default' + ) {} + + public function handle() + { + $phone_number = PhoneNumber::findByValueOrCreate($this->phone_number); + + $phone_number->update(['is_opted_out' => false]); + + $formatted_number = phone_number($phone_number)->formatNational(); + + SendToDiscord::dispatch( + message: "**{$formatted_number}**\n```$this->message```", + channel: $this->channel, + ); + } +} diff --git a/app/Http/Controllers/Webhooks/TwilioWebhookController.php b/app/Http/Controllers/Webhooks/TwilioWebhookController.php index 4306ecd..f73c3be 100644 --- a/app/Http/Controllers/Webhooks/TwilioWebhookController.php +++ b/app/Http/Controllers/Webhooks/TwilioWebhookController.php @@ -7,6 +7,7 @@ use App\Events\CheckedInViaSms; use App\Events\OptOutRequested; use App\Events\PhoneNumberQueried; +use App\Events\SubmittedFyi; use App\Events\TwilioWebhookReceived; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -33,6 +34,7 @@ public function __invoke(Request $request) SmsCommandType::Update => CheckedInViaSms::webhook($request, $command), SmsCommandType::Search => PhoneNumberQueried::webhook($request, $command), SmsCommandType::OptOut => OptOutRequested::webhook($request, $command), + SmsCommandType::Fyi => SubmittedFyi::webhook($request, $command), default => 'To send updates on DisasterCheckin site to anyone who knows your number, start your msg with "UPDATE" (UPDATE I am OK) SEARCH to find others (SEARCH 8285550000)', }); } catch (EventNotValid $exception) { From 5f31bf6bd020dce52f74c49850bf4ca3b9bdd9c6 Mon Sep 17 00:00:00 2001 From: hans-lts Date: Thu, 3 Oct 2024 20:41:51 -0500 Subject: [PATCH 2/4] WIP --- app/Jobs/SendToDiscord.php | 37 +++++++++++++++++++++++++++++++++++++ config/discord.php | 7 +++++++ 2 files changed, 44 insertions(+) create mode 100644 app/Jobs/SendToDiscord.php create mode 100644 config/discord.php diff --git a/app/Jobs/SendToDiscord.php b/app/Jobs/SendToDiscord.php new file mode 100644 index 0000000..9de120b --- /dev/null +++ b/app/Jobs/SendToDiscord.php @@ -0,0 +1,37 @@ +channel}"); + + if(empty($url)) { + Log::error("Discord channel [{$this->channel}] not configured"); + $this->fail("Discord channel [{$this->channel}] not configured"); + return; + } + + $payload = [ + 'content' => $this->message, + ]; + + Http::post($url, $payload); + } +} diff --git a/config/discord.php b/config/discord.php new file mode 100644 index 0000000..13b45b3 --- /dev/null +++ b/config/discord.php @@ -0,0 +1,7 @@ + [ + 'default' => env('DISCORD_FYI_WEBHOOK'), + ], +]; From a33b0d33c589c171bdce26f81df8676300ae9b87 Mon Sep 17 00:00:00 2001 From: hans-lts Date: Thu, 3 Oct 2024 20:41:56 -0500 Subject: [PATCH 3/4] WIP --- routes/console.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/routes/console.php b/routes/console.php index ccac319..bf200c1 100644 --- a/routes/console.php +++ b/routes/console.php @@ -5,6 +5,7 @@ use App\Events\CheckedInViaSms; use App\Events\OptOutRequested; use App\Events\PhoneNumberQueried; +use App\Events\SubmittedFyi; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Schedule; use Propaganistas\LaravelPhone\PhoneNumber; @@ -35,6 +36,11 @@ phone_number: $phone_number, payload: $synthetic_payload, ), + SmsCommandType::Fyi => SubmittedFyi::commit( + phone_number: $phone_number, + message: $command->message, + payload: $synthetic_payload, + ), default => 'Please start your message with the word "UPDATE" (eg. UPDATE I am doing OK!)', }; From a26c6e7bbe0bdad54036f35c96e1dd15e5eac68e Mon Sep 17 00:00:00 2001 From: hans-lts Date: Thu, 3 Oct 2024 20:42:05 -0500 Subject: [PATCH 4/4] WIP --- tests/Feature/FyiTest.php | 33 +++++++++++++++++++++++++++++++++ tests/Unit/SmsParserTest.php | 10 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/Feature/FyiTest.php diff --git a/tests/Feature/FyiTest.php b/tests/Feature/FyiTest.php new file mode 100644 index 0000000..2597f62 --- /dev/null +++ b/tests/Feature/FyiTest.php @@ -0,0 +1,33 @@ +set('discord.webhooks.default', 'https://some-test-site.com/webhook'); + + SubmittedFyi::commit( + phone_number: '+12024561111', + message: 'Some information for you', + payload: [], + ); + + Queue::assertPushed(SendToDiscord::class); +}); + + +test('it will fail the job if the webhook does not exist', function () { + Queue::fake(); + + $job = (new SendToDiscord( + message: "test", + channel: 'wrong-channel' + ))->withFakeQueueInteractions(); + + $job->handle(); + + $job->assertFailed(); +}); diff --git a/tests/Unit/SmsParserTest.php b/tests/Unit/SmsParserTest.php index bbfdea3..7834865 100644 --- a/tests/Unit/SmsParserTest.php +++ b/tests/Unit/SmsParserTest.php @@ -56,3 +56,13 @@ $parsed = SmsParser::parse('cancel hi'); expect($parsed->command)->toBe(SmsCommandType::OptOut); })->group('parser'); + +test('FYI is parsed', function () { + $parsed = SmsParser::parse('fyi abcd'); + expect($parsed->command)->toBe(SmsCommandType::Fyi) + ->and($parsed->message)->toBe("abcd"); + + $parsed = SmsParser::parse("fyi\nsome news for you\nabcd"); + expect($parsed->command)->toBe(SmsCommandType::Fyi) + ->and($parsed->message)->toBe("some news for you\nabcd"); +})->group('parser');