/** * @param array $config * @param Lead $lead * @param MauticFactory $factory * * @return boolean */ public static function send(array $config, Lead $lead, MauticFactory $factory) { /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */ $leadModel = $factory->getModel('lead.lead'); if ($leadModel->isContactable($lead, 'sms') !== DoNotContact::IS_CONTACTABLE) { return array('failed' => 1); } $leadPhoneNumber = $lead->getFieldValue('mobile'); if (empty($leadPhoneNumber)) { $leadPhoneNumber = $lead->getFieldValue('phone'); } if (empty($leadPhoneNumber)) { return array('failed' => 1); } /** @var \Mautic\SmsBundle\Api\AbstractSmsApi $sms */ $smsApi = $factory->getKernel()->getContainer()->get('mautic.sms.api'); /** @var \Mautic\SmsBundle\Model\SmsModel $smsModel */ $smsModel = $factory->getModel('sms'); $smsId = (int) $config['sms']; /** @var \Mautic\SmsBundle\Entity\Sms $sms */ $sms = $smsModel->getEntity($smsId); if ($sms->getId() !== $smsId) { return array('failed' => 1); } $dispatcher = $factory->getDispatcher(); $event = new SmsSendEvent($sms->getMessage(), $lead); $event->setSmsId($smsId); $dispatcher->dispatch(SmsEvents::SMS_ON_SEND, $event); $metadata = $smsApi->sendSms($leadPhoneNumber, $event->getContent()); // If there was a problem sending at this point, it's an API problem and should be requeued if ($metadata === false) { return false; } return array('type' => 'mautic.sms.sms', 'status' => 'mautic.sms.timeline.status.delivered', 'id' => $sms->getId(), 'name' => $sms->getName(), 'content' => $event->getContent()); }
/** * @param array $config * @param Lead $lead * @param MauticFactory $factory * * @return array */ public static function send(array $config, Lead $lead, MauticFactory $factory) { /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */ $leadModel = $factory->getModel('lead.lead'); $logger = $factory->getLogger(); if ($leadModel->isContactable($lead, 'notification') !== DoNotContact::IS_CONTACTABLE) { $logger->error('Error: Lead ' . $lead->getId() . ' is not contactable on the web push channel.'); return array('failed' => 1); } // If lead has subscribed on multiple devices, get all of them. /** @var \Mautic\NotificationBundle\Entity\PushID[] $pushIDs */ $pushIDs = $lead->getPushIDs(); $playerID = array(); foreach ($pushIDs as $pushID) { $playerID[] = $pushID->getPushID(); } if (empty($playerID)) { $logger->error('Error: Lead ' . $lead->getId() . ' has not subscribed to web push channel.'); return array('failed' => 1); } /** @var \Mautic\NotificationBundle\Api\AbstractNotificationApi $notification */ $notificationApi = $factory->getKernel()->getContainer()->get('mautic.notification.api'); /** @var \Mautic\NotificationBundle\Model\NotificationModel $notificationModel */ $notificationModel = $factory->getModel('notification'); $notificationId = (int) $config['notification']; /** @var \Mautic\NotificationBundle\Entity\Notification $notification */ $notification = $notificationModel->getEntity($notificationId); if ($notification->getId() !== $notificationId) { $logger->error('Error: The requested notification cannot be found.'); return array('failed' => 1); } $url = $notificationApi->convertToTrackedUrl($notification->getUrl(), array('notification' => $notification->getId(), 'lead' => $lead->getId())); $response = $notificationApi->sendNotification($playerID, $notification->getMessage(), $notification->getHeading(), $url); // If for some reason the call failed, tell mautic to try again by return false if ($response->code !== 200) { $logger->error('Error: The notification failed to send and returned a ' . $response->code . ' HTTP response with a body of: ' . $response->body); return false; } return array('status' => 'mautic.notification.timeline.status.delivered', 'type' => 'mautic.notification.notification', 'id' => $notification->getId(), 'name' => $notification->getName(), 'heading' => $notification->getHeading(), 'content' => $notification->getMessage()); }