Giter Club home page Giter Club logo

telegram-notifier's Introduction

Telegram Notifier

Provides Telegram integration for Symfony Notifier.

DSN example

TELEGRAM_DSN=telegram://TOKEN@default?channel=CHAT_ID

where:

  • TOKEN is your Telegram token
  • CHAT_ID is your Telegram chat id

Adding Interactions to a Message

With a Telegram message, you can use the TelegramOptions class to add message options.

use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('');

// Create Telegram options
$telegramOptions = (new TelegramOptions())
    ->chatId('@symfonynotifierdev')
    ->parseMode('MarkdownV2')
    ->disableWebPagePreview(true)
    ->disableNotification(true)
    ->replyMarkup((new InlineKeyboardMarkup())
        ->inlineKeyboard([
            (new InlineKeyboardButton('Visit symfony.com'))
                ->url('https://symfony.com/'),
        ])
    );

// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);

$chatter->send($chatMessage);

Adding files to a Message

With a Telegram message, you can use the TelegramOptions class to add message options.

โš ๏ธ WARNING In one message you can send only one file

Telegram supports 3 ways for passing files:

  • You can send files by passing public http url to option:
    • Photo
      $telegramOptions = (new TelegramOptions())
           ->photo('https://localhost/photo.mp4');
    • Video
      $telegramOptions = (new TelegramOptions())
           ->video('https://localhost/video.mp4');
    • Animation
      $telegramOptions = (new TelegramOptions())
           ->animation('https://localhost/animation.gif');
    • Audio
      $telegramOptions = (new TelegramOptions())
           ->audio('https://localhost/audio.ogg');
    • Document
      $telegramOptions = (new TelegramOptions())
           ->document('https://localhost/document.odt');
    • Sticker
      $telegramOptions = (new TelegramOptions())
           ->sticker('https://localhost/sticker.webp', '๐Ÿค–');
  • You can send files by passing local path to option, in this case file will be sent via multipart/form-data:
    • Photo
      $telegramOptions = (new TelegramOptions())
           ->uploadPhoto('files/photo.png');
    • Video
      $telegramOptions = (new TelegramOptions())
           ->uploadVideo('files/video.mp4');
    • Animation
          $telegramOptions = (new TelegramOptions())
               ->uploadAnimation('files/animation.gif');
    • Audio
      $telegramOptions = (new TelegramOptions())
           ->uploadAudio('files/audio.ogg');
    • Document
      $telegramOptions = (new TelegramOptions())
           ->uploadDocument('files/document.odt');
    • Sticker
      $telegramOptions = (new TelegramOptions())
           ->uploadSticker('files/sticker.webp', '๐Ÿค–');
  • You can send files by passing file_id to option:
    • Photo
      $telegramOptions = (new TelegramOptions())
           ->photo('ABCDEF');
    • Video
      $telegramOptions = (new TelegramOptions())
           ->video('ABCDEF');
    • Animation
      $telegramOptions = (new TelegramOptions())
           ->animation('ABCDEF');
    • Audio
      $telegramOptions = (new TelegramOptions())
           ->audio('ABCDEF');
    • Document
      $telegramOptions = (new TelegramOptions())
           ->document('ABCDEF');
    • Sticker - Can't be sent using file_id

Full example:

use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Photo Caption');

// Create Telegram options
$telegramOptions = (new TelegramOptions())
    ->chatId('@symfonynotifierdev')
    ->parseMode('MarkdownV2')
    ->disableWebPagePreview(true)
    ->hasSpoiler(true)
    ->protectContent(true)
    ->photo('https://symfony.com/favicons/android-chrome-192x192.png');

// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);

$chatter->send($chatMessage);

Adding Location to a Message

With a Telegram message, you can use the TelegramOptions class to add message options.

use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('');

// Create Telegram options
$telegramOptions = (new TelegramOptions())
    ->chatId('@symfonynotifierdev')
    ->parseMode('MarkdownV2')
    ->location(48.8566, 2.3522);

// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);

$chatter->send($chatMessage);

Adding Venue to a Message

With a Telegram message, you can use the TelegramOptions class to add message options.

use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('');

// Create Telegram options
$telegramOptions = (new TelegramOptions())
    ->chatId('@symfonynotifierdev')
    ->parseMode('MarkdownV2')
    ->venue(48.8566, 2.3522, 'Center of Paris', 'France, Paris');

// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);

$chatter->send($chatMessage);

Adding Contact to a Message

With a Telegram message, you can use the TelegramOptions class to add message options.

use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('');

$vCard = 'BEGIN:VCARD
VERSION:3.0
N:Doe;John;;;
FN:John Doe
EMAIL;type=INTERNET;type=WORK;type=pref:[email protected]
TEL;type=WORK;type=pref:+330186657200
END:VCARD';

// Create Telegram options
$telegramOptions = (new TelegramOptions())
    ->chatId('@symfonynotifierdev')
    ->parseMode('MarkdownV2')
    ->contact('+330186657200', 'John', 'Doe', $vCard);

// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);

$chatter->send($chatMessage);

Updating Messages

The TelegramOptions::edit() method was introduced in Symfony 6.2.

When working with interactive callback buttons, you can use the TelegramOptions to reference a previous message to edit.

use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Are you really sure?');
$telegramOptions = (new TelegramOptions())
    ->chatId($chatId)
    ->edit($messageId) // extracted from callback payload or SentMessage
    ->replyMarkup((new InlineKeyboardMarkup())
        ->inlineKeyboard([
            (new InlineKeyboardButton('Absolutely'))->callbackData('yes'),
        ])
    );

Answering Callback Queries

The TelegramOptions::answerCallbackQuery() method was introduced in Symfony 6.3.

When sending message with inline keyboard buttons with callback data, you can use TelegramOptions to answer callback queries.

use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Thank you!');
$telegramOptions = (new TelegramOptions())
    ->chatId($chatId)
    ->answerCallbackQuery(
        callbackQueryId: '12345', // extracted from callback
        showAlert: true,
        cacheTime: 1,
    );

Resources

telegram-notifier's People

Contributors

alexandre-daubois avatar alexsoft avatar chalasr avatar chr-hertel avatar derrabus avatar fabpot avatar fancyweb avatar gromnan avatar iamvar avatar igrizzli avatar jderusse avatar jeremyfreeagent avatar jeroeny avatar krasilnikovm avatar mryamous avatar nicolas-grekas avatar nsbx avatar nyholm avatar oskarstark avatar rosier avatar thepanz avatar thomas2411 avatar tigitz avatar wouterj avatar xabbuh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.