Download the PHP package vance-page/laravel-notification-telegram without Composer
On this page you can find all versions of the php package vance-page/laravel-notification-telegram. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vance-page/laravel-notification-telegram
More information about vance-page/laravel-notification-telegram
Files in vance-page/laravel-notification-telegram
Package laravel-notification-telegram
Short Description Telegram Notifications Channel for Laravel
License MIT
Homepage https://github.com/vance-page
Informations about the package laravel-notification-telegram
Telegram Notifications Channel for Laravel
This package makes it easy to send Telegram notification using Telegram Bot API with Laravel.
Contents
- Installation
- Setting up your Telegram Bot
- Retrieving Chat ID
- Using in Lumen
- Proxy or Bridge Support
- Usage
- Text Notification
- Send a Poll
- Attach a Contact
- Attach an Audio
- Attach a Photo
- Attach a Document
- Attach a Location
- Attach a Video
- Attach a GIF File
- Routing a Message
- Handling Response
- On-Demand Notifications
- Sending to Multiple Recipients
- Available Methods
- Shared Methods
- Telegram Message methods
- Telegram Location methods
- Telegram File methods
- Telegram Contact methods
- Telegram Poll methods
- Alternatives
- Changelog
- Testing
- Security
- Contributing
- Credits
- License
Installation
You can install the package via composer:
Setting up your Telegram Bot
Talk to @BotFather and generate a Bot API Token.
Then, configure your Telegram Bot API Token:
Retrieving Chat ID
For us to send notifications to your Telegram Bot user/channel or group, we need to know their Chat ID.
This can be done by fetching the updates for your Bot using the getUpdates
method as per Telegram Bot API docs.
An 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.
To make things easier, the library comes with a handy method that can be used to get the updates from which you can parse the relevant Chat ID.
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.
Here's an example of fetching an update:
Note: This method will not work if an outgoing webhook is set up.
For a complete list of available parameters for the options
, see Telegram Bot API docs.
Using in Lumen
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.
Proxy or Bridge Support
You may not be able to send notifications if Telegram Bot API is not accessible in your country,
you can either set a proxy by following the instructions here or
use a web bridge by setting the base_uri
config above with the bridge uri.
You can set HTTPS_PROXY
in your .env
file.
Usage
You can now use the channel in your via()
method inside the Notification class.
Text Notification
Here's a screenshot preview of the above notification on Telegram Messenger:
Send a Poll
Preview:
Attach a Contact
Preview:
Attach an Audio
Preview:
Attach a Photo
Preview:
Attach a Document
Preview:
Attach a Location
Preview:
Attach a Video
Preview:
Attach a GIF File
Preview:
Routing a Message
You can either send the notification by providing with the chat ID of the recipient to the to($chatId)
method like shown in the previous examples or add a routeNotificationForTelegram()
method in your notifiable model:
Handling Response
You can make use of the notification events to handle the response from Telegram. On success, your event listener will receive a Message object with various fields as appropriate to the notification type.
For a complete list of response fields, please refer the Telegram Bot API's Message object docs.
On-Demand Notifications
Sometimes you may need to send a notification to someone who is not stored as a "user" of your application. Using the
Notification::route
method, you may specify ad-hoc notification routing information before sending the notification. For more details, you can check out the on-demand notifications docs.
Sending to Multiple Recipients
Using the notification facade you can send a notification to multiple recipients at once.
If you're sending bulk notifications to multiple users, the Telegram Bot API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results.
Also note that your bot will not be able to send more than 20 messages per minute to the same group.
If you go over the limit, you'll start getting
429
errors. For more details, refer Telegram Bots FAQ.
Available Methods
Shared Methods
These methods are optional and shared across all the API methods.
to(int|string $chatId)
: Recipient's chat id.token(string $token)
: Bot token if you wish to override the default token for a specific notification.button(string $text, string $url, int $columns = 2)
: Adds an inline "Call to Action" button. You can add as many as you want, and they'll be placed 2 in a row by default.buttonWithCallback(string $text, string $callback_data, int $columns = 2)
: Adds an inline button with the given callback data. You can add as many as you want, and they'll be placed 2 in a row by default.disableNotification(bool $disableNotification = true)
: Send the message silently. Users will receive a notification with no sound.options(array $options)
: Allows you to add additional params or override the payload.getPayloadValue(string $key)
: Get payload value for given key.
Telegram Message methods
For more information on supported parameters, check out these docs.
content(string $content, int $limit = null)
: Notification message, supports markdown. For more information on supported markdown styles, check out these docs.view(string $view, array $data = [], array $mergeData = [])
: (optional) Blade template name with Telegram supported HTML or Markdown syntax content if you wish to use a view file instead of thecontent()
method.chunk(int $limit = 4096)
: (optional) Message chars chunk size to send in parts (For long messages). Note: Chunked messages will be rate limited to one message per second to comply with rate limitation requirements from Telegram.
Telegram Location methods
latitude(float|string $latitude)
: Latitude of the location.longitude(float|string $longitude)
: Longitude of the location.
Telegram File methods
content(string $content)
: (optional) File caption, supports markdown. For more information on supported markdown styles, check out these docs.view(string $view, array $data = [], array $mergeData = [])
: (optional) Blade template name with Telegram supported HTML or Markdown syntax content if you wish to use a view file instead of thecontent()
method.file(string|resource|StreamInterface $file, string $type, string $filename = null)
: Local file path or remote URL,$type
of the file (Ex:photo
,audio
,document
,video
,animation
,voice
,video_note
) and optionally filename with extension. Ex:sample.pdf
. You can use helper methods instead of using this to make it easier to work with file attachment.photo(string $file)
: Helper method to attach a photo.audio(string $file)
: Helper method to attach an audio file (MP3 file).document(string $file, string $filename = null)
: Helper method to attach a document or any file as document.video(string $file)
: Helper method to attach a video file.animation(string $file)
: Helper method to attach an animated gif file.voice(string $file)
: Helper method to attach a voice note (.ogg
file with OPUS encoded).videoNote(string $file)
: Helper method to attach a video note file (Upto 1 min long, rounded square video).
Telegram Contact methods
phoneNumber(string $phoneNumber)
: Contact phone number.firstName(string $firstName)
: Contact first name.lastName(string $lastName)
: (optional) Contact last name.vCard(string $vCard)
: (optional) Contact vcard.
Telegram Poll methods
question(string $question)
: Poll question.choices(array $choices)
: Poll choices.
Alternatives
For advance usage, please consider using telegram-bot-sdk instead.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
- Irfaq Syed
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-notification-telegram with dependencies
ext-json Version *
guzzlehttp/guzzle Version ^7.0
illuminate/contracts Version ^6.9|^7.0|^8.0|^9.0
illuminate/notifications Version ^6.9|^7.0|^8.0|^9.0
illuminate/support Version ^6.9|^7.0|^8.0|^9.0