Example #1
0
 /**
  * Create a subscription
  * 
  * @param  array $subscription
  * @throws \Exception
  * @return $this
  */
 private function create($subscription)
 {
     if (empty($subscription['content']) || empty($subscription['to_lead']) && empty($subscription['to_channel'])) {
         throw new \Exception('Cannot create subscription. A required field is missing!');
     }
     // If this notification has already been created. Just find the notification
     if (!empty($subscription['unique_id'])) {
         $this->find($subscription['unique_id']);
     }
     // Create the notification
     if (is_null($this->current_message) || !$this->current_message) {
         $model = new Model();
         $subscription['content'] = $model->parseWithoutSave($subscription['content']);
         $this->current_message = Message::firstOrCreate($subscription);
     }
     return $this;
 }
Example #2
0
 /**
  * Response sender message
  *
  * @param $nodes
  * @param null $lead_id
  */
 public function response($nodes, $lead_id = null)
 {
     if (is_null($lead_id)) {
         $lead_id = $this->conversation->get('lead_id');
     }
     foreach ($nodes as $node) {
         /** New wait */
         if (!empty($node->wait)) {
             $this->storage->set($lead_id, '_wait', $node->wait);
         }
         // Backward compatibility
         if (array_key_exists('type', $node->answers)) {
             $node->answers = [$node->answers];
         }
         foreach ($node->answers as $answer) {
             /** Process dynamic content */
             if (isset($answer['type'])) {
                 // Backward compatibility
                 if (isset($answer['callback'])) {
                     $answer['content'] = $answer['callback'];
                 }
                 if (giga_match('%SerializableClosure%', $answer['content'])) {
                     $answer['content'] = $this->serializer->unserialize($answer['content']);
                 }
                 $return = DynamicParser::parse($answer);
                 // If the callback return, we'll send that message to user.
                 if ($return != null || !empty($return)) {
                     $answer = $this->model->parseWithoutSave($return);
                     // Answer == 0 means that answers is already parsed and it's a single message.
                     if ($answer != false) {
                         $this->request->sendMessages($answer);
                     } else {
                         $this->request->sendMessage($return);
                     }
                     continue;
                 }
             }
             $this->request->sendMessage($answer);
         }
     }
 }