承接 i-rocky/laravel-twilio 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

i-rocky/laravel-twilio

Composer 安装命令:

composer require i-rocky/laravel-twilio

包简介

Twilio Fax, SMS, MMS, Voice Call recording, receiving, sending support for laravel

README 文档

README

Installation

Run the following commands

composer require i-rocky/laravel-twilio

php artisan laravel-twilio:install

This should publish the following files

project
└───config
|       laravel-twilio.php
|
└───resources
    └───assets
        └───js
            └───vendor
                └───laravel-twilio
                    └───mappers
                    |       ResponseMapper.js //maps the response into Response instance
                    |
                    └───models
                    |       Response.js //response model
                    |
                    └───services
                            HttpService.js //proxy for axios requests
                            TwilioService.js //wrapper for twilio client

To use TwilioService.js run yarn add axios twilio-client

You can add/update/remove/move the files and use as your wish

Setup

Update config/services.php

...   
    'twilio' => [
        'account_sid' => env('TWILIO_ACCOUNT_SID'),
        'auth_token'  => env('TWILIO_AUTH_TOKEN'),
        'caller_id'   => env('TWILIO_NUMBER'),
        'username'    => env('TWILIO_USERNAME'),
        'password'    => env('TWILIO_PASSWORD'),
        'app_sid'     => env('TWIML_APP_SID'),
    ],
...

Update .env

TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=
TWILIO_NUMBER=
TWIML_APP_SID=

LARAVEL_TWILIO_BASE_URL=laravel-twilio 
LARAVEL_TWILIO_ENABLE_CALL=true
LARAVEL_TWILIO_RECORD_CALL=true
LARAVEL_TWILIO_REJECT_CALL_MESSAGE="Thank you for calling us"
LARAVEL_TWILIO_REPLY_MESSAGE=null
MIX_LARAVEL_TWILIO_BASE_URL="${LARAVEL_TWILIO_BASE_URL}"
  • TWIML_APP_SID - you need to create a TwiML app for calling from browser
  • LARAVEL_TWILIO_BASE_URL - URL prefix for laravel-twilio
  • LARAVEL_TWILIO_REJECT_CALL_MESSAGE - reject incoming calls with this message when calling is disabled
  • LARAVEL_TWILIO_REPLY_MESSAGE - reply to incoming messages, null for no reply

Now you have to set the Webhook URL in Twilio console.

Replace laravel-twilio in the Webhook URL with the base URL you've set for LARAVEL_TWILIO_BASE_URL in .env

Incoming Calls

Go to your phone number configuration from Active Numbers then click on the desired number.

  1. Under Voice & Fax for Accept Incoming select Voice Calls
  2. Under Configure With select Webhooks, TwiML Bins, Functions, Studio, or Proxy
  3. Under A Call Comes In select Webhook and set the value to https://your-domain.tld/api/laravel-twilio/voice/incoming

Incoming Faxes

Go to your phone number configuration from Active Numbers then click on the desired number.

  1. Under Voice & Fax for Accept Incoming select Faxes
  2. Under Configure With select Webhooks, TwiML Bins, Functions, Studio, or Proxy
  3. Under A Fax Comes In select Webhook and set the value to https://your-domain.tld/api/laravel-twilio/fax/incoming

Incoming Messages

Go to your phone number configuration from Active Numbers then click on the desired number.

  1. Under Configure With select Webhooks, TwiML Bins, Functions, Studio, or Proxy
  2. Under A Message Comes In select Webhook and set the value to https://your-domain.tld/api/laravel-twilio/message/incoming

Outgoing Calls

Go to TwiML Apps list and select desired app or create a new app

  1. Under Voice set the REQUEST URI to https://your-domain.tld/api/laravel-twilio/voice

Usage

Implement Notifiable

/**
 * @property string username
 * @property string phone
 * @property string phone_number
 */
clas User extends Authenticable {
    use Notifiable;

...

    public function routeNotificationForTwilio() {
        return "+{$this->phone}";
    }
    
    public function laravelTwilioIdentity() {
        return Str::snake($this->first_name);
    }
}

Implement notification

use Rocky\LaravelTwilio\Foundation\TwilioMessage;
use Rocky\LaravelTwilio\Message\TwilioSMSMessage;
use Rocky\LaravelTwilio\Message\TwilioMMSMessage;
use Rocky\LaravelTwilio\Message\TwilioFaxMessage;
use Rocky\LaravelTwilio\Message\TwilioCallMessage;
use Rocky\LaravelTwilio\TwilioChannel;

class TwilioTestNotification extends Notification {

...

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

...

    public function toTwilio($notifiable) {
        // SMS
        return (new TwilioSMSMessage())
            ->to('+receiver') // optional
            ->from('+sender') // optional
            ->text('Your message'); // required
    
        // MMS (only works for Canada and US number)
        return (new TwilioMMSMessage())
            ->to('+receiver') // optional
            ->from('+sender') // optional
            ->text('Your message') // optional
            ->mediaUrl('publicly accessible media url'); // required
    
        // Call
        return (new TwilioCallMessage())
            ->to('+receiver') // optional
            ->from('+sender') // optional
            ->mediaUrl('publicly accessible media'); // required
    
        // Fax
        return (new TwilioSMSMessage())
            ->to('+receiver') // optional
            ->from('+sender') // optional
            ->mediaUrl('publicly accessible media url'); // required
    }
}

If you don't use the to('+number') method in your message construction, you must have phone, phone_number property or routeNotificationForTwilio() method implemented in your Notifiable implementation. The number must start with + followed by country code.

If you don't have username property definition in your Auth provider model, you must implement laravelTwilioIdentity() method to give your agents an identity for calling.

Events

Namespace Rocky\LaravelTwilio\Events

  • LaravelTwilioIncomingMessage::class [gives access to IncomingMessage at $event->getMessage()]
  • LaravelTwilioIncomingFax::class [gives access to IncomingFax at $event->getFax()]
  • LaravelTwilioMessageSent::class [gives access to InstanceResource at $event->getMessage() and $notifiable at $event->getNotifiable()]
  • LaravelTwilioMessageSendingFailed::class [gives access to Exception at $event->getException(), Notification at $event->getNotification(), $notifiable at $event->getNotifiable()]
  • LaravelTwilioMessageDeliveryReport::class [gives access to MessageDeliveryReport at $event->getReport()]
  • LaravelTwilioFaxDeliveryReport::class [gives access to FaxDeliveryReport at $event->getReport()]
  • LaravelTwilioInboundCall::class [gives access to InboundCall at $event->getCall()]
  • LaravelTwilioInboundCallRejected::class [gives access to InboundCall at $event->getCall()]
  • LaravelTwilioOutboundCall::class [gives access to OutboundCall at $event->getCall()]
  • LaravelTwilioCallStatusUpdate::class [gives access to CallStatus at $event->getStatus()]
  • LaravelTwilioCallRecord::class [gives access to CallRecord at $event->getRecord()]

All the parameters sent by Twilio are available in the instance passed through Event. Some of the frequently used properties are added for autocomplete support.

Example:

$call = $event->getCall();

$sid = $call->CallSid;
$sid = $call->callSid;
$sid = $call->call_sid;

$from = $call->From;
$from = $call->from;

$allParams = $call->all();

###The incoming fax implementation is not tested.

Look into the source code for a clearer understanding.

i-rocky/laravel-twilio 适用场景与选型建议

i-rocky/laravel-twilio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 45
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-17