This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
64 changed files
with
2,464 additions
and
1,347 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php namespace App\Espinoso; | ||
|
||
use Illuminate\Support\Collection; | ||
use Telegram\Bot\Objects\Message; | ||
use Telegram\Bot\Objects\User as TelegramUser; | ||
|
||
/** | ||
* Class BrainNode | ||
* @package App\Espinoso | ||
*/ | ||
class BrainNode | ||
{ | ||
protected $regex; | ||
protected $reply; | ||
protected $match; | ||
protected $matches; | ||
protected $ignored; | ||
|
||
public function __construct(string $regex, array $data = []) | ||
{ | ||
$this->regex = $regex; | ||
$this->reply = $data['reply'] ?? ''; | ||
$this->ignored = collect($data['ignored'] ?? []); | ||
} | ||
|
||
public function matchMessage(Message $message) | ||
{ | ||
$this->match = preg_match($this->regex, $message->getText(), $this->matches) === 1; | ||
|
||
return !empty($this->reply) | ||
&& $this->shouldResponseTo($message->getFrom()) | ||
&& $this->match; | ||
} | ||
|
||
public function pickReply(Message $message) | ||
{ | ||
return is_array($this->reply) | ||
? $this->pickFromBag($message) | ||
: $this->reply; | ||
} | ||
|
||
public function addIgnored(Collection $ignored) { | ||
$this->ignored->merge($ignored); | ||
} | ||
|
||
protected function shouldResponseTo(TelegramUser $from) | ||
{ | ||
// TODO | ||
return true; | ||
} | ||
|
||
protected function pickFromBag(Message $message) | ||
{ | ||
// FIXME: make a better behavior than simple random | ||
$number = rand(0, count($this->reply) - 1); | ||
|
||
$reply = $this->reply[$number]; | ||
|
||
if (str_contains($reply, ':name:')) { | ||
$reply = str_replace(':name:', $message->getFrom()->getFirstName(), $reply); | ||
} | ||
|
||
return $reply; | ||
} | ||
|
||
} |
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,68 @@ | ||
<?php namespace App\Espinoso; | ||
|
||
use Exception; | ||
use Illuminate\Support\Collection; | ||
use Telegram\Bot\Objects\Message; | ||
use Telegram\Bot\Api as ApiTelegram; | ||
use App\Espinoso\Handlers\EspinosoHandler; | ||
|
||
/** | ||
* Class Espinoso | ||
* @package Espinoso | ||
*/ | ||
class Espinoso | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $handlers; | ||
|
||
public function __construct(Collection $handlers) | ||
{ | ||
$this->handlers = $handlers; | ||
} | ||
|
||
/** | ||
* @param ApiTelegram $telegram | ||
* @param Message $message | ||
*/ | ||
public function executeHandlers(ApiTelegram $telegram, Message $message) | ||
{ | ||
$this->getHandlers()->map(function ($handler) use ($telegram) { | ||
return new $handler($this, $telegram); | ||
})->filter(function (EspinosoHandler $handler) use ($message) { | ||
return $handler->shouldHandle($message); | ||
})->each(function (EspinosoHandler $handler) use ($message) { | ||
try { | ||
$handler->handle($message); | ||
} catch (Exception $e) { | ||
$handler->handleError($e, $message); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return Collection | ||
*/ | ||
public function getHandlers(): Collection | ||
{ | ||
return $this->handlers; | ||
} | ||
|
||
// public function register(stdClass $update) | ||
// { | ||
// $from = $update->message->from; | ||
// | ||
// $user = TelegramUser::whereTelegramId($from->id)->first(); | ||
// if (!$user) { | ||
// $user = new TelegramUser; | ||
// $user->telegram_id = $from->id; | ||
// } | ||
// | ||
// $user->first_name = $from->first_name ?? ''; | ||
// $user->last_name = $from->last_name ?? ''; | ||
// $user->username = $from->username ?? ''; | ||
// $user->save(); | ||
// } | ||
|
||
} |
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 |
---|---|---|
@@ -1,21 +1,27 @@ | ||
<?php namespace App\Espinoso\Handlers; | ||
|
||
use Telegram\Bot\Laravel\Facades\Telegram; | ||
use Telegram\Bot\Objects\Message; | ||
|
||
class BardoDelEspinosoHandler extends EspinosoHandler | ||
class BardoDelEspinosoHandler extends EspinosoCommandHandler | ||
{ | ||
public function shouldHandle($updates, $context = null) | ||
{ | ||
return $this->isTextMessage($updates) | ||
&& preg_match('/^send me nudes$/i', $updates->message->text) ; | ||
} | ||
/** | ||
* @var string | ||
*/ | ||
protected $allow_ignore_prefix = true; | ||
/** | ||
* @var string | ||
*/ | ||
protected $pattern = "send me nudes$"; | ||
|
||
protected $signature = "[espi] send me nudes"; | ||
protected $description = "no sé, fijate"; | ||
|
||
public function handle($updates, $context = null) | ||
public function handle(Message $message) | ||
{ | ||
return Telegram::sendPhoto([ | ||
'chat_id' => $updates->message->chat->id, | ||
return $this->telegram->sendPhoto([ | ||
'chat_id' => $message->getChat()->getId(), | ||
'photo' => 'https://cdn.drawception.com/images/panels/2012/4-4/FErsE1a6t7-8.png', | ||
'caption' => 'Acá tenés tu nude, puto del orto!' | ||
'caption' => 'Acá tenés tu nude, hijo de puta!' | ||
]); | ||
} | ||
} | ||
} |
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,95 @@ | ||
<?php namespace App\Espinoso\Handlers; | ||
|
||
use App\Espinoso\Espinoso; | ||
use App\Espinoso\BrainNode; | ||
use Telegram\Bot\Objects\Message; | ||
use Telegram\Bot\Api as ApiTelegram; | ||
|
||
class BrainHandler extends EspinosoHandler | ||
{ | ||
protected $allNodes; | ||
protected $matchedNodes; | ||
|
||
protected $signature = "macri, facu, ine, alan, asado, ..."; | ||
protected $description = "Macri Gato, Facu Puto"; | ||
|
||
public function __construct(Espinoso $espinoso, ApiTelegram $telegram) | ||
{ | ||
parent::__construct($espinoso, $telegram); | ||
|
||
$this->matchedNodes = collect([]); | ||
$this->allNodes = collect(config('brain.patterns'))->map(function ($data, $regex) { | ||
return new BrainNode($regex, $data); | ||
}); | ||
} | ||
|
||
public function shouldHandle(Message $message): bool | ||
{ | ||
$this->matchedNodes = $this->allNodes->filter(function ($node) use ($message) { | ||
$node->addIgnored($this->globalIgnored()); | ||
return $node->matchMessage($message); | ||
}); | ||
|
||
return $this->matchedNodes->isNotEmpty(); | ||
} | ||
|
||
public function handle(Message $message) | ||
{ | ||
$this->matchedNodes->each(function (BrainNode $node) use ($message) { | ||
$this->telegram->sendMessage([ | ||
'chat_id' => $message->getChat()->getId(), | ||
'text' => $node->pickReply($message), | ||
'parse_mode' => 'Markdown' | ||
]); | ||
}); | ||
} | ||
|
||
/* | ||
* Internals | ||
*/ | ||
|
||
protected function globalIgnored() | ||
{ | ||
return collect(config('brain.ignore_to')); | ||
} | ||
|
||
// public function handle(Message $message) | ||
// { | ||
// if ($this->ignoringSender($message->getFrom())) { | ||
// $fromName = $message->getFrom()->getFirstName(); | ||
// $msg = Msg::md("Con vos no hablo porque no viniste al asado $fromName")->build($message); | ||
// $this->telegram->sendMessage($msg); | ||
// return; | ||
// } | ||
// | ||
// foreach ($this->mappings() as $pattern => $response) { | ||
// if ( preg_match($pattern, $message->getText()) ) { | ||
// $msg = $this->buildMessage($response, $pattern, $message); | ||
// $this->telegram->sendMessage($msg); | ||
// } | ||
// } | ||
// } | ||
|
||
// private function buildMessage($response, $pattern, Message $message) | ||
// { | ||
// if ($response instanceof Msg) | ||
// return $response->build($message, $pattern); | ||
// else | ||
// return Msg::plain($response)->build($message, $pattern); | ||
// } | ||
// | ||
// private function mappings() | ||
// { | ||
// return config('espinoso_data.ResponseByMatch.mappings'); | ||
// } | ||
// | ||
|
||
// private function ignoringSender($sender) | ||
// { | ||
// foreach ($this->ignoredNames() as $name) | ||
// if ( preg_match("/$name/i", $sender->first_name) ) | ||
// return true ; | ||
// return false ; | ||
// } | ||
|
||
} |
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 |
---|---|---|
@@ -1,32 +1,44 @@ | ||
<?php namespace App\Espinoso\Handlers; | ||
|
||
use Telegram\Bot\Objects\Message; | ||
|
||
abstract class EspinosoCommandHandler extends EspinosoHandler | ||
{ | ||
protected $flags = 'i'; | ||
protected $prefix_regex = "^(?'e'espi(noso)?\s+)"; // 'espi|espinoso ' | ||
protected $pattern = '$'; | ||
protected $matches = []; | ||
/** | ||
* @var bool | ||
* If false, should match 'espi' | ||
* If true, could not match 'espi' | ||
*/ | ||
protected $allow_ignore_prefix = false; | ||
|
||
/** | ||
* Default behavior to determine is Command handler should response the message. | ||
* | ||
* @param Message $message | ||
* @return bool | ||
*/ | ||
public function shouldHandle(Message $message): bool | ||
{ | ||
return $this->matchCommand($this->pattern, $message, $this->matches); | ||
} | ||
|
||
/** | ||
* @param $pattern | ||
* @param $updates | ||
* @param Message $message | ||
* @param array|null $matches | ||
* @return int | ||
* @return bool | ||
*/ | ||
protected function matchCommand($pattern, $updates, array &$matches = null) | ||
protected function matchCommand($pattern, Message $message, array &$matches = null): bool | ||
{ | ||
$quantifier = $this->allow_ignore_prefix ? '?' : '{1,3}'; | ||
$text = $this->isTextMessage($updates) ? $updates->message->text : ''; | ||
$quantifier = $this->allow_ignore_prefix ? '{0,3}' : '{1,3}'; | ||
$text = $message->getText(); | ||
$pattern = "/{$this->prefix_regex}{$quantifier}{$pattern}/{$this->flags}"; | ||
|
||
return preg_match( | ||
"/{$this->prefix_regex}{$quantifier}{$pattern}/{$this->flags}", | ||
$text, | ||
$matches | ||
); | ||
return preg_match($pattern, $text, $matches) === 1; | ||
} | ||
|
||
} |
Oops, something went wrong.