Skip to content

Commit fab9951

Browse files
authored
Merge pull request #34 from nasserghiasi/master
[URGENT] BUGFIX: trim don't accept null argument
2 parents 285a64c + 933084b commit fab9951

File tree

3 files changed

+106
-32
lines changed

3 files changed

+106
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
55

66
## [Unreleased]
77
### Added
8+
- Custom output callback can be defined for getUpdates method.
89
### Changed
10+
- Default output of getUpdates method now shows the message type or query text, not the text message content.
911
### Deprecated
1012
### Removed
1113
### Fixed
14+
- GetUpdates method would crash if a non-text message was sent.
1215
### Security
1316

1417
## [1.1.0] - 2017-05-23

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,43 @@ $bot = new BotManager([
325325

326326
Now, the updates can be done either through the [browser](#via-browser) or [via CLI](#via-cli).
327327

328+
#### Custom getUpdates output
329+
330+
A callback can be defined, to override the default output when updates are handled via getUpdates.
331+
332+
Example of the default output:
333+
```
334+
...
335+
2017-07-10 14:59:25 - Updates processed: 1
336+
123456: <text>
337+
2017-07-10 14:59:27 - Updates processed: 0
338+
2017-07-10 14:59:30 - Updates processed: 0
339+
2017-07-10 14:59:32 - Updates processed: 0
340+
2017-07-10 14:59:34 - Updates processed: 1
341+
123456: <photo>
342+
2017-07-10 14:59:36 - Updates processed: 0
343+
...
344+
```
345+
346+
Using custom callback that must return a string:
347+
```php
348+
// In manager.php after $bot has been defined:
349+
$bot->setCustomGetUpdatesCallback(function (ServerResponse $get_updates_response) {
350+
$results = array_filter((array) $get_updates_response->getResult());
351+
352+
return sprintf('There are %d update(s)' . PHP_EOL, count($results));
353+
});
354+
```
355+
output:
356+
```
357+
...
358+
There are 0 update(s)
359+
There are 0 update(s)
360+
There are 2 update(s)
361+
There are 1 update(s)
362+
...
363+
```
364+
328365
## Development
329366

330367
When running live bot tests on a fork, you must enter the following environment variables to your [repository settings][travis-repository-settings] on travis-ci.org:

src/BotManager.php

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class BotManager
4545
*/
4646
private $action;
4747

48+
/**
49+
* @var callable
50+
*/
51+
private $custom_get_updates_callback;
52+
4853
/**
4954
* BotManager constructor.
5055
*
@@ -431,6 +436,19 @@ public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interv
431436
return $this;
432437
}
433438

439+
/**
440+
* Set a custom callback for handling the output of the getUpdates results.
441+
*
442+
* @param callable $callback
443+
*
444+
* @return \TelegramBot\TelegramBotManager\BotManager
445+
*/
446+
public function setCustomGetUpdatesCallback(callable $callback): BotManager
447+
{
448+
$this->custom_get_updates_callback = $callback;
449+
return $this;
450+
}
451+
434452
/**
435453
* Handle the updates using the getUpdates method.
436454
*
@@ -439,45 +457,61 @@ public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interv
439457
*/
440458
public function handleGetUpdates(): self
441459
{
442-
$output = date('Y-m-d H:i:s', time()) . ' - ';
443-
444-
$response = $this->telegram->handleGetUpdates();
445-
if ($response->isOk()) {
446-
$results = array_filter((array) $response->getResult());
447-
448-
$output .= sprintf('Updates processed: %d' . PHP_EOL, count($results));
449-
450-
/** @var Entities\Update $result */
451-
foreach ($results as $result) {
452-
$chat_id = 0;
453-
$text = 'Nothing';
454-
455-
$update_content = $result->getUpdateContent();
456-
if ($update_content instanceof Entities\Message) {
457-
$chat_id = $update_content->getFrom()->getId();
458-
$text = $update_content->getText();
459-
} elseif ($update_content instanceof Entities\InlineQuery ||
460-
$update_content instanceof Entities\ChosenInlineResult
461-
) {
462-
$chat_id = $update_content->getFrom()->getId();
463-
$text = $update_content->getQuery();
464-
}
460+
$get_updates_response = $this->telegram->handleGetUpdates();
465461

466-
$output .= sprintf(
467-
'%d: %s' . PHP_EOL,
468-
$chat_id,
469-
preg_replace('/\s+/', ' ', trim($text))
470-
);
471-
}
462+
// Check if the user has set a custom callback for handling the response.
463+
if ($this->custom_get_updates_callback !== null) {
464+
$this->handleOutput(call_user_func($this->custom_get_updates_callback, $get_updates_response));
472465
} else {
473-
$output .= sprintf('Failed to fetch updates: %s' . PHP_EOL, $response->printError());
466+
$this->handleOutput($this->defaultGetUpdatesCallback($get_updates_response));
474467
}
475468

476-
$this->handleOutput($output);
477-
478469
return $this;
479470
}
480471

472+
/**
473+
* Return the default output for getUpdates handling.
474+
*
475+
* @param Entities\ServerResponse $get_updates_response
476+
*
477+
* @return string
478+
*/
479+
protected function defaultGetUpdatesCallback($get_updates_response): string
480+
{
481+
/** @var Entities\Update[] $results */
482+
$results = array_filter((array) $get_updates_response->getResult());
483+
484+
$output = sprintf(
485+
'%s - Updates processed: %d' . PHP_EOL,
486+
date('Y-m-d H:i:s'),
487+
count($results)
488+
);
489+
490+
foreach ($results as $result) {
491+
$chat_id = 0;
492+
$text = '<n/a>';
493+
494+
$update_content = $result->getUpdateContent();
495+
if ($update_content instanceof Entities\Message) {
496+
$chat_id = $update_content->getFrom()->getId();
497+
$text = sprintf('<%s>', $update_content->getType());
498+
} elseif ($update_content instanceof Entities\InlineQuery ||
499+
$update_content instanceof Entities\ChosenInlineResult
500+
) {
501+
$chat_id = $update_content->getFrom()->getId();
502+
$text = sprintf('<query> %s', $update_content->getQuery());
503+
}
504+
505+
$output .= sprintf(
506+
'%d: %s' . PHP_EOL,
507+
$chat_id,
508+
preg_replace('/\s+/', ' ', trim($text))
509+
);
510+
}
511+
512+
return $output;
513+
}
514+
481515
/**
482516
* Handle the updates using the Webhook method.
483517
*

0 commit comments

Comments
 (0)