PHP code example of jacksmall / dd-notification-channel
1. Go to this page and download the library: Download jacksmall/dd-notification-channel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jacksmall / dd-notification-channel example snippets
namespace App\Services;
use App\Services\DingDing\Config;
use App\Services\DingDing\Signer;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Notification as NotificationFacade;
class DingDingService
{
protected $signer;
protected $config;
public function __construct(Config $config, Signer $signer)
{
$this->signer = $signer;
$this->config = $config;
}
/**
* @param string $robotName
* @return string
*/
public function getSignedUrl(string $robotName = 'default')
{
$url = $this->config->getWebhookUrl($robotName);
$secret = $this->config->getSecret($robotName);
if (empty($url) || empty($secret)) {
throw new \InvalidArgumentException("Invalid DingDing config for: {$robotName}");
}
[$sign, $timestamp] = $this->signer->generate($secret);
return "{$url}×tamp={$timestamp}&sign={$sign}";
}
/**
* @param Notification $notification
* @param string $robotName
* @return void
*/
public function sendNotification(Notification $notification, string $robotName = 'default')
{
$url = $this->getSignedUrl($robotName);
NotificationFacade::route('dingding', $url)->notify($notification);
}
}
use App\Services\DingDing\Config;
use App\Services\DingDing\Signer;
use App\Services\DingDingService;
...
$this->app->singleton(Config::class, function () {
return new Config();
});
$this->app->singleton(Signer::class, function () {
return new Signer();
});
$this->app->singleton(DingDingService::class, function($app) {
return new DingDingService(
$app->make(Config::class),
$app->make(Signer::class)
);
});
...
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Jacksmall\DdNotificationChannel\Channels\DingDingChannel;
class TestDingDingNotice extends Notification
{
use Queueable;
public $message;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [DingDingChannel::class];
}
/**
* Get the dingding representation of the notification.
*
* @param mixed $notifiable
*/
public function toDingDing($notifiable)
{
return [
'type' => 'text',
'params' => [
'content' => $this->message
],
'http' => [
'verify' => false,
'timeout' => 5
] // 可选 HTTP 参数
];
}
}
/**
* @return void
*/
public function dingTalkNotification(Request $request)
{
$notification = new TestDingDingNotice($request->input('message'));
app(DingDingService::class)->sendNotification($notification, 'default');
}
php artisan make:notification TestDingDingNotice
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.