2017 © Pedro Peláez
 

yii2-extension yii2-notifications

Notifications system

image

mirkhamidov/yii2-notifications

Notifications system

  • Wednesday, July 25, 2018
  • by miroff
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

TODO

  • configure components from module
  • configure queueNotification from module
  • add requirements in composer.json
  • slack notifications
  • email notifications

Notification providers

Telegram

description see below, in examples, (*1)

Default Yii2 Mailer

configure in main config file main.php, (*2)

return [
    'modules' => [
        ...
        'notifications' => [
            'class' => mirkhamidov\notifications\Module::class,
            'queueIn' => 'queueNotifications',
            'providers' => [
                ...
                'default-mailer' => [
                    'class' => MailerProvider::class,
                    'from' => [{SENDER EMAIL} => {SENDER NAME}],
                ],
            ],
        ],
    ],
];

send using view file and view params, (*3)

Yii::$app->notifications->sendTemplateMail([
    'to' => {RECEPIENT EMAIL},
    'subject' => {SUBJECT},
    'view' => ['html' => {HTML VIEW}, 'text' => {TEXT VIEW}],
    'params' => [
        {KEY-VALUE params for views}
    ],
]);

or already rendered/prepared text and other params, (*4)

Yii::$app->notifications->sendTemplateMail([
    'to' => {RECEPIENT EMAIL},
    'subject' => 'Test messages fo ' . rand(1, 99999),
    'htmlBody' => $msg,
    ['textBody' => $msg,]
    ['cc' => {CC},]
    ['bcc' => {BCC},]
    ['replyTo' => {REPLY TO EMAIL},]
    ['attachFilePath' => {PATH TO FILE TO ATTACH},]
]);

Configuration

in main app config file, (*5)

return [
    'bootstrap' => [
        'queueNotifications',
    ],
    'components' => [
        'queueNotifications' => [
            'class' => \yii\queue\db\Queue::class,
            'as log' => \yii\queue\LogBehavior::class,
            'db' => 'db', // DB connection component or its config
            'tableName' => '{{%queue}}', // Table name
            'channel' => 'notification', // Queue channel key
            'mutex' => \yii\mutex\PgsqlMutex::class, // Mutex that used to sync queries
            'mutexTimeout' => 0,
            'ttr' => 5 * 60, // Max time for anything job handling
            'attempts' => 5, // Max number of attempts
        ],
        'notifications' => [
            'class' => \mirkhamidov\notifications\Notifications::class,
        ],
    ],
    'modules' => [
        'notifications' => [
            'class' => mirkhamidov\notifications\Module::class,
            'queueIn' => 'queueNotifications',
            'providers' => [
                'telegram' => [
                    'class' => \mirkhamidov\notifications\providers\Telegram::class,
                ],
            ],
        ],
    ],
];

Logs to different file

In app config file, (*6)

'components' => [
    'log' => [
        'targets' => [
            ...
            [
                'class' => 'yii\log\FileTarget',
                'categories' => [
                    'mirkhamidov\notifications\providers\*',
                ],
                'logFile' => '@app/runtime/logs/notification-providers.log',
                'logVars' => [],
                'prefix' => function ($message) {
                    return '';
                }
            ],
        ],
    ],
],

Examples

Telegram Send message

use mirkhamidov\notifications\providers\TelegramProvider;

$msg = 'any message';
\Yii::$app->notifications->send($msg, TelegramProvider::ID, [
    'providerParams' => [
        'chat_id' => {CHAT_ID},
    ],
]);

More providerParams look at Telegram SendMessage API, (*7)

Telegram Message with file

use mirkhamidov\notifications\providers\TelegramProvider;

$msg = 'any message';

\Yii::$app->notifications->send($msg, TelegramProvider::ID, [
    'providerParams' => [
        'chat_id' => Yii::$app->params['telegram']['miroff'],
        'file' => $model->getPdfFilePath(),
        'fileParams' => [
            // custom params
            ['fileType' => TelegramProvider::FILE_TYPE_DOCUMENT,]
            ['messageMergeType' => TelegramProvider::FILE_MESSAGE_MERGE_TYPE_AS_REPLY,]

            // any other Telegram API params, see below
            ['disable_notification' => true,]
        ],
    ],
]);

To send only file (without message) just set $mgs to null, (*8)

The Versions

25/07 2018

dev-master

9999999-dev

Notifications system

  Sources   Download

BSD-3-Clause

The Requires

 

by Jasur Mirkhamidov

yii2 module telegram notifications

25/07 2018

0.2.0

0.2.0.0

Notifications system

  Sources   Download

BSD-3-Clause

The Requires

 

by Jasur Mirkhamidov

yii2 module telegram notifications

04/06 2018

0.1.0

0.1.0.0

Notifications system

  Sources   Download

BSD-3-Clause

The Requires

 

by Jasur Mirkhamidov

yii2 module telegram notifications