Git Product home page Git Product logo

telegram's Introduction

Telegram Notifications Channel for Laravel

Latest Version on Packagist Software License SensioLabsInsight Quality Score Total Downloads

This package makes it easy to send Telegram notification using Telegram Bot API with Laravel.

Contents

Installation

You can install the package via composer:

composer require laravel-notification-channels/telegram

Setting up your Telegram Bot

Talk to @BotFather and generate a Bot API Token.

Then, configure your Telegram Bot API Token:

// config/services.php
...
'telegram-bot-api' => [
    'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE')
],
...

Usage

You can now use the channel in your via() method inside the Notification class.

use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [TelegramChannel::class];
    }

    public function toTelegram($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        return TelegramMessage::create()
            ->to($this->user->telegram_user_id) // Optional.
            ->content("*HELLO!* \n One of your invoices has been paid!") // Markdown supported.
            ->button('View Invoice', $url); // Inline Button
    }
}

Here's a screenshot preview of the above notification on Telegram Messenger:

Laravel Telegram Notification Example

Attach a Photo

...
    public function toTelegram($notifiable)
    {
        $url = url('/file/' . $this->file->id);

        return TelegramMessage::create()
            ->to($this->user->telegram_user_id) // Optional.
            ->content("*bold text* [inline URL](http://www.example.com/)") // Markdown supported.
            ->file('/storage/archive/6029014.jpg', 'photo') // local photo
            // OR
            // ->file('https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6029/6029014_rd.jpg', 'photo') // remote photo
            ->button('Download PDF', $url); // Inline Button
    }
...

Sample : photo5879686121305255739

Attach a Document

...
    public function toTelegram($notifiable)
    {
        $url = url('/file/' . $this->file->id);

        return TelegramMessage::create()
            ->to($this->user->telegram_user_id) // Optional.
            ->content("*bold text* [inline URL](http://www.example.com/)") // Markdown supported.
            ->file('/storage/archive/file.pdf', 'document') // local file
            // OR
            // ->file('http://www.domain.com/file.pdf', 'document') // remote file
            ->button('Download PDF', $url); // Inline Button
    }
...

Sample : photo5879686121305255737

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 above example or add a routeNotificationForTelegram() method in your notifiable model:

...
/**
 * Route notifications for the Telegram channel.
 *
 * @return int
 */
public function routeNotificationForTelegram()
{
    return $this->telegram_user_id;
}
...

Available Message methods

  • to($chatId): (integer) Recipient's chat id.
  • content(''): (string) Notification message, supports markdown. For more information on supported markdown styles, check out these docs.
  • button($text, $url): (string) Adds an inline "Call to Action" button. You can add as many as you want and they'll be placed 2 in a row.
  • options([]): (array) Allows you to add additional or override sendMessage payload (A Telegram Bot API method used to send message internally). For more information on supported parameters, check out these docs.

Alternatives

For advance usage, please consider using telegram-bot-sdk instead.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

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

License

The MIT License (MIT). Please see License File for more information.

telegram's People

Contributors

belguinan avatar bmitch avatar casperboone avatar enniel avatar freekmurze avatar irajtaghlidi avatar irazasyed avatar ivanvermeyen avatar jackcolley avatar jaybizzle avatar lotuashvili avatar mpociot avatar okaufmann avatar oyed avatar peregraum avatar themsaid avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.