Skip to content

Commit

Permalink
Merge pull request #44 from miamibc/ignore-plugin
Browse files Browse the repository at this point in the history
Ignore plugin
  • Loading branch information
miamibc authored Nov 25, 2021
2 parents 5b2c5d3 + bfc0585 commit b4842c0
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 9 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"gabordemooij/redbean": "dev-master",
"cheprasov/php-redis-client": "^1.10",
"mnapoli/silly": "^1.7",
"guzzlehttp/guzzle": "^7.3"
"guzzlehttp/guzzle": "^7.3",
"imangazaliev/didom": "^1.18"
},
"autoload": {
"psr-4": {
Expand All @@ -46,4 +47,4 @@
"test": "php vendor/bin/phpunit",
"test-github": "php vendor/bin/phpunit --testdox --exclude-group=exclude-from-github-test"
}
}
}
3 changes: 2 additions & 1 deletion joker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
['from' => -343502518, 'text' => ['*покуп*'], 'to' => -343502518, ],
['from' => -343502518, 'text' => ['*прода*', '*сдаё*'], 'to' => -343502518, 'forward' => false ],
]),
new Joker\Plugin\Ignore(),
new Joker\Plugin\UrlCollector(['file' => 'data/urls.txt']),
new Joker\Plugin\Viabot(),
// new Joker\Plugin\Viabot(), // TODO: not necessary anymore, will be removed soon

// *** insert your plugins here, order is important ***

