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

Commit d470172

Browse files
committed
merge done
2 parents 1c39dfc + 9839d2e commit d470172

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2464
-1347
lines changed

app/Espinoso/BrainNode.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php namespace App\Espinoso;
2+
3+
use Illuminate\Support\Collection;
4+
use Telegram\Bot\Objects\Message;
5+
use Telegram\Bot\Objects\User as TelegramUser;
6+
7+
/**
8+
* Class BrainNode
9+
* @package App\Espinoso
10+
*/
11+
class BrainNode
12+
{
13+
protected $regex;
14+
protected $reply;
15+
protected $match;
16+
protected $matches;
17+
protected $ignored;
18+
19+
public function __construct(string $regex, array $data = [])
20+
{
21+
$this->regex = $regex;
22+
$this->reply = $data['reply'] ?? '';
23+
$this->ignored = collect($data['ignored'] ?? []);
24+
}
25+
26+
public function matchMessage(Message $message)
27+
{
28+
$this->match = preg_match($this->regex, $message->getText(), $this->matches) === 1;
29+
30+
return !empty($this->reply)
31+
&& $this->shouldResponseTo($message->getFrom())
32+
&& $this->match;
33+
}
34+
35+
public function pickReply(Message $message)
36+
{
37+
return is_array($this->reply)
38+
? $this->pickFromBag($message)
39+
: $this->reply;
40+
}
41+
42+
public function addIgnored(Collection $ignored) {
43+
$this->ignored->merge($ignored);
44+
}
45+
46+
protected function shouldResponseTo(TelegramUser $from)
47+
{
48+
// TODO
49+
return true;
50+
}
51+
52+
protected function pickFromBag(Message $message)
53+
{
54+
// FIXME: make a better behavior than simple random
55+
$number = rand(0, count($this->reply) - 1);
56+
57+
$reply = $this->reply[$number];
58+
59+
if (str_contains($reply, ':name:')) {
60+
$reply = str_replace(':name:', $message->getFrom()->getFirstName(), $reply);
61+
}
62+
63+
return $reply;
64+
}
65+
66+
}

app/Espinoso/Espinoso.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php namespace App\Espinoso;
2+
3+
use Exception;
4+
use Illuminate\Support\Collection;
5+
use Telegram\Bot\Objects\Message;
6+
use Telegram\Bot\Api as ApiTelegram;
7+
use App\Espinoso\Handlers\EspinosoHandler;
8+
9+
/**
10+
* Class Espinoso
11+
* @package Espinoso
12+
*/
13+
class Espinoso
14+
{
15+
/**
16+
* @var array
17+
*/
18+
protected $handlers;
19+
20+
public function __construct(Collection $handlers)
21+
{
22+
$this->handlers = $handlers;
23+
}
24+
25+
/**
26+
* @param ApiTelegram $telegram
27+
* @param Message $message
28+
*/
29+
public function executeHandlers(ApiTelegram $telegram, Message $message)
30+
{
31+
$this->getHandlers()->map(function ($handler) use ($telegram) {
32+
return new $handler($this, $telegram);
33+
})->filter(function (EspinosoHandler $handler) use ($message) {
34+
return $handler->shouldHandle($message);
35+
})->each(function (EspinosoHandler $handler) use ($message) {
36+
try {
37+
$handler->handle($message);
38+
} catch (Exception $e) {
39+
$handler->handleError($e, $message);
40+
}
41+
});
42+
}
43+
44+
/**
45+
* @return Collection
46+
*/
47+
public function getHandlers(): Collection
48+
{
49+
return $this->handlers;
50+
}
51+
52+
// public function register(stdClass $update)
53+
// {
54+
// $from = $update->message->from;
55+
//
56+
// $user = TelegramUser::whereTelegramId($from->id)->first();
57+
// if (!$user) {
58+
// $user = new TelegramUser;
59+
// $user->telegram_id = $from->id;
60+
// }
61+
//
62+
// $user->first_name = $from->first_name ?? '';
63+
// $user->last_name = $from->last_name ?? '';
64+
// $user->username = $from->username ?? '';
65+
// $user->save();
66+
// }
67+
68+
}
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
<?php namespace App\Espinoso\Handlers;
22

