Skip to content

Commit a0969d0

Browse files
authored
Merge pull request #47 from miamibc/flip-plugin
Advice plugin improvements
2 parents 4cd0bb2 + e013625 commit a0969d0

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

joker.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333

3434
new Joker\Plugin\Server( ['host' => '127.0.0.1', 'port' => 5566] ),
3535
new Joker\Plugin\Temp( ['api_key' => getenv( 'OPENWEATHER_API_KEY' ),'default' => 'Tallinn'] ),
36-
new Joker\Plugin\Advice(),
36+
new Joker\Plugin\Advice([
37+
'random_time' => 60*60, // time condition (one advice per hour)
38+
'random_ticks' => 5, // tick condition (5 messages in last minute)
39+
'random_chance' => .33, // random chance (33%)
40+
'random_delay' => 5, // random advice delay
41+
]),
3742
new Joker\Plugin\Flip(),
3843
new Joker\Plugin\Vkmusic(),
3944
new Joker\Plugin\Ytmusic( ['api_key' => getenv('GOOGLE_API_KEY')]),

src/Plugin/Advice.php

+40-17
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111
* !advice wrongtopic
1212
* bot will answer with list of proper topics
1313
*
14+
* Options:
15+
* - `random_time` (int, default 360) - seconds between random advices
16+
* - `random_ticks` (int, default 5) - chat activity, number of messages per last minute
17+
* - `random_chance` (float, default .33) - random chance
18+
* - `random_delay` (int, default 5) - delay before message will be sent
19+
*
1420
* @package joker-telegram-bot
1521
* @author Sergei Miami <[email protected]>
1622
*/
1723

1824
namespace Joker\Plugin;
1925

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

39+
protected $options = [
40+
'random_time' => 60*60, // time condition (one advice per hour)
41+
'random_ticks' => 5, // tick condition (5 messages in last minute)
42+
'random_chance' => .33, // random chance (33%)
43+
'random_delay' => 5, // random advice delay
44+
];
45+
3246
private
3347
$tags = [], // list of topics
3448
$advices = [], // advices caches
@@ -51,7 +65,7 @@ public function __construct($options = [])
5165

5266
// initialize http client
5367
$this->client = new Client([
54-
'timeout' => 2.0,
68+
'timeout' => 5.0,
5569
'headers' => [
5670
'Referer' => 'https://fucking-great-advice.ru/',
5771
'Accept' => 'application/json',
@@ -60,11 +74,16 @@ public function __construct($options = [])
6074
]);
6175

6276
// request information about tags
63-
$response = $this->client->get(self::TAGS_ENDPOINT);
64-
$body = json_decode($response->getBody(), true);
65-
foreach ($body['data'] as $item)
77+
try
6678
{
67-
$this->tags[ $item['alias'] ] ="{$item['title']}, {$item['advicesCount']} advices";
79+
$response = $this->client->get(self::TAGS_ENDPOINT);
80+
$body = json_decode($response->getBody(),true);
81+
foreach ($body['data'] as $item)
82+
{
83+
$this->tags[$item['alias']] = "{$item['title']}, {$item['advicesCount']} advices";
84+
}
85+
} catch (ConnectException $exception){
86+
/** nothing to do */
6887
}
6988

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

90109
// random advice, if we pass some checks
91110
if (
92-
time()-$this->last >= 60 * 10 // time condition (one advice in 10 minutes)
93-
&& $this->tickometer->count() >= 5 // tick condition (5 messages in last minute)
94-
&& $this->randomFloat() < .33 // random chance (33%)
111+
isset($this->tags['']) // tags is loaded
112+
&& time()-$this->last >= $this->getOption('random_time')
113+
&& $this->tickometer->count() >= $this->getOption('random_ticks')
114+
&& $this->randomFloat() <= $this->getOption('random_chance')
95115
){
96-
// send with 3 seconds delay
116+
// send with delay
97117
$advice = $this->getAdvice();
98-
$this->timer->add(3, function () use ($update, $advice) {
118+
$this->timer->add( $this->getOption('random_delay'), function () use ($update, $advice) {
99119
$update->answerMessage( $advice );
100120
});
101121
$this->last = time();
@@ -125,7 +145,6 @@ public function randomFloat($min = 0, $max = 1)
125145
* @param string $topic
126146
*
127147
* @return string
128-
* @throws \GuzzleHttp\Exception\GuzzleException
129148
*/
130149
public function getAdvice($topic = '')
131150
{
@@ -145,12 +164,16 @@ public function getAdvice($topic = '')
145164
// load few advices from sever
146165
if (!isset( $this->advices[$topic] ) || empty( $this->advices[$topic]))
147166
{
148-
$request = empty($topic)
149-
? $this->client->get(self::RANDOM_ENDPOINT)
150-
: $this->client->get(self::CATEGORY_ENDPOINT, ['query'=> ['tag' => $topic]])
151-
;
152-
$body = json_decode($request->getBody(),true);
153-
167+
try
168+
{
169+
$request = empty($topic)
170+
? $this->client->get(self::RANDOM_ENDPOINT)
171+
: $this->client->get(self::CATEGORY_ENDPOINT,['query' => ['tag' => $topic]]);
172+
$body = json_decode($request->getBody(),true);
173+
}
174+
catch (ConnectException $e){
175+
/* nothing to do */
176+
}
154177
// no advices came in
155178
if (!isset($body['data']) || empty( $body['data']))
156179
{

src/Plugin/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ You can ask:
6464

6565
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).
6666

67+
Options:
68+
- `random_time` (int, default 360) - seconds between random advices
69+
- `random_ticks` (int, default 5) - activity needed to produce random advice (messages per minute)
70+
- `random_chance` (float, default .33) - chance of random advice
71+
- `random_delay` (int, default 5) - delay before random advice will be sent
72+
6773
Thanks for idea [D0b3rm4nN](https://gist.github.com/bcdober)
6874

6975
## Bash Plugin

0 commit comments

Comments
 (0)