概觀
laravel-bluesky 可整合至 Laravel 的 Notification 系統。透過 BlueskyChannel 發送一般貼文,BlueskyPrivateChannel 則可發送私訊(DM)。
可用的頻道
| 頻道 | 用途 |
|---|---|
BlueskyChannel | 以一般公開貼文形式進行通知 |
BlueskyPrivateChannel | 以 DM(私訊)形式通知收件者 |
Notification 類別
BlueskyChannel
在via() 中指定 BlueskyChannel::class,並在 toBluesky() 回傳發送內容。
use Illuminate\Notifications\Notification;
use Revolution\Bluesky\Notifications\BlueskyChannel;
use Revolution\Bluesky\Record\Post;
use Revolution\Bluesky\RichText\TextBuilder;
use Revolution\Bluesky\Embed\External;
class TestNotification extends Notification
{
public function via(object $notifiable): array
{
return [
BlueskyChannel::class
];
}
public function toBluesky(object $notifiable): Post
{
$external = External::create(title: 'Title', description: 'test', uri: 'https://');
return Post::build(function (TextBuilder $builder) {
$builder->text('test')
->newLine()
->tag('#Laravel');
})->embed($external);
}
}
Post 的用法與 Basic client 相同。TextBuilder、Embed 等也可以同樣使用。
BlueskyPrivateChannel
BlueskyPrivateMessage 幾乎與 Post 相同,但僅支援 text、facets、embed。embed 僅支援 QuoteRecord。
use Illuminate\Notifications\Notification;
use Revolution\Bluesky\Notifications\BlueskyPrivateChannel;
use Revolution\Bluesky\Notifications\BlueskyPrivateMessage;
use Revolution\Bluesky\RichText\TextBuilder;
use Revolution\Bluesky\Embed\QuoteRecord;
use Revolution\Bluesky\Types\StrongRef;
class TestNotification extends Notification
{
public function via(object $notifiable): array
{
return [
BlueskyPrivateChannel::class
];
}
public function toBlueskyPrivate(object $notifiable): BlueskyPrivateMessage
{
$quote = QuoteRecord::create(StrongRef::to(uri: 'at://', cid: 'cid'));
return BlueskyPrivateMessage::build(function (TextBuilder $builder) {
$builder->text('test')
->newLine()
->tag('#Laravel');
})->embed($quote);
}
}
隨選通知
若不使用模型,而是當場指定通知對象,可使用Notification::route()。
BlueskyChannel
use Illuminate\Support\Facades\Notification;
use Revolution\Bluesky\Notifications\BlueskyRoute;
use Revolution\Bluesky\Session\OAuthSession;
use App\Models\User;
// App password
Notification::route('bluesky', BlueskyRoute::to(identifier: config('bluesky.identifier'), password: config('bluesky.password')))
->notify(new TestNotification());
// OAuth
$user = User::find(1);
$session = OAuthSession::create([
'did' => $user->did,
'iss' => $user->iss,
'refresh_token' => $user->refresh_token,
]);
Notification::route('bluesky', BlueskyRoute::to(oauth: $session))
->notify(new TestNotification());
BlueskyPrivateChannel
DM 必須指定receiver(收件者的 DID 或 handle)。同時,接收方也需啟用 DM 接收。
- 使用 App password 時需具備 DM 發送權限。
- 使用 OAuth 時需具備
transition:chat.bskyscope。
use Illuminate\Support\Facades\Notification;
use Revolution\Bluesky\Notifications\BlueskyRoute;
use Revolution\Bluesky\Session\OAuthSession;
use App\Models\User;
// App password
Notification::route('bluesky-private', BlueskyRoute::to(identifier: config('bluesky.identifier'), password: config('bluesky.password'), receiver: 'did or handle'))
->notify(new TestNotification());
// OAuth
$user = User::find(1);
$session = OAuthSession::create([
'did' => $user->did,
'iss' => $user->iss,
'refresh_token' => $user->refresh_token,
]);
Notification::route('bluesky-private', BlueskyRoute::to(oauth: $session, receiver: 'did or handle'))
->notify(new TestNotification());
Bluesky 中無法對自己發送 DM。若要對自己發送通知,請另備一個帳號用於發送。
// 對自己發送 DM
use Illuminate\Support\Facades\Notification;
use Revolution\Bluesky\Notifications\BlueskyRoute;
Notification::route('bluesky-private', BlueskyRoute::to(identifier: 'sender identifier', password: 'sender password', receiver: 'your did or handle'))
->notify(new TestNotification());
.env 設定。請建立發送專用的帳號。
BLUESKY_SENDER_IDENTIFIER=sender did or handle
BLUESKY_SENDER_APP_PASSWORD=sender password
BLUESKY_RECEIVER=your did or handle
Notification::route('bluesky-private', BlueskyRoute::to(
identifier: config('bluesky.notification.private.sender.identifier'),
password: config('bluesky.notification.private.sender.password'),
receiver: config('bluesky.notification.private.receiver'),
))->notify(new TestNotification());
使用者通知
在使用Notifiable trait 的模型上定義通知路由。
BlueskyChannel
use Illuminate\Notifications\Notifiable;
use Revolution\Bluesky\Notifications\BlueskyRoute;
use Revolution\Bluesky\Session\OAuthSession;
class User
{
use Notifiable;
public function routeNotificationForBluesky($notification): BlueskyRoute
{
// App password
return BlueskyRoute::to(identifier: $this->bluesky_identifier, password: $this->bluesky_password);
// OAuth
$session = OAuthSession::create([
'did' => $this->did,
'iss' => $this->iss,
'refresh_token' => $this->refresh_token,
]);
return BlueskyRoute::to(oauth: $session);
}
}
$user->notify(new TestNotification());
BlueskyPrivateChannel
use Illuminate\Notifications\Notifiable;
use Revolution\Bluesky\Notifications\BlueskyRoute;
use Revolution\Bluesky\Session\OAuthSession;
class User
{
use Notifiable;
public function routeNotificationForBlueskyPrivate($notification): BlueskyRoute
{
// App password
return BlueskyRoute::to(identifier: $this->bluesky_identifier, password: $this->bluesky_password, receiver: $this->receiver);
// OAuth
$session = OAuthSession::create([
'did' => $this->did,
'iss' => $this->iss,
'refresh_token' => $this->refresh_token,
]);
return BlueskyRoute::to(oauth: $session, receiver: $this->receiver);
}
}
$user->notify(new TestNotification());
public function routeNotificationForBlueskyPrivate($notification): BlueskyRoute
{
// App password
return BlueskyRoute::to(identifier: 'sender identifier', password: 'sender password', receiver: $this->did);
}
BlueskyRoute
根據認證方式(App password 或 OAuth)指定方法不同。建議使用具名參數。use Revolution\Bluesky\Notifications\BlueskyRoute;
use Revolution\Bluesky\Session\OAuthSession;
// App password
BlueskyRoute::to(identifier: config('bluesky.identifier'), password: config('bluesky.password'))
// OAuth
$session = OAuthSession::create([
'did' => '...',
'iss' => '...',
'refresh_token' => '...',
]);
BlueskyRoute::to(oauth: $session);
若僅是對自己的帳號發送通知,使用 App password 最為簡單。無需考量 refresh_token 的更新,只要設定
.env 即可使用。BLUESKY_IDENTIFIER=
BLUESKY_APP_PASSWORD=
確認通知結果
與一般 Laravel 相同,可透過NotificationSent 事件確認通知後的回應。
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Http\Client\Response;
class Listener
{
public function handle(NotificationSent $event): void
{
// $event->channel BlueskyChannel
// $event->notifiable
// $event->notification
// $event->response null|Response
}
}
Source:docs/notification.md