xorgxx/neox-notify-bundle

简单发送通知

安装: 76

依赖: 0

建议者: 0

安全: 0

星星: 1

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

1.0 2023-08-31 11:40 UTC

This package is auto-updated.

Last update: 2024-09-29 10:56:51 UTC


README

此包提供了一种简单灵活的方法,在您的应用程序中提供“缠绕”通知*。其主要目标是使您发送简单命令以通过任何您想要的方式(电子邮件(带或不带附件)、浏览器、短信等)变得简单。请注意,没有测试代码!

2023-08-28-15-43-05.png

安装!!

使用Composer安装此包!!因为它仍然是beta版本!!

  composer require xorgxx/neox-notify-bundle
  or 
  composer require xorgxx/neox-notify-bundle:0.*

确保在您的AppKernel中注册了该包

Bundles.php
<?php

return [
    .....
    NeoxNotify\neoxNotifyBundle\neoxNotifyBundle::class => ['all' => true],
    .....
];

!! 重要提示 !!

要使用所有传输(Mercure、RabbitMQ等),必须安装它们才能使用它们!!NeoxNotifyBundle将使用您的传输配置。请注意,首先您需要安装并正确设置它们,然后才能使用通知。

  • NeoxNotifyBundle不会为您安装和设置Mercure、RabbitMQ等。

新闻

  • 为(非免费)服务提供商添加自动传输配置 | 合作伙伴 Sms Partner
  • 将发送短信作为电子邮件发送到数据库 + 在symfony日志中添加,如果失败
.env
    .....
      ###> smspartner/SMS ###
      SMSPARTNER_DSN=smspartner://API-KEY:SECRET@api.smspartner.fr/v1/send?from=xxxx&dns=smspartner
      ###> smspartner/SMS ###  
    .....

注意: 您可能需要使用 [ symfony composer dump-autoload ] 重新加载自动加载

..... 完成 🎈

!! 您需要自定义样式配置!!

在此阶段,我们尚未对所有内容进行优化!!

配置

└─── src
│   └─── Templates
│       └─── _Partial
|           └─── Emails
|               └─── include      <--- this to store the base template
|               └─── template     <--- to store Template

neox_notify.yaml

它自动设置,但您可以自定义

    parameters:
        neox_notify:
            template: ~
                include: Partial\include\fdgdgdf
                emails: Partial\emails\
            save_notify: true # by default true mean all notification send will be save in Db messenger. 
            it will give error in Db data. it will also in monolog as log ERROR.
         
            service:
                channels: [] # email, slack, mercure, webhook, ...
                subject: subject
                template: default
                content: ....

您还可以自定义路径以渲染Twig模板!

  as you can see in code if you setTemplate() to what eve "xxx/xxxx/xxx.tmh.twig it will set.
  
  // try to fund way to be able to have custom path to template
  // $option->getTemplate() == "default" ; null ; "xxxx/xxxxx/default.html.twig"
  $value = $option->getTemplate();
  $Template = match (true) {
    str_contains($value, '/') => $value, 
    default => $this->neoxTemplate['emails'] . "/" . ($option->getTemplate() ? : 'default'). '.html.twig',
  };

如何使用?

myController.php
<?php
....
    use NeoxNotify\NeoxNotifyBundle\notify\NotificationStrategyFactory;
    use NeoxNotify\NeoxNotifyBundle\notify\notificationQueue;
....

        #[Route('/{id}/send', name: 'app_admin_tokyo_crud_send', methods: ['GET'])]
        public function send( Request $request, Tokyo $tokyo, NotificationStrategyFactory $notificationStrategyFactory): Response
        {
            
            $urlToken           = $this->generateUrl("app_home_tokyo_switch",["token" => $tokyo->getToken()], UrlGeneratorInterface::ABSOLUTE_URL);
            
            // Create listing Queue
            $notificationQueue  = new NotificationQueue();
            // Create and configure email strategy
            $notification = $notificationStrategyFactory->EmailStrategy();
//            $notification->setRecipient(new Recipient($tokyo->getEmail()));  < --- This will set by default valeur
            $notification->setTemplate("tokyo");
            $notification->Subject('Test ONLY !!');
            $notification->content('....');
            $notification->setContext('urlToken',$urlToken);
            // put in Queue
            $notificationQueue->addNotification($notification);
            
            ......
    
        }

使用属性发送通知

myController.php
<?php
....
  use NeoxNotify\NeoxNotifyBundle\Attribute\NeoxNotifyAlert;
....

        #[Route('/{id}/send', name: 'app_admin_tokyo_crud_send', methods: ['GET'])]
        #[NeoxNotifyAlert(channels: ["email", "sms"], template: 'default', subject: 'Download file', content: '....', contexts: ["name" => "windev"])]
        public function send( Request $request, Tokyo $tokyo, NotificationStrategyFactory $notificationStrategyFactory): Response
        {
                     
            ......
    
        }
        
        all is set by default! attributes :
        channels: ["email", "sms"]      -> chose witch channel you want to use.
        template: 'default'             -> template name in folder "emails: Partial\emails\"
        subject: 'Download file'        -> subject of email
        content: '....'                 -> content/body email, sms, ....
        
        ------------ !!!! SUBJECT and CONTEXTS are special !!!! ------------------------------
        contexts: [ "name" => "file", "opt" => "[attributes.file;attributes.controller]"], 
        subject: "Download file [attributes.file;attributes.controller]"
        
        In subject | add "xxxxx xxxxx [attributes.file;attributes.controller]" in subject it will read in the Request object -> attribute-get("file")
        In contexts: [ "opt" => "[attributes.file;attributes.controller]"] it will read in the Request object -> attribute-get("file") and pass (neox_file, neox_controller) to twig in [template: 'default'] 
        

请注意!

您在Twig中传递的所有变量都将使用前缀 ["neox_"] 设置,我们选择这样做是为了避免在模板中发生冲突

        $key    = "name"
        $value  = "trying for conflicts avoid"
          public function setContext($key, $value): void
        {
            // Add a prefix to the key to avoid conflicts in the template
            $prefixedKey = "neox_" . $key;
            
            // Set the value in the context array
            $this->context[$prefixedKey] = $value;
        }
        
        <p style="line-height: 100%; font-size: 18px;"><em><strong>{{ "-----" ~ neox_name|default("Message interne") ~ "-----"}}</strong></em></p>

如何使用高级功能?🎉

  • 更多电子邮件通知 EMAIL

  • 更多短信通知 SMS

  • 更多聊天通知 CHAT

  • 更多Mercure设置闪烁 set flash

  • 高级功能:您可以创建一个包含您完整逻辑的类,就像您通常使用NotificationBundle一样。它必须返回Notification。

贡献

如果您想为此包做出贡献(感谢您!),以下是一些指导方针

  • 请尊重 Symfony指南
  • 测试一切!当您修复一个之前未覆盖的bug、添加新功能或看到代码(但在测试目录中没有相应的测试用例)时,请添加测试用例到tests/目录中。
    • 您修复了一个之前未覆盖的bug
    • 您添加了一个新功能
    • 您看到代码正在运行,但没有测试用例覆盖(在天堂有一个特别的地方为您保留)

待办事项

谢谢