3-
use Telegram\Bot\Laravel\Facades\Telegram;
3+
use Telegram\Bot\Objects\Message;
44

5-
class BardoDelEspinosoHandler extends EspinosoHandler
5+
class BardoDelEspinosoHandler extends EspinosoCommandHandler
66
{
7-
public function shouldHandle($updates, $context = null)
8-
{
9-
return $this->isTextMessage($updates)
10-
&& preg_match('/^send me nudes$/i', $updates->message->text) ;
11-
}
7+
/**
8+
* @var string
9+
*/
10+
protected $allow_ignore_prefix = true;
11+
/**
12+
* @var string
13+
*/
14+
protected $pattern = "send me nudes$";
15+
16+
protected $signature = "[espi] send me nudes";
17+
protected $description = "no sé, fijate";
1218

13-
public function handle($updates, $context = null)
19+
public function handle(Message $message)
1420
{
15-
return Telegram::sendPhoto([
16-
'chat_id' => $updates->message->chat->id,
21+
return $this->telegram->sendPhoto([
22+
'chat_id' => $message->getChat()->getId(),
1723
'photo' => 'https://cdn.drawception.com/images/panels/2012/4-4/FErsE1a6t7-8.png',
18-
'caption' => 'Acá tenés tu nude, puto del orto!'
24+
'caption' => 'Acá tenés tu nude, hijo de puta!'
1925
]);
2026
}
21-
}
27+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php namespace App\Espinoso\Handlers;
2+
3+
use App\Espinoso\Espinoso;
4+
use App\Espinoso\BrainNode;
5+
use Telegram\Bot\Objects\Message;
6+
use Telegram\Bot\Api as ApiTelegram;
7+
8+
class BrainHandler extends EspinosoHandler
9+
{
10+
protected $allNodes;
11+
protected $matchedNodes;
12+
13+
protected $signature = "macri, facu, ine, alan, asado, ...";
14+
protected $description = "Macri Gato, Facu Puto";
15+
16+
public function __construct(Espinoso $espinoso, ApiTelegram $telegram)
17+
{
18+
parent::__construct($espinoso, $telegram);
19+
20+
$this->matchedNodes = collect([]);
21+
$this->allNodes = collect(config('brain.patterns'))->map(function ($data, $regex) {
22+
return new BrainNode($regex, $data);
23+
});
24+
}
25+
26+
public function shouldHandle(Message $message): bool
27+
{
28+
$this->matchedNodes = $this->allNodes->filter(function ($node) use ($message) {
29+
$node->addIgnored($this->globalIgnored());
30+
return $node->matchMessage($message);
31+
});
32+
33+
return $this->matchedNodes->isNotEmpty();
34+
}
35+
36+
public function handle(Message $message)
37+
{
38+
$this->matchedNodes->each(function (BrainNode $node) use ($message) {
39+
$this->telegram->sendMessage([
40+
'chat_id' => $message->getChat()->getId(),
41+
'text' => $node->pickReply($message),
42+
'parse_mode' => 'Markdown'
43+
]);
44+
});
45+
}
46+
47+
/*
48+
* Internals
49+
*/
50+
51+
protected function globalIgnored()
52+
{
53+
return collect(config('brain.ignore_to'));
54+
}
55+
56+
// public function handle(Message $message)
57+
// {
58+
// if ($this->ignoringSender($message->getFrom())) {
59+
// $fromName = $message->getFrom()->getFirstName();
60+
// $msg = Msg::md("Con vos no hablo porque no viniste al asado $fromName")->build($message);
61+
// $this->telegram->sendMessage($msg);
62+
// return;
63+
// }
64+
//
65+
// foreach ($this->mappings() as $pattern => $response) {
66+
// if ( preg_match($pattern, $message->getText()) ) {
67+
// $msg = $this->buildMessage($response, $pattern, $message);
68+
// $this->telegram->sendMessage($msg);
69+
// }
70+
// }
71+
// }
72+
73+
// private function buildMessage($response, $pattern, Message $message)
74+
// {
75+
// if ($response instanceof Msg)
76+
// return $response->build($message, $pattern);
77+
// else
78+
// return Msg::plain($response)->build($message, $pattern);
79+
// }
80+
//
81+
// private function mappings()
82+
// {
83+
// return config('espinoso_data.ResponseByMatch.mappings');
84+
// }
85+
//
86+
87+
// private function ignoringSender($sender)
88+
// {
89+
// foreach ($this->ignoredNames() as $name)
90+
// if ( preg_match("/$name/i", $sender->first_name) )
91+
// return true ;
92+
// return false ;
93+
// }
94+
95+
}

app/Espinoso/Handlers/CinemaHandler.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
use Illuminate\Support\Str;
44
use App\Facades\GoutteClient;
5-
use Telegram\Bot\Laravel\Facades\Telegram;
5+
use Telegram\Bot\Objects\Message;
66

77
class CinemaHandler extends EspinosoCommandHandler
88
{
9-
public function shouldHandle($updates, $context = null)
10-
{
11-
return parent::shouldHandle($updates, $context)
12-
&& $this->matchCommand('.*\bcine\b.*', $updates);
13-
}
9+
/**
10+
* @var string
11+
*/
12+
protected $pattern = ".{0,100}\b(cine)\b.{0,100}$";
13+
14+
protected $signature = "espi cine";
15+
protected $description = "te muestro que hay para ver en el cine y ponerla";
1416

15-
public function handle($updates, $context = null)
17+
public function handle(Message $message)
1618
{
1719
$crawler = GoutteClient::request('GET', config('espinoso.url.cinema'));
1820

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

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

33-
Telegram::sendMessage([
34-
'chat_id' => $updates->message->chat->id,
35-
'text' => $message,
35+
$this->telegram->sendMessage([
36+
'chat_id' => $message->getChat()->getId(),
37+
'text' => $response,
3638
]);
3739
}
3840
}
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
<?php namespace App\Espinoso\Handlers;
22

3+
use Telegram\Bot\Objects\Message;
4+
35
abstract class EspinosoCommandHandler extends EspinosoHandler
46
{
57
protected $flags = 'i';
68
protected $prefix_regex = "^(?'e'espi(noso)?\s+)"; // 'espi|espinoso '
9+
protected $pattern = '$';
10+
protected $matches = [];
711
/**
812
* @var bool
913
* If false, should match 'espi'
1014
* If true, could not match 'espi'
1115
*/
1216
protected $allow_ignore_prefix = false;
1317

18+
/**
19+
* Default behavior to determine is Command handler should response the message.
20+
*
21+
* @param Message $message
22+
* @return bool
23+
*/
24+
public function shouldHandle(Message $message): bool
25+
{
26+
return $this->matchCommand($this->pattern, $message, $this->matches);
27+
}
28+
1429
/**
1530
* @param $pattern
16-
* @param $updates
31+
* @param Message $message
1732
* @param array|null $matches
18-
* @return int
33+
* @return bool
1934
*/
20-
protected function matchCommand($pattern, $updates, array &$matches = null)
35+
protected function matchCommand($pattern, Message $message, array &$matches = null): bool
2136
{
22-
$quantifier = $this->allow_ignore_prefix ? '?' : '{1,3}';
23-
$text = $this->isTextMessage($updates) ? $updates->message->text : '';
37+
$quantifier = $this->allow_ignore_prefix ? '{0,3}' : '{1,3}';
38+
$text = $message->getText();
39+
$pattern = "/{$this->prefix_regex}{$quantifier}{$pattern}/{$this->flags}";
2440

25-
return preg_match(
26-
"/{$this->prefix_regex}{$quantifier}{$pattern}/{$this->flags}",
27-
$text,
28-
$matches
29-
);
41+
return preg_match($pattern, $text, $matches) === 1;
3042
}
3143

3244
}

0 commit comments

Comments
 (0)