-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from ECFMP/discord-bot
Discord bot
- Loading branch information
Showing
105 changed files
with
3,787 additions
and
467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "protobuf"] | ||
path = protobuf | ||
url = [email protected]:ECFMP/ecfmp-protobuf.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Log; | ||
use App\Discord\FlowMeasure\Generator\EcfmpFlowMeasureMessageGenerator; | ||
|
||
class SendEcfmpDiscordMessages extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'discord:send-ecfmp-messages'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send ECFMP Discord messages'; | ||
|
||
public function handle(EcfmpFlowMeasureMessageGenerator $generator): int | ||
{ | ||
if (!config('discord.enabled')) { | ||
Log::info('Skipping discord notifications, disabled in config'); | ||
return 0; | ||
} | ||
|
||
Log::info('Sending discord notifications'); | ||
$generator->generateAndSend(); | ||
Log::info('Discord notification sending complete'); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Discord\Client; | ||
|
||
use Ecfmp_discord\DiscordClient; | ||
use Grpc\ChannelCredentials; | ||
|
||
/** | ||
* This class exists because gRPC has trouble when the client is registered directly to the | ||
* service container (the channel is closed before the request is sent). This class is a | ||
* workaround for that issue. | ||
* @codeCoverageIgnore | ||
*/ | ||
class ClientFactory implements ClientFactoryInterface | ||
{ | ||
private DiscordClient|null $client = null; | ||
|
||
public function create(): DiscordClient | ||
{ | ||
if ($this->client === null) { | ||
$this->client = new DiscordClient( | ||
config('discord.service_host'), | ||
[ | ||
'credentials' => ChannelCredentials::createInsecure(), | ||
'grpc.primary_user_agent' => config('app.name'), | ||
], | ||
); | ||
} | ||
|
||
return $this->client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Discord\Client; | ||
|
||
use Ecfmp_discord\DiscordClient; | ||
|
||
interface ClientFactoryInterface | ||
{ | ||
public function create(): DiscordClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Discord; | ||
|
||
use App\Discord\Exception\DiscordServiceException; | ||
use App\Discord\Message\EcfmpMessageInterface; | ||
|
||
interface DiscordServiceInterface | ||
{ | ||
/** | ||
* Returns the remote message id, or throws an exception if the message could not be sent. | ||
* | ||
* @throws DiscordServiceException | ||
*/ | ||
public function sendMessage(string $clientRequestId, EcfmpMessageInterface $message): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Discord; | ||
|
||
use App\Discord\Client\ClientFactoryInterface; | ||
use App\Discord\Exception\DiscordServiceException; | ||
use App\Discord\Message\EcfmpMessageInterface; | ||
use Ecfmp_discord\CreateRequest; | ||
use Log; | ||
|
||
use const Grpc\STATUS_OK; | ||
|
||
class DiscordServiceMessageSender implements DiscordServiceInterface | ||
{ | ||
private readonly ClientFactoryInterface $discordClientFactory; | ||
|
||
public function __construct(ClientFactoryInterface $discordClientFactory) | ||
{ | ||
$this->discordClientFactory = $discordClientFactory; | ||
} | ||
|
||
public function sendMessage(string $clientRequestId, EcfmpMessageInterface $message): string | ||
{ | ||
$client = $this->discordClientFactory->create(); | ||
|
||
// Wait for 1 second for the channel to be ready | ||
$channelReady = $client->waitForReady(1000000); | ||
if (!$channelReady) { | ||
Log::error('Discord grpc channel not ready'); | ||
throw new DiscordServiceException('Discord grpc channel not ready'); | ||
} | ||
|
||
/** | ||
* @var $response \Ecfmp_discord\CreateResponse | ||
*/ | ||
[$response, $status] = $client->Create( | ||
new CreateRequest( | ||
[ | ||
'channel' => $message->channel(), | ||
'content' => $message->content(), | ||
'embeds' => $message->embeds()->toProtobuf(), | ||
] | ||
), | ||
[ | ||
'authorization' => [config('discord.service_token')], | ||
'x-client-request-id' => [$clientRequestId], | ||
], | ||
)->wait(); | ||
|
||
if ($status->code !== STATUS_OK) { | ||
Log::error('Discord grpc call failed', [ | ||
'code' => $status->code, | ||
'details' => $status->details, | ||
]); | ||
|
||
throw new DiscordServiceException('Discord grpc call failed'); | ||
} | ||
|
||
return $response->getId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Discord\Exception; | ||
|
||
use RuntimeException; | ||
|
||
class DiscordServiceException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/Discord/FlowMeasure/Generator/EcfmpFlowMeasureMessageGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Discord\FlowMeasure\Generator; | ||
|
||
use App\Discord\FlowMeasure\Helper\EcfmpNotificationReissuer; | ||
use App\Discord\FlowMeasure\Provider\PendingEcfmpMessage; | ||
use App\Discord\FlowMeasure\Sender\EcfmpFlowMeasureSender; | ||
use App\Repository\FlowMeasureNotification\RepositoryInterface; | ||
|
||
class EcfmpFlowMeasureMessageGenerator | ||
{ | ||
private readonly EcfmpFlowMeasureSender $sender; | ||
|
||
/** @var RepositoryInterface[] */ | ||
private readonly array $repositories; | ||
|
||
public function __construct(EcfmpFlowMeasureSender $sender, array $repositories) | ||
{ | ||
$this->sender = $sender; | ||
$this->repositories = $repositories; | ||
} | ||
|
||
public function generateAndSend(): void | ||
{ | ||
foreach ($this->repositories as $repository) { | ||
foreach ($repository->flowMeasuresToBeSentToEcfmp() as $measure) { | ||
$pendingMessage = new PendingEcfmpMessage( | ||
$measure->measure, | ||
$repository->notificationType(), | ||
new EcfmpNotificationReissuer($measure, $repository->notificationType()) | ||
); | ||
|
||
$this->sender->send($pendingMessage); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.