定制 laravel-notification-channels/twilio 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravel-notification-channels/twilio

Composer 安装命令:

composer require laravel-notification-channels/twilio

包简介

Provides Twilio notification channel for Laravel

README 文档

README

Latest Version on Packagist Software License Build Status Total Downloads

This package makes it easy to send Twilio notifications with Laravel 12 and 13.

Contents

Installation

You can install the package via Composer:

$ composer require laravel-notification-channels/twilio

Configuration

Add your Twilio Account SID, Auth Token, and From Number (optional) to your .env:

TWILIO_USERNAME=XYZ # optional when using auth token
TWILIO_PASSWORD=ZYX # optional when using auth token
TWILIO_AUTH_TOKEN=ABCD # optional when using username and password
TWILIO_ACCOUNT_SID=1234 # always required
TWILIO_FROM=100000000 # optional default from
TWILIO_ALPHA_SENDER=HELLO # optional
TWILIO_DEBUG_TO=23423423423 # Set a number that call calls/messages should be routed to for debugging
TWILIO_SMS_SERVICE_SID=MG0a0aaaaaa00aa00a00a000a00000a00a # Optional but recommended
TWILIO_SHORTEN_URLS=true # optional, enable URL shortener

Advanced configuration

Run php artisan vendor:publish --provider="NotificationChannels\Twilio\TwilioProvider"

/config/twilio-notification-channel.php

Suppressing specific errors or all errors

Publish the config using the above command, and edit the ignored_error_codes array. You can get the list of exception codes from the documentation.

If you want to suppress all errors, you can set the option to ['*']. The errors will not be logged but notification failed events will still be emitted.

Recommended Configuration

Twilio recommends always using a Messaging Service because it gives you access to features like Advanced Opt-Out, Sticky Sender, Scaler, Geomatch, Shortcode Reroute, and Smart Encoding.

Having issues with SMS? Check Twilio's best practices.

Upgrading from 4.1 to 4.2

We have dropped support for PHP < 8.3 and the minimum Laravel version is now 12. Other than that, there are no breaking changes.

Upgrading from 3.x to 4.x

We have dropped support for PHP < 8.2 and the minimum Laravel version is now 11. Other than that, there are no breaking changes.

Upgrading from 2.x to 3.x

If you're upgrading from version 2.x, you'll need to make sure that your set environment variables match those above in the config section. None of the environment variable names have changed, but if you used different keys in your services.php config then you'll need to update them to match the above, or publish the config file and change the env key.

You should also remove the old entry for twilio from your services.php config, since it's no longer used.

The main breaking change between 2.x and 3.x is that failed notification will now throw an exception unless they are in the list of ignored error codes (publish the config file to edit these).

You can replicate the 2.x behaviour by setting 'ignored_error_codes' => ['*'], which will case all exceptions to be suppressed.

Usage

Now you can use the channel in your via() method inside the notification to send an SMS:

use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioSmsMessage;
use Illuminate\Notifications\Notification;

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

    public function toTwilio($notifiable)
    {
        return (new TwilioSmsMessage())
            ->content("Your {$notifiable->service} account was approved!");
    }
}

You can also send an MMS:

use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioMmsMessage;
use Illuminate\Notifications\Notification;

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

    public function toTwilio($notifiable)
    {
        return (new TwilioMmsMessage())
            ->content("Your {$notifiable->service} account was approved!")
            ->mediaUrl("https://picsum.photos/300");
    }
}

You can also send using Content Templates:

use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioContentTemplateMessage;
use Illuminate\Notifications\Notification;

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

    public function toTwilio($notifiable)
    {
        return (new TwilioContentTemplateMessage())
            ->contentSid("HXXXXXXXXXXXXXXXXXXXXXXXX")
            ->contentVariables([
                '1' => 'John Doe',
                '2' => 'ACME Inc.',
            ]);
    }
}

Note

If sending via WhatsApp, you must add whatsapp: to the beginning of the phone number (i.e. ->from('whatsapp:+61428000382')). The number must also be approved as a WhatsApp Sender.

Or create a Twilio Call Message:

use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioCallMessage;
use Illuminate\Notifications\Notification;

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

    public function toTwilio($notifiable)
    {
        return (new TwilioCallMessage())
            ->url("http://example.com/your-twiml-url");
    }
}

In order to let your Notification know which phone are you sending/calling to, the channel will look for the phone_number attribute of the Notifiable model. If you want to override this behaviour, add the routeNotificationForTwilio method to your Notifiable model.

public function routeNotificationForTwilio()
{
    return '+1234567890';
}

Available Message methods

TwilioSmsMessage

  • from(''): Accepts a phone to use as the notification sender.
  • content(''): Accepts a string value for the notification body.
  • messagingServiceSid(''): Accepts a messaging service SID to handle configuration.

TwilioMmsMessage

  • mediaUrl(''): Set the message media url.

TwilioContentTemplateMessage

  • contentSid(''): Set the content sid (starting with H).
  • contentVariables([...]): Set the content variables.

TwilioCallMessage

  • from(''): Accepts a phone to use as the notification sender.
  • url(''): Accepts an url for the call TwiML.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email pipo@onlime.ch instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-notification-channels/twilio 适用场景与选型建议

laravel-notification-channels/twilio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.28M 次下载、GitHub Stars 达 258, 最近一次更新时间为 2016 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sms」 「twilio」 「notification」 「laravel」 「call」 「mms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laravel-notification-channels/twilio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laravel-notification-channels/twilio 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravel-notification-channels/twilio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.28M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 258
  • 点击次数: 33
  • 依赖项目数: 16
  • 推荐数: 3

GitHub 信息

  • Stars: 258
  • Watchers: 1
  • Forks: 96
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-12