Ejemplo n.º 1
0
 /**
  * @param CampaignExecutionEvent $event
  */
 public function onCampaignTriggerAction(CampaignExecutionEvent $event)
 {
     $lead = $event->getLead();
     if ($this->leadModel->isContactable($lead, 'sms') !== DoNotContact::IS_CONTACTABLE) {
         return $event->setFailed('mautic.sms.campaign.failed.not_contactable');
     }
     $leadPhoneNumber = $lead->getFieldValue('mobile');
     if (empty($leadPhoneNumber)) {
         $leadPhoneNumber = $lead->getFieldValue('phone');
     }
     if (empty($leadPhoneNumber)) {
         return $event->setFailed('mautic.sms.campaign.failed.missing_number');
     }
     $smsId = (int) $event->getConfig()['sms'];
     $sms = $this->smsModel->getEntity($smsId);
     if ($sms->getId() !== $smsId) {
         return $event->setFailed('mautic.sms.campaign.failed.missing_entity');
     }
     $smsEvent = new SmsSendEvent($sms->getMessage(), $lead);
     $smsEvent->setSmsId($smsId);
     $this->dispatcher->dispatch(SmsEvents::SMS_ON_SEND, $smsEvent);
     $tokenEvent = $this->dispatcher->dispatch(SmsEvents::TOKEN_REPLACEMENT, new TokenReplacementEvent($smsEvent->getContent(), $lead, ['channel' => ['sms', $sms->getId()]]));
     $metadata = $this->smsApi->sendSms($leadPhoneNumber, $tokenEvent->getContent());
     $defaultFrequencyNumber = $this->factory->getParameter('sms_frequency_number');
     $defaultFrequencyTime = $this->factory->getParameter('sms_frequency_time');
     /** @var \Mautic\LeadBundle\Entity\FrequencyRuleRepository $frequencyRulesRepo */
     $frequencyRulesRepo = $this->leadModel->getFrequencyRuleRepository();
     $leadIds = $lead->getId();
     $dontSendTo = $frequencyRulesRepo->getAppliedFrequencyRules('sms', $leadIds, null, $defaultFrequencyNumber, $defaultFrequencyTime);
     if (!empty($dontSendTo) and $dontSendTo[0]['lead_id'] != $lead->getId()) {
         $metadata = $this->smsApi->sendSms($leadPhoneNumber, $smsEvent->getContent());
     }
     // If there was a problem sending at this point, it's an API problem and should be requeued
     if ($metadata === false) {
         return $event->setResult(false);
     }
     $this->smsModel->createStatEntry($sms, $lead);
     $this->smsModel->getRepository()->upCount($smsId);
     $event->setChannel('sms', $sms->getId());
     $event->setResult(['type' => 'mautic.sms.sms', 'status' => 'mautic.sms.timeline.status.delivered', 'id' => $sms->getId(), 'name' => $sms->getName(), 'content' => $tokenEvent->getContent()]);
 }
Ejemplo n.º 2
0
 /**
  * @param CampaignExecutionEvent $event
  */
 public function onCampaignTriggerAction(CampaignExecutionEvent $event)
 {
     $lead = $event->getLead();
     if ($this->leadModel->isContactable($lead, 'notification') !== DoNotContact::IS_CONTACTABLE) {
         return $event->setFailed('mautic.notification.campaign.failed.not_contactable');
     }
     // If lead has subscribed on multiple devices, get all of them.
     /** @var \Mautic\NotificationBundle\Entity\PushID[] $pushIDs */
     $pushIDs = $lead->getPushIDs();
     $playerID = [];
     foreach ($pushIDs as $pushID) {
         $playerID[] = $pushID->getPushID();
     }
     if (empty($playerID)) {
         return $event->setFailed('mautic.notification.campaign.failed.not_subscribed');
     }
     $notificationId = (int) $event->getConfig()['notification'];
     /** @var \Mautic\NotificationBundle\Entity\Notification $notification */
     $notification = $this->notificationModel->getEntity($notificationId);
     if ($notification->getId() !== $notificationId) {
         return $event->setFailed('mautic.notification.campaign.failed.missing_entity');
     }
     if ($url = $notification->getUrl()) {
         $url = $this->notificationApi->convertToTrackedUrl($url, ['notification' => $notification->getId(), 'lead' => $lead->getId()]);
     }
     $tokenEvent = $this->dispatcher->dispatch(NotificationEvents::TOKEN_REPLACEMENT, new TokenReplacementEvent($notification->getMessage(), $lead, ['channel' => ['notification', $notification->getId()]]));
     $sendEvent = $this->dispatcher->dispatch(NotificationEvents::NOTIFICATION_ON_SEND, new NotificationSendEvent($tokenEvent->getContent(), $notification->getHeading(), $lead));
     $response = $this->notificationApi->sendNotification($playerID, $sendEvent->getMessage(), $sendEvent->getHeading(), $url);
     $event->setChannel('notification', $notification->getId());
     // If for some reason the call failed, tell mautic to try again by return false
     if ($response->code !== 200) {
         return $event->setResult(false);
     }
     $this->notificationModel->createStatEntry($notification, $lead);
     $this->notificationModel->getRepository()->upCount($notificationId);
     $result = ['status' => 'mautic.notification.timeline.status.delivered', 'type' => 'mautic.notification.notification', 'id' => $notification->getId(), 'name' => $notification->getName(), 'heading' => $event->getHeading(), 'content' => $event->getMessage()];
     $event->setResult($result);
 }