input(); $data = (array)json_decode($post['data']); Log::debug(print_r($data, true)); //$data = (array)($post['data']); $number = substr($data['from'], -10); $reply = ''; $session_data = []; $sessions = []; $finalised = false; $botSess = Rapiwha::where('from', $data['from'])->where('created_at', '>=', date("Y-m-d H:i:s", strtotime("now")-88200 ) )->orderby('created_at', 'desc')->first(); if($botSess) { $session_data = (array)json_decode($botSess->session_data); if(isset($session_data['session'])) { $sess = $session_data['session_data']; $reply = 'Greetings from CREST,'."\n\n"; $reply .= 'We can see that you have registered for '.ucwords($sess->type).' ('.$sess->number.') on '.date("d M Y", strtotime($sess->start_date)). ' at ' . $sess->venue."\n\n"; //$finalised = true; } } if(!$botSess && !$finalised) { $response = LambdaRequest::getrequest('ums', 'contact/get_all', ['source'=>'dynamodb', 'search' => json_encode(['phone' => $number]), 'fields' => json_encode(['session_code', 'present']) ], env('LAMBDA_ACCESS_TOKEN', 'default_value') ); $users = []; if( sizeof($response->data->items) > 0 ) { foreach($response->data->items as $item) { if( isset($item->session_code) ) { $sessions_codes[] = $item->session_code; $users[$item->session_code] = $item->id; } } $sessions = DB::table('sessions')->wherein('code', $sessions_codes)->where('start_date', '>=', date("Y-m-d 00:00:00", strtotime("now") ) )->orderby('created_at', 'desc')->get(); if(sizeof($sessions->toArray()) == 0) { $sessions = DB::table('sessions')->wherein('code', $sessions_codes)->orderby('created_at', 'desc')->get(); } if($sessions->count() > 1) { $reply = 'Greetings from CREST,'."\n\n"; $reply .= 'We can see that you have registered for multiple future Crest events. Please reply for which crest session you want to communicate'."\n\n"; $session_data['available_sessions'] = $sessions->toArray(); $session_data['available_users'] = $users; $count = 1; foreach($sessions->toArray() as $sess) { $session_data['session_selection'] = [(string)$count => $sess->code ]; $reply .= 'Reply '.$count.' if you want to select '.ucwords($sess->type).' ('.$sess->number.') on '.date("d M Y", strtotime($sess->start_date)). ' at ' . $sess->venue."\n\n"; $count++; } $finalised = true; } else { $sess = $sessions->toArray(); $sess = $sess[0]; $session_data['session'] = $sess->code; $session_data['session_data'] = $sess; $session_data['user'] = $users[$sess->code]; $reply = 'We can see that you have registered for '.ucwords($sess->type).' ('.$sess->number.') on '.date("d M Y", strtotime($sess->start_date)). ' at ' . $sess->venue."\n\n"; } } else { $reply = 'Thank you for your interest in Crest. we can see that you have not registered for amy future Crest event.'; $finalised = true; } } //echo '
'; print_r($session_data); exit;

        if(!isset($session_data['session']) && !$finalised ) {
            $count = 1;
            foreach($session_data['available_sessions'] as $sess) {
                if($count == $data['text']) {
                    $session_data['session'] = $sess->code;
                    $session_data['session_data'] = $sess;
                    $session_data['user'] = $session_data['available_users']->{$sess->code};
                    $reply = 'You have selected '.ucwords($sess->type).' ('.$sess->number.') on '.date("d M Y", strtotime($sess->start_date)). ' at ' . $sess->venue."\n\n";
                    $data['text'] = 'hi';
                    //$finalised = true;
                }
                $count++;
            }
        }

        if(isset($session_data['session']) && !$finalised) {
            switch(Str::slug($data['text'], '_')) {
                case '1':
                    $reply = 'Here is the link to the session website: ' . $domain . 'microsite/' . $session_data['session_data']->type . '-' . $session_data['session_data']->code;
                    break;
                case '2':
                    $reply = 'Please find the details below'."\n";
                    $reply .= 'Venue: ' . $session_data['session_data']->venue . "\n";
                    $reply .= 'Address: ' . $session_data['session_data']->address . "\n\n";
                    $reply .= 'Timings:' . "\n";
                    $dates = json_decode($session_data['session_data']->dates);
                    foreach($dates->date as $i => $date){
                        $reply .= date("d M, Y", strtotime($date) ) . ' at ' . date("h:iA", strtotime($dates->start[$i]) ) .' - ' . date("h:iA", strtotime($dates->end[$i]) ) . "\n";
                    }
                    break;
                case '3':
                    $reply = 'Here is the link to download session brochure: ' . $domain . 'brochure/' . $session_data['session_data']->code;
                    break;
                case '4':
                    $response = LambdaRequest::getrequest('ums', 'contact/get_one', ['id'=>$session_data['user']], env('LAMBDA_ACCESS_TOKEN', 'default_value') );
                    $doctor = $response->data->item;
                    $output = Creation::certificate($session_data['session_data'], $doctor);
                    $filename = $output['filename'];
                    DB::table('raphiwa_send')->insert(['to'=> $data['from'], 'text' => $domain . 'storage/certificate/'.$session_data['session_data']->type.'/'.$filename ]);
                    $reply = 'We will generate your certificate and send it to you in a while, please give us few minutes.';
                    break;

                default:
                    $reply .= 'Please select what do you want to do'."\n";
                    $reply .= 'Reply 1 if you want link to the session website'."\n";
                    $reply .= 'Reply 2 if you want to know about session location and timings'."\n";
                    //$reply .= 'Reply 3 if you want to know about session agenda and speakers'."\n\n";
                    $reply .= 'Reply 3 if you want to download session brochure'."\n";
                    $reply .= 'Reply 4 if you want to download your certificate'."\n\n";
                    $reply .= 'Please give us upto few minutes to reply to your requests.'."\n";
                    break;
            }
        }

        $data['session_data'] = json_encode($session_data);
        //echo $reply; echo '
'; print_r($data); exit;
        if($botSess) {
            unset($data['event']);
            $data['msg_history'] = json_decode($botSess->msg_history);
            $data['msg_history'][] = $data['text'];
            $data['msg_history'] = json_encode($data['msg_history']);
            Rapiwha::where('id', $botSess->id)->update($data);
        } else {
            $data['msg_history'] = json_encode([$data['text']]);
            Rapiwha::create($data);
        }
        
        return response()->json(['autoreply' => $reply]);

        // if(Str::slug($data['text'], '_') == 'hi_pulmoflix') {
        //     return response()->json(['autoreply' => 'Thank you for showing interest in Pulmoflix. To access the website use the link https://pulmoflix.com']);
        // } elseif(Str::slug($data['text'], '_') == 'hi_napcon') {
        //     return response()->json(['autoreply' => 'Dear Doctor, Please click on the link and download the NAPCON 2022 Application, for a better NAPCON Experience'."\n\n".'Android:'."\n".'https://play.google.com/store/apps/details?id=com.startawiz.napcon'."\n\n".'iOS:'."\n".'https://apps.apple.com/in/app/napcon/id1615285887']);
        // } else {
        //     return response()->json([]);
        // }
        
    }
}