Skip to content

Commit 5b08826

Browse files
authored
Add Retrieving Chat ID docs to Readme
1 parent 06833e3 commit 5b08826

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ This package makes it easy to send Telegram notification using [Telegram Bot API
1212
## Contents
1313

1414
- [Installation](#installation)
15-
- [Setting up your Telegram bot](#setting-up-your-telegram-bot)
15+
- [Setting up your Telegram Bot](#setting-up-your-telegram-bot)
16+
- [Retrieving Chat ID](#retrieving-chat-id)
1617
- [Using in Lumen](#using-in-lumen)
1718
- [Proxy or Bridge Support](#proxy-or-bridge-support)
1819
- [Usage](#usage)
@@ -66,6 +67,47 @@ Then, configure your Telegram Bot API Token:
6667
],
6768
```
6869

70+
## Retrieving Chat ID
71+
72+
In order for us to send notifications to your Telegram Bot user/channel or group, we need to know their Chat ID.
73+
74+
This can be done by fetching the [updates][link-telegram-docs-update] for your Bot using the `getUpdates` method as per Telegram Bot API [docs][link-telegram-docs-getupdates].
75+
76+
An [update][link-telegram-docs-update] is an object containing relevant fields based on the type of update it represents, some examples of an update object are `message`, `callback_query`, and `poll`. For a complete list of fields, see [Telegram Bot API docs][link-telegram-docs-update].
77+
78+
To make things easier, the library comes with a handy method which can be used to get the updates from which you can parse the relevant Chat ID.
79+
80+
Please keep in mind the user has to first interact with your bot for you to be able to obtain their Chat ID which you can then store in your database for future interactions or notifications.
81+
82+
Here's an example of fetching an update:
83+
84+
```php
85+
use NotificationChannels\Telegram\TelegramUpdates;
86+
87+
// Response is an array of updates.
88+
$updates = TelegramUpdates::create()
89+
// (Optional). Get's the latest update. NOTE: All previous updates will be forgotten using this method.
90+
// ->latest()
91+
92+
// (Optional). Limit to 2 updates (By default, updates starting with the earliest unconfirmed update are returned).
93+
->limit(2)
94+
95+
// (Optional). Add more params to the request.
96+
->options([
97+
'timeout' => 0,
98+
])
99+
->get();
100+
101+
if($updates['ok']) {
102+
// Chat ID
103+
$chatId = $updates['result'][0]['message']['chat']['id'];
104+
}
105+
```
106+
107+
_Note: This method will not work if an outgoing webhook is set up._
108+
109+
For a complete list of available parameters for the `options`, see [Telegram Bot API docs][link-telegram-docs-getupdates].
110+
69111
## Using in Lumen
70112

71113
If you're using this notification channel in your Lumen project, you will have to add the below code in your `bootstrap/app.php` file.
@@ -416,3 +458,5 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
416458
[link-contributors]: ../../contributors
417459
[link-notification-facade]: https://laravel.com/docs/8.x/notifications#using-the-notification-facade
418460
[link-on-demand-notifications]: https://laravel.com/docs/8.x/notifications#on-demand-notifications
461+
[link-telegram-docs-update]: https://core.telegram.org/bots/api#update
462+
[link-telegram-docs-getupdates]: https://core.telegram.org/bots/api#getupdates

0 commit comments

Comments
 (0)