Skip to content
Draft
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 app/Data/SmsCommandType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
3 changes: 2 additions & 1 deletion app/Data/SmsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
},
Expand Down
44 changes: 44 additions & 0 deletions app/Events/SubmittedFyi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Events;

use App\Data\SmsCommand;
use App\Jobs\SendToDiscord;
use App\Models\PhoneNumber;
use Illuminate\Http\Request;
use Thunk\Verbs\Event;

class SubmittedFyi extends Event
{
public static function webhook(Request $request, SmsCommand $command): string
{
static::commit(
phone_number: $request->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,
);
}
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/Webhooks/TwilioWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions app/Jobs/SendToDiscord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

/** @method static PendingDispatch dispatch(string $message, string $channel = 'default') */
class SendToDiscord implements ShouldQueue
{
use Queueable;

public function __construct(
public string $message,
public string $channel = 'default'
){}

public function handle(): void
{
$url = config("discord.channels.{$this->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);
}
}
7 changes: 7 additions & 0 deletions config/discord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'channels' => [
'default' => env('DISCORD_FYI_WEBHOOK'),
],
];
6 changes: 6 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!)',
};

Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/FyiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use App\Events\SubmittedFyi;
use App\Jobs\SendToDiscord;
use Illuminate\Support\Facades\Queue;

test('you can submit resource information to discord', function () {
Queue::fake();

config()->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();
});
10 changes: 10 additions & 0 deletions tests/Unit/SmsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');