Skip to content

Commit 933084b

Browse files
committed
Improve getUpdates output format, make a note of the custom getUpdates callback in the readme and update changelog.
1 parent ae55eaa commit 933084b

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,25 +478,28 @@ public function handleGetUpdates(): self
478478
*/
479479
protected function defaultGetUpdatesCallback($get_updates_response): string
480480
{
481+
/** @var Entities\Update[] $results */
481482
$results = array_filter((array) $get_updates_response->getResult());
482483

483-
$output = date('Y-m-d H:i:s') . ' - ';
484-
$output .= sprintf('Updates processed: %d' . PHP_EOL, count($results));
484+
$output = sprintf(
485+
'%s - Updates processed: %d' . PHP_EOL,
486+
date('Y-m-d H:i:s'),
487+
count($results)
488+
);
485489

486-
/** @var Entities\Update $result */
487490
foreach ($results as $result) {
488491
$chat_id = 0;
489-
$text = 'Nothing';
492+
$text = '<n/a>';
490493

491494
$update_content = $result->getUpdateContent();
492495
if ($update_content instanceof Entities\Message) {
493496
$chat_id = $update_content->getFrom()->getId();
494-
$text = $update_content->getType();
497+
$text = sprintf('<%s>', $update_content->getType());
495498
} elseif ($update_content instanceof Entities\InlineQuery ||
496499
$update_content instanceof Entities\ChosenInlineResult
497500
) {
498501
$chat_id = $update_content->getFrom()->getId();
499-
$text = $update_content->getQuery();
502+
$text = sprintf('<query> %s', $update_content->getQuery());
500503
}
501504

502505
$output .= sprintf(

0 commit comments

Comments
 (0)