blood72/laravel-jandi

该包已被废弃,不再维护。未建议替代包。

JANDI通知Laravel

v1.0.4 2021-02-03 08:11 UTC

This package is auto-updated.

Last update: 2023-09-29 02:13:36 UTC


README

JANDI是由Toss Lab Inc.开发的商业协作消息工具。
此包为非官方包,仅提供JANDI incoming webhook连接的通知通道。

目录

需求

  • PHP >= 7.2
  • Laravel 5.8+, 6.* 或 7.*

安装

使用composer进行安装。

composer require blood72/laravel-jandi

如果禁用了自动发现,则需要额外的操作。

将服务提供者添加到config/app.php中的providers数组中。

'providers' => [
    // ...
    Blood72\Jandi\JandiServiceProvider::class,
],

还要添加此代码到aliases数组以使用JANDI通知器。

'aliases' => [
    // ...
    'Jandi' => \Blood72\Jandi\JandiFacade::class,
],

如果您不想使用JANDI通知器,请按照以下说明操作。

  1. composer.json中添加禁用自动发现的代码
    {
        "extra": {
            "laravel": {
                "dont-discover": [
                    "blood72/laravel-jandi"
                ]
            }
        }
    }
  2. 在您的服务提供者的register()方法中添加此代码
    use Blood72\Jandi\Notifications\Channels\JandiWebhookChannel;
    use GuzzleHttp\Client as HttpClient;
    use Illuminate\Notifications\ChannelManager;
    use Illuminate\Support\Facades\Notification;
    
    // ...
    
    Notification::resolved(function (ChannelManager $service) {
        $service->extend('jandi', function ($app) {
            return new JandiWebhookChannel(new HttpClient);
        });
    });

配置

默认情况下,此包仅支持一个webhook URL。

JANDI_WEBHOOK_URL=https://wh.jandi.com/connect-api/webhook/{team_id}/{payload_token}

您可以发布一个配置文件。

php artisan vendor:publish --provider="Blood72\Jandi\JandiServiceProvider"

并且您可以进行如下自定义。在这种情况下,每个URL都会发送请求。

'jandi_webhook_url' => [
    env('JANDI_WEBHOOK_URL_1'),
    env('JANDI_WEBHOOK_URL_2'),
    // ... and so all
],

使用

  • 消息

    • to(): 为JANDI团队消息设置一个邮箱(可选)。

      $message = (new JandiMessage)->to('test@example.org');
    • content(): 设置JANDI消息的内容。

      $message = (new JandiMessage)->content('hello test');

      创建带有内容的对象;__construct(), create()

      $message = new JandiMessage('hello test');
      $message = JandiMessage::create('hello test');
    • color(): 设置附件部分的颜色。

      $message->color('#000000');
      $message->color('#30fe2a');

      它支持Bootstrap 4颜色方案。

      $message->primary();
      $message->secondary();
      $message->success();
      $message->danger();
      $message->warning();
      $message->info();
      $message->light();
      $message->dark();
    • attachment(): 定义消息的附件。您可以添加多个。

      $message->attachment(function ($attachment) {
          $attachment->title('attachment-title');
          $attachment->description('attachment-description');
          $attachment->image('attachment-image-url');
      });
      
      $message->attachment(function ($attachment) {
          $attachment->title('attachment-title');
          $attachment->description('attachment-description');
          $attachment->image('attachment-image-url');
      })->attachment(function ($attachment) {
          $attachment->title('attachment-another-title');
          $attachment->description('attachment-another-description');
          $attachment->image('attachment-another-image-url');
      });
  • 通知

    • 通知覆盖
      您可以使用定义的抽象类。它需要定义toJandi()方法。
      use Blood72\Jandi\Notifications\JandiNotification;
      
      class JandiExampleNotification extends JandiNotification
      {
          public function toJandi($notifiable/* = null*/): JandiMessage
          {
              return (new JandiMessage)->to('test@example.org')->content('hello test');
          }
      }
    • 发送通知
      • 通过匿名可通知的
        use Illuminate\Notifications\AnonymousNotifiable;
        use Illuminate\Support\Facades\Notification;
        
        Notification::send(new AnonymousNotifiable, new JandiExampleNotification
      • 通过可通知模型
        要使用此功能,必须定义routeNotificationForJandi()
        use Illuminate\Database\Eloquent\Model;
        use Illuminate\Notifications\Notifiable;
        
        class ExampleNotifiableModel extends Model
        {
            use Notifiable;
        
            public function routeNotificationForJandi()
            {
                return 'hello routeNotificationForJandi() test';
            }
        }
        $notifiable = new ExampleNotifiableModel;
        
        $notification->send($notifiable, new JandiExampleNotification);
  • 外观

    它支持JandiNotifier类作为'Jandi'外观。

    • send(): 您可以简单地发送消息。

      • 它是根据默认路由设置发送的。
        use Blood72\Jandi\Notifications\Messages\JandiMessage;
        
        $message = JandiMessage::create('hello test');
        // or $message = new JandiMessage('hello test');
        
        Jandi::send($message);
      • 当然,也可以用简单的字符串形式完成。
        Jandi::send('hello test');
      • 如果您不想使用默认通知类,可以设置其他通知类。
        Jandi::send('hello test', YourOtherNotification::class);
    • to(): 您可以指定接收者URL(s)。

      • 通过字符串
        Jandi::to('jandi-webhook-url')->send('hello test');
      • 通过多个参数
        Jandi::to('jandi-webhook-url-1', 'jandi-webhook-url-2')->send('hello test');
      • 通过数组
        Jandi::to([
            'jandi-webhook-url-1',
            'email-1' => 'jandi-webhook-url-2',
            'email-2' => [
                'jandi-webhook-url-3',
                'jandi-webhook-url-4',
            ],
        ]);
        在发送团队聊天webhook URL (仅限付费团队) 1:1聊天时,可以设置邮箱。JANDI中不验证邮箱,并且要一起发送,它必须是字符串。
      • 通过对象
        要使用此功能,必须定义routeNotificationForJandi()jandi_webhook_url。它可以将jandi_webhook_url定义为getJandiWebhookUrlAttribute()
        use App\User;
        
        public function routeNotificationForJandi()
        {
            return 'url';
        }
        
        // in example, $user is User model instance.
        
        Jandi::to($user)->send('hello test');
        如果您想设置邮箱,则必须定义jandi_email(或getJandiEmailAttribute())。

参考

... 以及基于@kwonmory编写的代码

许可证

此包是开源软件,根据MIT许可证授权。