zhb / nexmo-bundle
v0.1.0
2018-09-14 06:13 UTC
Requires
- php: ^7.1.3
- nexmo/client: ^1.4
- symfony/config: ^3.4 || ^4.0
- symfony/dependency-injection: ^3.4 || ^4.0
- symfony/http-kernel: ^3.4 || ^4.0
- symfony/serializer: ^3.4 || ^4.1
- symfony/swiftmailer-bundle: ^3.1
Requires (Dev)
- symfony/browser-kit: ^3.4 || ^4.0
- symfony/framework-bundle: ^3.4 || ^4.0
- symfony/phpunit-bridge: ^3.4 || ^4.0
- symfony/property-access: ^3.4 || ^4.1
This package is not auto-updated.
Last update: 2021-07-23 23:47:49 UTC
README
Nexmo SMS 集成到 Symfony。此包提供了一种简单的方法来从您的应用程序发送 SMS。
安装
使用 Composer 在您的项目中安装 NexmoBundle
composer require "zhb/nexmo-bundle"
配置
首先,创建一个 Nexmo 账户。然后,配置此包
# config/packages/zhb_nexmo.yaml zhb_nexmo: provider: 'zhb_nexmo.mail' # (optional) default to zhb_nexmo.sms disable_delivery: true # (optional) default to false. sms: api_key: 'nexmo_api_key' api_secret: 'nexmo_api_secret' from: 'John Doe' ttl: '86400000' # (optional) default to 259200000 ms. callback: 'https://website.com/zhb-nexmo/delivery-receipt' # (optional) default to null. This parameter overrides the webhook endpoint you set in Dashboard. mail: from: 'email@sent.from' to: 'email@sent.to'
为了能够使用 Nexmo 递送回执 (DLR)
# config/routes/zhb_nexmo.yaml _zhb_nexmo: resource: '@ZhbNexmoBundle/Resources/config/routes.xml' prefix: /zhb-nexmo/delivery-receipt # change as you want
要设置用于接收消息的 webhook,使用 zhb_nexmo.sms.callback
参数或使用消息设置器
$message->setCallback('https://website.com/nexmo/delivery-receipt');
或者,您可以访问 Nexmo 控制台的 您的号码 部分。为虚拟号码点击 '编辑' 并设置回调 URL(上面定义的前缀)。
用法
如何发送 SMS
<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use Zhb\NexmoBundle\Message\TextMessage; use Zhb\NexmoBundle\Provider\MailProvider; use Zhb\NexmoBundle\Provider\ProviderInterface; use Zhb\NexmoBundle\Provider\SmsProvider; class SmsController extends AbstractController { private $provider; private $emailProvider; private $smsProvider; public function __construct(ProviderInterface $provider, MailProvider $emailProvider, SmsProvider $smsProvider) { $this->provider = $provider; // provider defined in zhb_nexmo.yaml provider key. $this->emailProvider = $emailProvider; $this->smsProvider = $smsProvider; } /** * @Route("/sms/send", name="sms_send") */ public function send() { // phone number must be in E.164 format $message = new TextMessage('+41798562466', 'Sms message content'); // message setters overrides global config defined in config/packages/zhb_nexmo.yaml $message->setFrom('Tom Cruise'); $message->setTtl(900000); // in milliseconds $message->setCallback('https://website.com/zhb-nexmo/delivery-receipt'); $message->setClientRef('custom_sms_reference'); $response = $this->provider->send($message); var_dump($response); $this->emailProvider->send($message); $this->smsProvider->send($message); ... } }
如何监听 SMS 递送回执 (DLR)
<?php namespace App\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Zhb\NexmoBundle\Event\DeliveryReceiptEvent; use Zhb\NexmoBundle\Event\ZhbNexmoEvents; class DeliveryReceiptSubscriber implements EventSubscriberInterface { public function onSmsStatusChanged(DeliveryReceiptEvent $event) { $deliveryReceipt = $event->getDeliveryReceipt(); ... } public static function getSubscribedEvents() { return [ ZhbNexmoEvents::SMS_STATUS_CHANGED => 'onSmsStatusChanged', ]; } }