Skip to content

Commit

Permalink
email checker
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Aug 21, 2023
1 parent 4530f8b commit 73ae7a1
Show file tree
Hide file tree
Showing 11 changed files with 583 additions and 3 deletions.
35 changes: 35 additions & 0 deletions app/Console/Commands/CheckEmailCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Console\Commands;

use Facades\App\Domains\EmailParser\Client;
use Illuminate\Console\Command;

class CheckEmailCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'llm_assistant:check_email';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This will check email make sure the llm_assistant.check_email is true';

/**
* Execute the console command.
*/
public function handle()
{
logger('Checking Email');
$this->info('Checking Email');
Client::handle();
logger('Done Checking Email');
$this->info('Done Checking Email');
}
}
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->command('llm_assistant:check_email')->everyMinute();
$schedule->command('security-portal-client:sync')->environments('production')->hourly();
}

Expand Down
41 changes: 41 additions & 0 deletions app/Domains/EmailParser/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Domains\EmailParser;

use App\Jobs\MailBoxParserJob;
use Webklex\IMAP\Facades\Client as ClientFacade;

class Client
{
public function handle(): void
{
$client = ClientFacade::account('default');
$client->connect();

$folders = $client->getFolders(false);

foreach ($folders as $folder) {
$messages = $folder->messages()->all()->limit(3, 0)->get();

logger('Emails', [
$messages->count(),
]);

foreach ($messages as $message) {
logger('Getting Message');

$messageDto = MailDto::from([
'to' => $message->getTo(),
'from' => $message->getFrom(),
'body' => $message->getTextBody(),
'subject' => $message->getSubject(),
'header' => $message->getHeader()->raw,
]);

MailBoxParserJob::dispatch($messageDto);

$message->delete(expunge: true);
}
}
}
}
17 changes: 17 additions & 0 deletions app/Domains/EmailParser/MailDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Domains\EmailParser;

use Spatie\LaravelData\Data;

class MailDto extends Data
{
public function __construct(
public ?string $subject,
public ?string $from,
public ?string $to,
public ?string $body,
public ?string $header
) {
}
}
60 changes: 60 additions & 0 deletions app/Jobs/MailBoxParserJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Jobs;

use App\Domains\EmailParser\MailDto;
use App\Models\Message;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class MailBoxParserJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct(public MailDto $mailDto)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
try {

$tag = Tag::firstOrCreate([
'label' => 'email',
], [
'active' => 1,
]);

$message = Message::create([
'role' => 'user',
'user_id' => User::first()->id,
'content' => sprintf("subject: %s \n body: %s",
$this->mailDto->subject,
$this->mailDto->body),
]);

$message->tags()->attach($tag->id);

/**
* If tons of text we tldr it
* If a url we go get it then tldr it
* can make that a function?
*/
} catch (\Exception $e) {
logger('Email error', [$e->getMessage()]);
$this->fail();
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"symfony/postmark-mailer": "^6.2",
"tightenco/ziggy": "^1.0",
"voku/stop-words": "^2.0",
"wamania/php-stemmer": "^3.0"
"wamania/php-stemmer": "^3.0",
"webklex/laravel-imap": "^5.3"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
160 changes: 159 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 73ae7a1

Please sign in to comment.