Expand Down
3 changes: 2 additions & 1 deletion src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public function loop()
// if empty buffer, request updates
if (empty($this->buffer))
{
foreach ($this->getUpdates($this->last_update_id) as $item)
if (!$updates = $this->getUpdates($this->last_update_id)) $updates = [];
foreach ($updates as $item)
{
$this->buffer[] = new Update( $item, $this );
$this->last_update_id = $item['update_id'] + 1;
Expand Down
47 changes: 47 additions & 0 deletions src/Helper/Interval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Interval for doing things with interval
*
* - Add tasks (callable, this can be anonymous function)
* - Run periodically
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/
namespace Joker\Helper;

class Interval
{

private $jobs = [];

/**
* Add a job
* @param int $delay Delay in seconds
* @param callable $job function(){ ... }
*/
public function add( int $delay, callable $job )
{
$this->jobs[] = [ $delay, $job, time()+$delay ];
}

/**
* Run timer
* Proper tasks will be executed and removed
*/
public function run()
{
$time = time();
foreach ($this->jobs as $job)
{
list($delay, $callable, $alarma) = $job;
if ($time >= $alarma)
{
call_user_func( $callable );
$this->jobs[] = [ $delay, $job, time()+$delay];
}
}
}

}
34 changes: 34 additions & 0 deletions src/Helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,38 @@ class MyPlugin extends Base
}
```

## Stemmer/Lemmer

To-do.

Sources:
- https://github.com/neonxp/Stemmer/
- https://github.com/ladamalina/php-lingua-stem-ru
- https://github.com/andyceo/PHP-Porter-Stemmer
- https://github.com/wamania/php-stemmer
- https://yandex.ru/dev/mystem/
- https://github.com/iskander-akhmetov/Highly-Language-Independent-Word-Lemmatization-Using-a-Machine-Learning-Classifier/tree/master/DS_lemm
- https://nlpub.ru/%D0%A0%D0%B5%D1%81%D1%83%D1%80%D1%81%D1%8B
- http://opencorpora.org/
- http://www.solarix.ru/
-
## Rus to lat

Good one, found in internets...

```php
function rus2lat($string){
$rus = array('ё','ж','ц','ч','ш','щ','ю','я','Ё','Ж','Ц','Ч','Ш','Щ','Ю','Я','Ъ','Ь','ъ','ь');
$lat = array('e','zh','c','ch','sh','sh','ju','ja','E','ZH','C','CH','SH','SH','JU','JA','','','','');
$string = str_replace($rus,$lat,$string);
$string = strtr($string,
"АБВГДЕЗИЙКЛМНОПРСТУФХЫЭабвгдезийклмнопрстуфхыэ",
"ABVGDEZIJKLMNOPRSTUFHIEabvgdezijklmnoprstufhie");
return $string;
}
```

## FAQ

https://github.com/Koziev/chatbot
https://github.com/Koziev/chatbot/blob/master/data/faq2.txt
5 changes: 3 additions & 2 deletions src/Helper/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function run()
$time = time();
foreach ($this->jobs as $k=>$job)
{
if ($time > $job[0] && is_callable($job[1]))
list($alarma, $callable) = $job;
if ($time >= $alarma)
{
call_user_func( $job[1] );
call_user_func( $callable );
unset($this->jobs[$k]);
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/Plugin/Ignore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Ignore plugin for Joker
*
* Adds ability to be ignored in processing all incoming events.
* Additionally, ignored all messages sent via bot.
*
* To be ignored say:
* !ignore
*
* To be unignored, say:
* !unignore
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/

namespace Joker\Plugin;

use Joker\Parser\Update;

class Ignore extends Base
{

public function onViabot( Update $update )
{
return false;
}

public function onText( Update $update )
{
$trigger = $update->message()->text()->trigger();
if (!in_array($trigger, ['ignore','unignore'])) return;

$user = $update->message()->from();
$custom = $user->getCustom();
$custom->ignore = ($trigger === 'ignore');
$user->saveCustom();
$update->answerMessage("$user is {$trigger}d");
return false;
}

public function onMessage( Update $update )
{
if ($update->message()->from()->getCustom()->ignore) return false;
}

}
6 changes: 3 additions & 3 deletions src/Plugin/Kicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function onJoin( Update $update )
// send greeting
if ($greeting = $this->getOption( "greeting_{$with_or_without}_emoji"))
{
$chat->sendMessage(strtr($greeting,['%name%' => $user]));
$chat->sendMessage(strtr($greeting,['%name%' => $user->name()]));
}

// add to waiting list
Expand All @@ -79,7 +79,7 @@ public function onPublicText( Update $update )
unset($this->waiting_list[$id]);

// send greeting_not_bot
$greeting = strtr( $this->getOption('greeting_not_bot'), ['%name%'=>$user]);
$greeting = strtr( $this->getOption('greeting_not_bot'), ['%name%'=>$user->name()]);
if ($greeting) $update->answerMessage( $greeting );
}

Expand All @@ -105,7 +105,7 @@ public function onEmpty( Update $update)
unset($this->waiting_list[$index]);

// send greeting_is_bot
$greeting = strtr( $this->getOption('greeting_is_bot'), ['%name%'=>$user]);
$greeting = strtr( $this->getOption('greeting_is_bot'), ['%name%'=>$user->name()]);
if ($greeting) $chat->sendMessage( $greeting );

// ban user from chat
Expand Down
92 changes: 92 additions & 0 deletions src/Plugin/Koldun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Koldun plugin for Joker
* Performs search, founds "zero result" in google and yandex, and extracts data from it.
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/

namespace Joker\Plugin;

use DiDom\Document;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use Joker\Parser\Update;

class Koldun extends Base
{

private $client; // http client
protected $options =[
'triggers' => ['сколько', "что", "как", "где", "почему", "когда", "кому", "зачем"],
];

public function __construct($options = [])
{
parent::__construct($options);

// initialize http client
$this->client = new Client([
'timeout' => 2.0,
'headers' => [
'Referer' => 'https://fucking-great-advice.ru/',
'Accept' => 'application/json',
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0',
],
]);

}

public function onPublicText( Update $update )
{
$text = $update->message()->text();
if (!in_array( $text->trigger(), $this->getOption('triggers'))) return;

$google = $this->client->getAsync('https://www.google.com/search?' . http_build_query(['q'=>(string)$text]) );
$update->bot()->console( "Google started");
$google->then(
function (ResponseInterface $res) use ($update){
$update->bot()->console( "Google status " . $res->getStatusCode() );
file_put_contents("data/google.html", $html = $res->getBody());
if (!preg_match('@<div class="\w+" data-attrid="wa:\/description".*<\/div>@iU', $html )) return;
$didom = new Document("<body>$html</body>");
$text = $didom->first('div[data-attrid="wa:/description"]')->text();
$update->answerMessage("Google said: $text");
},
function (RequestException $e) use ($update) {
$update->bot()->console( "Google error " . $e->getMessage() );
}
);
/*
// $yandex = $this->client->getAsync('https://yandex.ru/search/?' . http_build_query(['lr' => 11481, 'text'=>(string)$text, ]) );
$yandex = $this->client->getAsync('https://yandex.ru/search/xml?' . http_build_query([
'user' => 'miamibc',
'key'=>'03.32681275:2af78167ff7d380aad89e871dd22f708',
'query'=>(string)$text,
]));
$update->bot()->console( "Yandex started");
$yandex->then(
function (ResponseInterface $res) use ($update){
$update->bot()->console( "Yandex status " . $res->getStatusCode() );
file_put_contents("data/yandex.html", $res->getBody());
$didom = new Document($res->getBody());
$text = $didom->first('div.fact-answer]')->text();
$update->answerMessage("Google said: $text");
},
function (RequestException $e) use ($update) {
$update->bot()->console( "Yandex error " . $e->getMessage() );
}
);
*/


$google->wait();
// $yandex->wait();

}


}
18 changes: 18 additions & 0 deletions src/Plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Plugins are well documented in inline comments, some interesting details will be
* [Forwarder Plugin](#forwarder-plugin)
* [Game Plugin](#game-plugin)
* [Hello Plugin](#hello-plugin)
* [Ignore Plugin](#ignore-plugin)
* [Kicker Plugin](#kicker-plugin)
* [Log Plugin](#log-plugin)
* [Lurk Plugin](#lurk-plugin)
Expand Down Expand Up @@ -288,6 +289,23 @@ Bot will answer you with standart greeting

Read more: https://github.com/miamibc/joker-telegram-bot

## Ignore Plugin

Adds ability to be ignored in processing all incoming events.
Additionally, ignored all messages sent via bot.

To be ignored say:
```
!ignore
```

To be unignored, say:
```
!unignore
```

Thanks to **Roboromat** for the idea.

## Kicker Plugin

If your channel is popular enough, you will constantly be attacked with bots with strange names containing emoji.
Expand Down

0 comments on commit b4842c0

Please sign in to comment.