Laravel 的 Facebook 通知通道

0.3.0 2022-01-13 22:28 UTC

This package is auto-updated.

Last update: 2024-09-12 21:37:17 UTC


README

Join PHP Chat Chat on Telegram Latest Version on Packagist Software License Quality Score Total Downloads

此包简化了使用 Laravel 通过 Facebook Messenger 发送通知的过程。

内容

安装

您可以通过 Composer 安装此包

composer require laravel-notification-channels/facebook

设置您的 Facebook 机器人

按照 入门指南 创建一个 Facebook Messenger 应用、一个 Facebook 页面和页面令牌,以便将它们连接起来。

接下来,我们需要将此令牌添加到我们的 Laravel 配置中。在 config/services.php 中创建一个新的 Facebook 部分,并将页面令牌放在那里

// config/services.php
...
'facebook' => [
    'page-token' => env('FACEBOOK_PAGE_TOKEN', 'YOUR PAGE TOKEN HERE'),
    
    // Optional - Omit this if you want to use default version.
    'version'    => env('FACEBOOK_GRAPH_API_VERSION', '4.0')
    
    // Optional - If set, the appsecret_proof will be sent to verify your page-token.
    'app-secret' => env('FACEBOOK_APP_SECRET', '')
],
...

用法

让我们以发票付款通知为例。现在您可以在 InvoicePaid 类的 via() 方法中使用 Facebook 通道。to($userId) 方法定义了您想要发送通知的 Facebook 用户。

根据您添加的详细信息(文本、附件等),将自动确定要发送的消息类型。例如,如果您只添加 text(),则它将是一个基本消息;使用 attach() 将将其转换为附件消息。如果有 buttonscards,则将其更改为 按钮模板通用模板

use NotificationChannels\Facebook\FacebookChannel;
use NotificationChannels\Facebook\FacebookMessage;
use NotificationChannels\Facebook\Components\Button;
use NotificationChannels\Facebook\Enums\NotificationType;

use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [FacebookChannel::class];
    }

    public function toFacebook($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        return FacebookMessage::create()
            ->to($notifiable->fb_messenger_user_id) // Optional
            ->text('One of your invoices has been paid!')
            ->isUpdate() // Optional
            ->isTypeRegular() // Optional
            // Alternate method to provide the notification type.
            // ->notificationType(NotificationType::REGULAR) // Optional
            ->buttons([
                Button::create('View Invoice', $url)->isTypeWebUrl(),
                Button::create('Call Us for Support!', '+1(212)555-2368')->isTypePhoneNumber(),
                Button::create('Start Chatting', ['invoice_id' => $this->invoice->id])->isTypePostback() // Custom payload sent back to your server
            ]); // Buttons are optional as well.
    }
}

通知将通过您的 Facebook 页面发送,其中包含您之前配置的页面令牌。以下是聊天窗口中通知的截图预览。

Laravel Facebook Notification Example

消息示例

基本文本消息

向用户发送基本文本消息

return FacebookMessage::create('You have just paid your monthly fee! Thanks')
    ->to($notifiable->fb_messenger_user_id);
附件消息

向用户发送文件附件(例如发送 pdf 发票)

return FacebookMessage::create()
    ->attach(AttachmentType::FILE, url('invoices/'.$this->invoice->id))
    ->to($notifiable->fb_messenger_user_id);
通用(卡片轮播)消息

向用户发送一组卡片/项目,以轮播的形式显示(例如发送一组链接)。注意,您还可以在每个卡片中添加最多三个按钮

return FacebookMessage::create()
    ->to($notifiable->fb_messenger_user_id) // Optional
    ->cards([
        Card::create('Card No.1 Title')
            ->subtitle('An item description')
            ->url('items/'.$this->item[0]->id)
            ->image('items/'.$this->item[0]->id.'/image'),
        
        Card::create('Card No.2 Title')
            ->subtitle('An item description')
            ->url('items/'.$this->item[1]->id)
            ->image('items/'.$this->item[1]->id.'/image')
        // could add buttons using ->buttons($array of Button)
    ]); 

