Skip to content

Commit

Permalink
Merge pull request #47 from miamibc/flip-plugin
Browse files Browse the repository at this point in the history
Advice plugin improvements
  • Loading branch information
miamibc authored Dec 27, 2021
2 parents 4cd0bb2 + e013625 commit a0969d0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
7 changes: 6 additions & 1 deletion joker.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@

new Joker\Plugin\Server( ['host' => '127.0.0.1', 'port' => 5566] ),
new Joker\Plugin\Temp( ['api_key' => getenv( 'OPENWEATHER_API_KEY' ),'default' => 'Tallinn'] ),
new Joker\Plugin\Advice(),
new Joker\Plugin\Advice([
'random_time' => 60*60, // time condition (one advice per hour)
'random_ticks' => 5, // tick condition (5 messages in last minute)
'random_chance' => .33, // random chance (33%)
'random_delay' => 5, // random advice delay
]),
new Joker\Plugin\Flip(),
new Joker\Plugin\Vkmusic(),
new Joker\Plugin\Ytmusic( ['api_key' => getenv('GOOGLE_API_KEY')]),
Expand Down
57 changes: 40 additions & 17 deletions src/Plugin/Advice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
* !advice wrongtopic
* bot will answer with list of proper topics
*
* Options:
* - `random_time` (int, default 360) - seconds between random advices
* - `random_ticks` (int, default 5) - chat activity, number of messages per last minute
* - `random_chance` (float, default .33) - random chance
* - `random_delay` (int, default 5) - delay before message will be sent
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/

namespace Joker\Plugin;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use Joker\Helper\Tickometer;
use Joker\Helper\Timer;
use Joker\Parser\Update;
Expand All @@ -29,6 +36,13 @@ class Advice extends Base
const RANDOM_ENDPOINT = 'https://fucking-great-advice.ru/api/v2/random-advices';
const CATEGORY_ENDPOINT = 'https://fucking-great-advice.ru/api/v2/random-advices-by-tag';

protected $options = [
'random_time' => 60*60, // time condition (one advice per hour)
'random_ticks' => 5, // tick condition (5 messages in last minute)
'random_chance' => .33, // random chance (33%)
'random_delay' => 5, // random advice delay
];

private
$tags = [], // list of topics
$advices = [], // advices caches
Expand All @@ -51,7 +65,7 @@ public function __construct($options = [])

// initialize http client
$this->client = new Client([
'timeout' => 2.0,
'timeout' => 5.0,
'headers' => [
'Referer' => 'https://fucking-great-advice.ru/',
'Accept' => 'application/json',
Expand All @@ -60,11 +74,16 @@ public function __construct($options = [])
]);

// request information about tags
$response = $this->client->get(self::TAGS_ENDPOINT);
$body = json_decode($response->getBody(), true);
foreach ($body['data'] as $item)
try
{
$this->tags[ $item['alias'] ] ="{$item['title']}, {$item['advicesCount']} advices";
$response = $this->client->get(self::TAGS_ENDPOINT);
$body = json_decode($response->getBody(),true);
foreach ($body['data'] as $item)
{
$this->tags[$item['alias']] = "{$item['title']}, {$item['advicesCount']} advices";
}
} catch (ConnectException $exception){
/** nothing to do */
}

}
Expand All @@ -89,13 +108,14 @@ public function onPublicText( Update $update )

// random advice, if we pass some checks
if (
time()-$this->last >= 60 * 10 // time condition (one advice in 10 minutes)
&& $this->tickometer->count() >= 5 // tick condition (5 messages in last minute)
&& $this->randomFloat() < .33 // random chance (33%)
isset($this->tags['']) // tags is loaded
&& time()-$this->last >= $this->getOption('random_time')
&& $this->tickometer->count() >= $this->getOption('random_ticks')
&& $this->randomFloat() <= $this->getOption('random_chance')
){
// send with 3 seconds delay
// send with delay
$advice = $this->getAdvice();
$this->timer->add(3, function () use ($update, $advice) {
$this->timer->add( $this->getOption('random_delay'), function () use ($update, $advice) {
$update->answerMessage( $advice );
});
$this->last = time();
Expand Down Expand Up @@ -125,7 +145,6 @@ public function randomFloat($min = 0, $max = 1)
* @param string $topic
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getAdvice($topic = '')
{
Expand All @@ -145,12 +164,16 @@ public function getAdvice($topic = '')
// load few advices from sever
if (!isset( $this->advices[$topic] ) || empty( $this->advices[$topic]))
{
$request = empty($topic)
? $this->client->get(self::RANDOM_ENDPOINT)
: $this->client->get(self::CATEGORY_ENDPOINT, ['query'=> ['tag' => $topic]])
;
$body = json_decode($request->getBody(),true);

try
{
$request = empty($topic)
? $this->client->get(self::RANDOM_ENDPOINT)
: $this->client->get(self::CATEGORY_ENDPOINT,['query' => ['tag' => $topic]]);
$body = json_decode($request->getBody(),true);
}
catch (ConnectException $e){
/* nothing to do */
}
// no advices came in
if (!isset($body['data']) || empty( $body['data']))
{
Expand Down
6 changes: 6 additions & 0 deletions src/Plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ You can ask:

Also, bot sends advices randomly from time to time, depending on users activity, luck and time delay. Here we implemented our new helpers [Timer](/miamibc/joker-telegram-bot/blob/master/src/Helper/Timer.php) and [Tickometer](/miamibc/joker-telegram-bot/blob/master/src/Helper/Tickometer.php) for first time (description will be added later).

Options:
- `random_time` (int, default 360) - seconds between random advices
- `random_ticks` (int, default 5) - activity needed to produce random advice (messages per minute)
- `random_chance` (float, default .33) - chance of random advice
- `random_delay` (int, default 5) - delay before random advice will be sent

Thanks for idea [D0b3rm4nN](https://gist.github.com/bcdober)

## Bash Plugin
Expand Down

0 comments on commit a0969d0

Please sign in to comment.