-
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.
Merge pull request #44 from miamibc/ignore-plugin
Ignore plugin
- Loading branch information
Showing
10 changed files
with
252 additions
and
9 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
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
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,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]; | ||
} | ||
} | ||
} | ||
|
||
} |
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
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,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; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -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(); | ||
|
||
} | ||
|
||
|
||
} |
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