manthan/nuncio

将您的客户消息/电子邮件转换为mustache风格的模板。只需为您的消息创建一个模板,nuncio就会用客户详情替换标签。

1.1.1 2015-10-03 09:21 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:21:16 UTC


README

Nuncio是一个灵活可定制的消息传递器,可以帮助您将短信/电子邮件转换为mustache风格的模板。

安装

您可以使用Composer安装Nuncio。

{
   "require": {
      "manthan/nuncio": "1.1.*"
   }
}

基本用法

使用Nuncio就像扩展Manthan\Nuncio\Nuncio并指定您希望在消息模板中替换的关键字一样简单。

例如,您想通过以下模板在仪表板上通过短信通知您的用户

Hello {{name}}, Your fees ${{fees}} are pending.

处理此消息的步骤如下

    use Manthan\Nuncio\Nuncio

    class SMSNotifier extends Nuncio
    {
      protected $keywords = array('name', 'fees');
    }

看起来很简单,对吧?

现在要使用这个新创建的通知器,请执行以下操作

    $SMS_Service = new TwilioService() // This must implement Manthan\Nuncio\MessengerInterface

    $processor = new Manthan\Nuncio\MessageProcessor();
    $notifier = new SMSNotifier($SMS_Service, $processor);

    $user = new stdClass();
    $user->name   = "Manthan";
    $user->fees   = "1500";
    $user->number = "78787878";

    $recepients = [$user];
    $notifier->from('YOUR-NUMBER')->to($recepients)->send($subject, $message);

上面的代码将以下消息发送到电话号码78787878

Hello Manthan, your fees $1500 are pending.

Nuncio非常灵活。如上所示,您可以使用任何消息服务实现(无论是Twilio、Plivo还是电子邮件通知器)发送您处理后的消息。

更改数字字段

如您在上面的示例中看到,Nuncio默认在每位接收者上查找'number'属性并向该号码发送消息。

但您很可能会在用户对象中不使用数字属性。在这种情况下,您可以更改默认数字字段如下

    use Manthan\Nuncio\Nuncio

    class SMSNotifier extends Nuncio
    {

      protected $keywords = array('name', 'fees');

      // Suppose your user's phone number is denoted by its phone_number property
      protected $number_field = 'phone_number';
    }

运行测试

您可以使用

./vendor/bin/phpunit