路由消息

您可以通过向 to($userId) 方法提供接收者的页面范围用户 ID(PSID),如上例所示,或添加 routeNotificationForFacebook() 方法到您的可通知模型中,来发送通知。

...
/**
 * Route notifications for the Facebook channel.
 *
 * @return int
 */
public function routeNotificationForFacebook()
{
    return $this->fb_messenger_user_id;
}
...

可用的消息方法

  • to($recipient, $type): (字符串|数组)接收者的页面范围用户 idphone_numberuser_refpost_idcomment_id(作为支持的类型之一 - 使用 Enums\RecipientType 使其更易于使用)。电话号码支持的格式 +1(212)555-2368注意:向电话号码发送消息需要 pages_messaging_phone_number 权限。有关更多信息,请参阅 文档
  • text(''): (字符串)通知消息。
  • isResponse(): 将 messaging_type 设置为 RESPONSE
  • isUpdate(): (默认)将 messaging_type 设置为 UPDATE
  • isMessageTag($messageTag): (字符串)将 messaging_type 设置为 MESSAGE_TAG,您可以使用 NotificationChannels\Facebook\Enums\MessageTag 来轻松地进行操作。
  • attach($attachment_type, $url): (AttachmentType, 字符串) 一个附件类型(IMAGE, AUDIO, VIDEO, FILE)以及该附件的URL
  • buttons($buttons = []): (数组) 一个“行动号召”按钮数组(使用NotificationChannels\Facebook\Components\Button::create()创建)。您可以添加最多3个以下类型的按钮:web_urlpostbackphone_number。有关更多详细信息,请参阅下面的按钮方法。
  • cards($cards = []): (数组) 要在轮播图中显示的项目卡片数组(使用NotificationChannels\Facebook\Components\Card::create()创建)。您可以添加最多10张卡片。有关更多详细信息,请参阅下面的卡片方法。
  • notificationType(''): (字符串) 推送通知类型:REGULAR将发出声音/振动和手机通知;SILENT_PUSH将仅发出手机通知,NO_PUSH则不发出任何通知。您可以使用NotificationType::REGULARNotificationType::SILENT_PUSHNotificationType::NO_PUSH来简化类型的使用。这是一个可选方法,默认为REGULAR类型。
  • imageAspectRatio(''): (字符串) 如果卡片包含image_url,则使用图像宽高比。您可以使用ImageAspectRatioType::SQUAREImageAspectRatioType::HORIZONTAL。这是一个可选方法,默认为ImageAspectRatioType::HORIZONTAL宽高比(图像应为1.91:1)。
  • isTypeRegular(): 创建通知类型REGULAR的辅助方法。
  • isTypeSilentPush(): 创建通知类型SILENT_PUSH的辅助方法。
  • isTypeNoPush(): 创建通知类型NO_PUSH的辅助方法。

可用的按钮方法

  • title(''): (字符串) 按钮标题。
  • data(''): (字符串) 按钮数据 - 可以是网页URL、postback数据或格式化的电话号码。
  • type(''): (字符串) 按钮类型 - web_urlpostbackphone_number。使用ButtonType枚举器以确保有效值。
  • isTypeWebUrl(): 创建web_url类型按钮的辅助方法。
  • isTypePhoneNumber(): 创建phone_number类型按钮的辅助方法。
  • isTypePostback(): 创建postback类型按钮的辅助方法。

可用的卡片方法

  • title(''): (字符串) 卡片标题。
  • subtitle(''): (字符串) 卡片副标题。
  • url(''): (字符串) 卡片项目URL。
  • image(''): (字符串) 卡片图像URL。图像比例应为1.91:1。
  • buttons($buttons = []): (数组) 一个“行动号召”按钮数组(使用NotificationChannels\Facebook\Components\Button::create()创建)。您可以添加最多3个以下类型的按钮:web_urlpostbackphone_number。有关更多详细信息,请参阅上面的按钮方法。

贡献

请参阅CONTRIBUTING获取详细信息。

致谢

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。