Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
merge done
Browse files Browse the repository at this point in the history
  • Loading branch information
laendoor committed Aug 17, 2017
2 parents 1c39dfc + 9839d2e commit d470172
Show file tree
Hide file tree
Showing 64 changed files with 2,464 additions and 1,347 deletions.
66 changes: 66 additions & 0 deletions app/Espinoso/BrainNode.php
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;
}

}
68 changes: 68 additions & 0 deletions app/Espinoso/Espinoso.php
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();
// }

}
30 changes: 18 additions & 12 deletions app/Espinoso/Handlers/BardoDelEspinosoHandler.php
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!'
]);
}
}
}
95 changes: 95 additions & 0 deletions app/Espinoso/Handlers/BrainHandler.php
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 ;
// }

}
24 changes: 13 additions & 11 deletions app/Espinoso/Handlers/CinemaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

use Illuminate\Support\Str;
use App\Facades\GoutteClient;
use Telegram\Bot\Laravel\Facades\Telegram;
use Telegram\Bot\Objects\Message;

class CinemaHandler extends EspinosoCommandHandler
{
public function shouldHandle($updates, $context = null)
{
return parent::shouldHandle($updates, $context)
&& $this->matchCommand('.*\bcine\b.*', $updates);
}
/**
* @var string
*/
protected $pattern = ".{0,100}\b(cine)\b.{0,100}$";

protected $signature = "espi cine";
protected $description = "te muestro que hay para ver en el cine y ponerla";

public function handle($updates, $context = null)
public function handle(Message $message)
{
$crawler = GoutteClient::request('GET', config('espinoso.url.cinema'));

Expand All @@ -25,14 +27,14 @@ public function handle($updates, $context = null)
return " - {$movie}";
})->implode("\n");

$message = "¿La pensás poner?
$response = "¿La pensás poner?
¡Mete Netflix pelotud@, es mas barato!
Pero igual podes ver todas estas:\n
{$movies}";

Telegram::sendMessage([
'chat_id' => $updates->message->chat->id,
'text' => $message,
$this->telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' => $response,
]);
}
}
32 changes: 22 additions & 10 deletions app/Espinoso/Handlers/EspinosoCommandHandler.php
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;
}

}
Loading

0 comments on commit d470172

Please sign in to comment.