Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RTippin committed Dec 16, 2021
2 parents 3e6c7a9 + e708ec4 commit 10eef02
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 35 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Coin Toss Bot
- Commands Bot
- Dad Joke Bot
- Document Finder Bot
- Giphy Bot
- Insult Bot
- Invite Bot
Expand All @@ -40,7 +41,7 @@
- Weather Bot
- Wikipedia Bot
- YoMomma Bot
- Youtube Bot
- YouTube Bot
- Included Bot Packages:
- Games Package
- Jokester Package
Expand Down Expand Up @@ -131,6 +132,7 @@ use RTippin\MessengerBots\Bots\ChuckNorrisBot;
use RTippin\MessengerBots\Bots\CoinTossBot;
use RTippin\MessengerBots\Bots\CommandsBot;
use RTippin\MessengerBots\Bots\DadJokeBot;
use RTippin\MessengerBots\Bots\DocumentFinderBot;
use RTippin\MessengerBots\Bots\GiphyBot;
use RTippin\MessengerBots\Bots\InsultBot;
use RTippin\MessengerBots\Bots\InviteBot;
Expand Down Expand Up @@ -166,6 +168,7 @@ class MessengerServiceProvider extends ServiceProvider
CoinTossBot::class,
CommandsBot::class,
DadJokeBot::class,
DocumentFinderBot::class,
GiphyBot::class,
InsultBot::class,
InviteBot::class,
Expand Down
Binary file added assets/games_package_avatar.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/games_package_avatar.png
Binary file not shown.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<php>
<env name="APP_ENV" value="self-testing"/>
<env name="APP_KEY" value="base64:yk+bUVuZa1p86Dqjk9OjVK2R1pm6XHxC6xEKFq8utH0="/>
<env name="APP_URL" value="http://messenger.test"/>
<env name="CACHE_DRIVER" value="file"/>
</php>
</phpunit>
121 changes: 121 additions & 0 deletions src/Bots/DocumentFinderBot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace RTippin\MessengerBots\Bots;

use Illuminate\Database\Eloquent\Collection;
use RTippin\Messenger\Messenger;
use RTippin\Messenger\MessengerBots;
use RTippin\Messenger\Models\Message;
use RTippin\Messenger\Support\BotActionHandler;
use Throwable;

class DocumentFinderBot extends BotActionHandler
{
/**
* The bots settings.
*
* @return array
*/
public static function getSettings(): array
{
return [
'alias' => 'document_finder',
'description' => 'Search the group for uploaded documents. [ !document {search} ]',
'name' => 'Document Finder',
'unique' => true,
'triggers' => ['!document', '!doc'],
'match' => MessengerBots::MATCH_STARTS_WITH_CASELESS,
];
}

/**
* @return array
*/
public function rules(): array
{
return [
'limit' => ['nullable', 'integer', 'min:1', 'max:10'],
];
}

/**
* @throws Throwable
*/
public function handle(): void
{
$search = $this->getParsedMessage();

if (! is_null($search)) {
$documents = $this->searchDocuments($search);

if (! $documents->count()) {
$this->sendNoResultsMessage($search);

return;
}

$this->sendDocumentResultsMessages($documents, $search);

return;
}

$this->sendInvalidSearchMessage();

$this->releaseCooldown();
}

/**
* @throws Throwable
*/
private function sendInvalidSearchMessage(): void
{
$this->composer()->emitTyping()->message('Please select a valid search term, i.e. ( !document resume )');
}

/**
* @throws Throwable
*/
private function sendNoResultsMessage(string $search): void
{
$this->composer()->emitTyping()->message("I didn't find any document(s) matching ( $search )");
}

/**
* @param Collection $documents
* @param string $search
*
* @throws Throwable
*/
private function sendDocumentResultsMessages(Collection $documents, string $search): void
{
Messenger::shouldUseAbsoluteRoutes(true);

$this->composer()->emitTyping()->message("I found the following document(s) matching ( $search ) :");

$documents->each(fn (Message $document) => $this->sendDocumentMessage($document));
}

/**
* @param Message $document
*
* @throws Throwable
*/
private function sendDocumentMessage(Message $document): void
{
$this->composer()->message(":floppy_disk: $document->body - {$document->getDocumentDownloadRoute()}");
}

/**
* @param string $search
* @return Collection|Message[]
*/
private function searchDocuments(string $search): Collection
{
return $this->thread
->documents()
->latest()
->where('body', 'LIKE', "%$search%")
->limit($this->getPayload('limit') ?? 5)
->get();
}
}
2 changes: 2 additions & 0 deletions src/MessengerBotsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RTippin\MessengerBots\Bots\CoinTossBot;
use RTippin\MessengerBots\Bots\CommandsBot;
use RTippin\MessengerBots\Bots\DadJokeBot;
use RTippin\MessengerBots\Bots\DocumentFinderBot;
use RTippin\MessengerBots\Bots\GiphyBot;
use RTippin\MessengerBots\Bots\InsultBot;
use RTippin\MessengerBots\Bots\InviteBot;
Expand Down Expand Up @@ -39,6 +40,7 @@ class MessengerBotsServiceProvider extends ServiceProvider
CoinTossBot::class,
CommandsBot::class,
DadJokeBot::class,
DocumentFinderBot::class,
GiphyBot::class,
InsultBot::class,
InviteBot::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Packages/GamesPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function getSettings(): array
'alias' => 'games_package',
'description' => 'Bundles games you can play with the bot.',
'name' => 'Games',
'avatar' => __DIR__.'/../../assets/games_package_avatar.png',
'avatar' => __DIR__.'/../../assets/games_package_avatar.gif',
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Packages/NeoPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RTippin\Messenger\MessengerBots;
use RTippin\Messenger\Support\PackagedBot;
use RTippin\MessengerBots\Bots\CommandsBot;
use RTippin\MessengerBots\Bots\DocumentFinderBot;
use RTippin\MessengerBots\Bots\GiphyBot;
use RTippin\MessengerBots\Bots\InviteBot;
use RTippin\MessengerBots\Bots\KanyeBot;
Expand Down Expand Up @@ -43,6 +44,10 @@ public static function installs(): array
CommandsBot::class => [
'cooldown' => 120,
],
DocumentFinderBot::class => [
'cooldown' => 15,
'limit' => 10,
],
GiphyBot::class => [
'cooldown' => 15,
],
Expand Down
Loading

0 comments on commit 10eef02

Please sign in to comment.