thinkstudeo/textlocal-notification-channel

Laravel 通知渠道用于 Textlocal Api

1.0.0 2019-04-23 16:25 UTC

This package is auto-updated.

Last update: 2024-09-24 21:49:49 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SymfonyInsight Quality Score Total Downloads

此包可以轻松使用 Laravel 5.6+ 发送通知,使用 Textlocal

支持同时使用 Textlocal 的交易账户和推广账户。

内容

安装

composer require thinkstudeo/textlocal-notification-channel

利用自 Laravel 5.5 以来可用的自动包发现功能,服务提供程序将自动注册。

设置 Textlocal 服务

config/services.php 文件中添加您的 textlocal 账户、API URL 和凭证。由于 textlocal 为不同国家提供不同的 URL,因此必须在配置文件中设置该 URL。例如,印度的 URL 是 https://api/textlocal.in/send/

...
'textlocal' => [
    'url' => 'https://api.textlocal.com/send/'	//or 'https://api.textlocal.in/send/ - for India

    //Textlocal Transactional Account
    'transactional' => [
        'apiKey' => env('TEXTLOCAL_TRANSACTIONAL_KEY'),
        'from' => env('TEXTLOCAL_TRANSACTIONAL_FROM', 'TXTLCL')
	],

    //Textlocal Promotional Account
    'promotional' => [
        'apiKey' => env('TEXTLOCAL_PROMOTIONAL_KEY'),
        'from' => env('TEXTLOCAL_PROMOTIONAL_FROM', 'TXTLCL')
    ]
],
...

别忘了在 .env 文件中添加密钥

...
TEXTLOCAL_TRANSACTIONAL_KEY= <Your Textlocal Transactional Account API KEY>
TEXTLOCAL_TRANSACTIONAL_FROM= <Registered/Approved sender for your Textlocal Transactional Account>
TEXTLOCAL_PROMOTIONAL_KEY= <Your Textlocal Promotional Account API KEY>
TEXTLOCAL_PROMOTIONAL_FROM= <Registered/Approved sender for your Textlocal Promotional Account>
...

使用

要使用该通道,请在您的通知类的 via() 方法中包含 NotificationChannels\Textlocal\TextlocalChannel 类。

使用 Textlocal 推广账户

use Illuminate\Notifications\Notification;
use NotificationChannels\Textlocal\TextlocalChannel;
use NotificationChannels\Textlocal\TextlocalMessage;

class SendBlackFridaySaleAnnouncement extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [TextlocalChannel::class];
    }

    /**
     * Get the Textlocal / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toTextlocal($notifiable)
    {
        return (new TextlocalMessage())
            //Required
            // To send sms via your Textlocal promotional account
            //or transactional() to sent via Textlocal transactional account
            ->promotional()

            //Optional
            //If you don't provide a from, it will pick up the value from the config
            ->from('TXTLCL')

            //Optional
            //If you want to send a copy of the sms to another number eg an Admin
            ->cc('914545454545')

            //Required
            ->content('We are running a BlackFriday sale from tomorrow for 3 days with 40% off. Hurry !!! Grab the opportunity!');
    }
}

使用 Textlocal 交易账户

use Illuminate\Notifications\Notification;
use NotificationChannels\Textlocal\TextlocalChannel;
use NotificationChannels\Textlocal\TextlocalMessage;

class SendLoginOtp extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [TextlocalChannel::class];
    }

    /**
     * Get the Textlocal / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toTextlocal($notifiable)
    {
        return (new TextlocalMessage())
            //Required
            // To send sms via your Textlocal transactional account
            //or promotional() to sent via Textlocal promotional account
            ->transactional()

            //Optional
            //If you don't provide a from, it will pick up the value from the config
            ->from('TXTLCL')

            //Optional
            //If you want to send a copy of the sms to another number eg an Admin
            ->cc('914545454545')

            //Required
            //When sending through Textlocal transactional account, the content must conform to one of your approved templates.
            ->content('Your OTP for Application is 234567. It is valid for the next 10 minutes only.');
    }
}

变更日志

请参阅 CHANGELOG 以获取最近更改的更多信息。

测试

phpunit.xml.dist 中填写 env 值。测试依赖于这些值。

<php>
    <env name="APP_ENV" value="Testing"/>
    <env name="TEXTLOCAL_TRANSACTIONAL_KEY" value="API_KEY for transactional account"/>
    <env name="TEXTLOCAL_TRANSACTIONAL_FROM" value="TXTLCL"/>
    <env name="TEXTLOCAL_PROMOTIONAL_KEY" value="API_KEY for transactional account"/>
    <env name="TEXTLOCAL_PROMOTIONAL_FROM" value="TXTLCL"/>

    <env name="TEST_TEXTLOCAL_TRANSACTIONAL_TEMPLATE" value="Your approved template for Textlocal transactional account"/>
    <env name="TEST_TEXTLOCAL_TRANSACTIONAL_MOBILE" value="Valid phone number"/>
    <env name="TEST_TEXTLOCAL_TRANSACTIONAL_CC" value="Another Valid Phone number"/>
</php>
$ composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件 neerav@thinkstudeo.com 联系,而不是使用问题跟踪器。

贡献

请参阅 CONTRIBUTING 以获取详细信息。

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件