Skip to content

Commit 0554f07

Browse files
authored
Merge pull request #62 from nutgram/helpers-page
Add "helpers" page
2 parents 1b34946 + 1b10cd7 commit 0554f07

File tree

4 files changed

+628
-118
lines changed

4 files changed

+628
-118
lines changed

docs/usage/handlers.mdx

-118
Original file line numberDiff line numberDiff line change
@@ -1233,121 +1233,3 @@ $bot->onCommand('start2 {param}', [$instance, 'handle']); // instance-method
12331233

12341234
$bot->run();
12351235
```
1236-
1237-
## Update Helpers
1238-
1239-
When dealing with updates, sometimes you may need to access data that is nested in the update structure, which can be
1240-
tedious and produce *a lot* of boilerplate, since the same objects can often be nested in other objects, depending on
1241-
the type of update. For this reason, the framework provides a number of **support methods to quickly access the most
1242-
used data, no matter the update type**, like this:
1243-
1244-
```php
1245-
use SergiX44\Nutgram\Nutgram;
1246-
1247-
$bot = new Nutgram($_ENV['TOKEN']);
1248-
1249-
$bot->onCommand('help', function (Nutgram $bot) {
1250-
// Get the Message object
1251-
$bot->message();
1252-
1253-
// Access the Chat object
1254-
$bot->chat();
1255-
});
1256-
1257-
$bot->onCommand('my_chat', function (Nutgram $bot) {
1258-
$bot->sendMessage('Your chat id is ' . $bot->chatId());
1259-
});
1260-
1261-
$bot->run();
1262-
```
1263-
1264-
### Available helpers
1265-
1266-
| Method | Return type | Description |
1267-
|------------------------|-----------------------|-------------------------------------------------------------------------------|
1268-
| `update()` | `?Update` | The current `Update` object. |
1269-
| `chatId()` | `?int` | The current `chat_id` if available, `null` otherwise. |
1270-
| `chat()` | `?Chat` | The current `Chat` if available, `null` otherwise. |
1271-
| `userId()` | `?int` | The current `from`.`id` if available, `null` otherwise. |
1272-
| `user()` | `?User` | The current `User` (`from` Telegram's object) if available, `null` otherwise. |
1273-
| `messageId()` | `?int` | The current `message`.`message_id` if available, `null` otherwise. |
1274-
| `message()` | `?Message` | The current `Message` if available, `null` otherwise. |
1275-
| `isCallbackQuery()` | `bool` | If the current update contains a `callback_query`. |
1276-
| `callbackQuery()` | `?CallbackQuery` | The current `CallbackQuery` if available, `null` otherwise. |
1277-
| `isInlineQuery()` | `bool` | If the current update contains an `inline_query`. |
1278-
| `inlineQuery()` | `?InlineQuery` | The current `InlineQuery` if available, `null` otherwise. |
1279-
| `chosenInlineResult()` | `?ChosenInlineResult` | The current `ChosenInlineResult` if available, `null` otherwise. |
1280-
| `shippingQuery()` | `?ShippingQuery` | The current `ShippingQuery` if available, `null` otherwise. |
1281-
| `isPreCheckoutQuery()` | `bool` | If the current update contains a `pre_checkout_query`. |
1282-
| `preCheckoutQuery()` | `?PreCheckoutQuery` | The current `PreCheckoutQuery` if available, `null` otherwise. |
1283-
| `poll()` | `?Poll` | The current `Poll` if available, `null` otherwise. |
1284-
| `pollAnswer()` | `?PollAnswer` | The current `PollAnswer` if available, `null` otherwise. |
1285-
| `isMyChatMember()` | `bool` | If the current `ChatMemberUpdated` is in the `my_chat_member`. |
1286-
| `chatMember()` | `?ChatMemberUpdated` | The current `ChatMemberUpdated` if available, `null` otherwise. |
1287-
1288-
## Persisting data
1289-
1290-
The framework gives you the ability to store data based on the update context: you can store data as **globally**
1291-
or **per-user**:
1292-
1293-
```php
1294-
use SergiX44\Nutgram\Nutgram;
1295-
1296-
$bot = new Nutgram($_ENV['TOKEN']);
1297-
1298-
$bot->setGlobalData('mykey', 'Hi!');
1299-
$bot->setUserData('mykey', 'Ciao!', $userId);
1300-
1301-
$value = $bot->getGlobalData('mykey'); // Hi!
1302-
$value = $bot->getUserData('mykey', $userId); // Ciao!
1303-
1304-
// when used inside a context, the $userId can be omitted.
1305-
$bot->onCommand('help', function (Nutgram $bot) {
1306-
$bot->setUserData('mykey', 'called help!');
1307-
$value = $bot->getUserData('mykey'); // called help!
1308-
});
1309-
1310-
$bot->run();
1311-
```
1312-
1313-
:::tip
1314-
If you need to persist data on disk, be sure to choose an appropriate cache adapter!
1315-
:::
1316-
1317-
### Available methods
1318-
1319-
<div class="margin-bottom--md">
1320-
<img style={{verticalAlign: 'middle'}} src="https://img.shields.io/badge/-GLOBALLY-brightgreen"/>&nbsp;
1321-
<code>getGlobalData($key, $default = null)</code><br/>
1322-
Returns the data associated to the <code>$key</code>, if null <code>$default</code> is returned.
1323-
</div>
1324-
1325-
<div class="margin-bottom--md">
1326-
<img style={{verticalAlign: 'middle'}} src="https://img.shields.io/badge/-GLOBALLY-brightgreen"/>&nbsp;
1327-
<code>setGlobalData($key, $value, DateInterval|int|null $ttl = null)</code><br/>
1328-
Returns <code>bool</code>
1329-
</div>
1330-
1331-
<div class="margin-bottom--md">
1332-
<img style={{verticalAlign: 'middle'}} src="https://img.shields.io/badge/-GLOBALLY-brightgreen"/>&nbsp;
1333-
<code>deleteGlobalData($key)</code><br/>
1334-
Returns <code>bool</code>
1335-
</div>
1336-
1337-
<div class="margin-bottom--md">
1338-
<img style={{verticalAlign: 'middle'}} src="https://img.shields.io/badge/-PER--USER-blue"/>&nbsp;
1339-
<code>getUserData($key, ?int $userId = null, $default = null)</code><br/>
1340-
Returns the data associated to the <code>$key</code>, if null <code>$default</code> is returned.
1341-
</div>
1342-
1343-
<div class="margin-bottom--md">
1344-
<img style={{verticalAlign: 'middle'}} src="https://img.shields.io/badge/-PER--USER-blue"/>&nbsp;
1345-
<code>setUserData($key, $value, ?int $userId = null, DateInterval|int|null $ttl = null)</code><br/>
1346-
Returns <code>bool</code>
1347-
</div>
1348-
1349-
<div class="margin-bottom--md">
1350-
<img style={{verticalAlign: 'middle'}} src="https://img.shields.io/badge/-PER--USER-blue"/>&nbsp;
1351-
<code>deleteUserData($key, ?int $userId = null)</code><br/>
1352-
Returns <code>bool</code>
1353-
</div>

0 commit comments

Comments
 (0)