src/AppBundle/Controller/GenericController.php line 27

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. //use AppBundle\Controller\SecurityController;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Twilio\Rest\Client;
  11. use AppBundle\Helpers\MailerHelper;
  12. use Twilio\Rest\Client as Twilio;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use AppBundle\Helpers\Constants;
  15. use AppBundle\Shame\Shame;
  16. class GenericController extends Controller
  17. {
  18.     /**
  19.      * @Route("/", name="index")
  20.      * @param Request $request
  21.      * @return string
  22.      */
  23.     public function indexAction(Request $request)
  24.     {
  25.         return $this->render('Default/index.html.twig');
  26.         $shame = new Shame($this);
  27.         return (new JsonResponse($shame->getUsers()));
  28.     }
  29.     private function init() {
  30.         $this->shame = new Shame($this);
  31.     }
  32.     public function getP($name)
  33.     {
  34.         try {
  35.             return $this->getParameter($name);
  36.         } catch (\Exception $e) {
  37.         }
  38.         try {
  39.             return $this->getParameter(strtoupper($name));
  40.         } catch (\Exception $e) {
  41.         }
  42.         try {
  43.             return $this->getParameter(strtolower($name));
  44.         } catch (\Exception $e) {
  45.             exit($e->getMessage() . " Both upper and lower case variable names were attempted");
  46.         }
  47.     }
  48.     /**
  49.      * @Route("/generic/call", name="generic-call")
  50.      * @param Request $request
  51.      * @return string
  52.      *
  53.      * Receive a Call
  54.      *
  55.      */
  56.     public function callAction(Request $request)
  57.     {
  58.         $To $request->get('Called');
  59.         $From $request->get('Caller');
  60.         $Digits $request->get('Digits');
  61.         $shame = new Shame($this);
  62.         $shame->callLog($request);
  63.         $response = new Response();
  64.         if ((!$To) or (!$From)) {
  65.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  66.             return $response;
  67.         }
  68.         $main_greeting =
  69.             '<?xml version="1.0" encoding="UTF-8"?>
  70. <Response>
  71. <Gather action="/generic/call" finishOnKey="#" timeout="5">
  72.     <Say voice="alice">Hello!  You have reached the Support Desk. 
  73.     If you have a support issue number please enter it now
  74.     followed by the pound sign.</Say>
  75.     <Pause length="10"/>
  76.     <Say voice="alice">
  77.     If you have a support issue number please enter it now
  78.     followed by the pound sign.</Say>
  79.     <Say voice="alice">
  80.     If you have a support issue number please enter it now
  81.     followed by the pound sign.</Say>
  82.     <Pause length="10"/>
  83.     </Gather>
  84.     <Say voice="alice">Thank you for calling, goodbye.</Say>
  85.     </Gather>
  86. </Response>';
  87.         $alt_greeting =
  88.             '<?xml version="1.0" encoding="UTF-8"?>
  89. <Response>
  90. <Gather action="/generic/call" finishOnKey="#" timeout="5">
  91.     <Say voice="alice">I\'m sorry, that was not a valid support issue number.  
  92.     If you have a support issue number please enter it now
  93.     followed by the pound sign.</Say>
  94.     <Pause length="10"/>
  95.     <Say voice="alice">
  96.     If you have a support issue number please enter it now
  97.     followed by the pound sign.</Say>
  98.     <Say voice="alice">
  99.     If you have a support issue number please enter it now
  100.     followed by the pound sign.</Say>
  101.     <Pause length="10"/>
  102.     </Gather>
  103.     <Say voice="alice">Thank you for calling, goodbye.</Say>
  104.     </Gather>       
  105. </Response>';
  106.         $alt2_greeting =
  107.             '<?xml version="1.0" encoding="UTF-8"?>
  108. <Response>
  109. <Gather action="/generic/call" finishOnKey="#" timeout="5">
  110.     <Say voice="alice">I\'m sorry, that was not a valid selection.  
  111.     If you have a support issue number please enter it now
  112.     followed by the pound sign.</Say>
  113.     <Pause length="10"/>
  114.     <Say voice="alice">
  115.     If you have a support issue number please enter it now
  116.     followed by the pound sign.</Say>
  117.     <Say voice="alice">
  118.     If you have a support issue number please enter it now
  119.     followed by the pound sign.</Say>
  120.     <Pause length="10"/>
  121.     </Gather>
  122.     <Say voice="alice">Thank you for calling, goodbye.</Say>
  123.     </Gather>       
  124. </Response>';
  125.         if ( ($Digits === false) or ($Digits == '') or ($Digits == '*') ) {
  126.             $response->setContent($main_greeting);
  127.             return $response;
  128.         }
  129.         if (strlen($Digits) > 1) {
  130.             $team_id $shame->getTicketTeam($Digits);
  131.             if (!$team_id) {
  132.                 if (empty($Digits)) {
  133.                     $response->setContent($alt_greeting);
  134.                     return $response;
  135.                 }
  136.             }
  137.         } else {
  138.             $team_id=$Digits;
  139.         }
  140.         switch ($team_id) {
  141.             case 1$team 'A Team';break;
  142.             case 2$team 'B Team';break;
  143.             case 3$team 'C Team';break;
  144.             case 4$team 'D Team';break;
  145.             default:
  146.                 $response->setContent($alt2_greeting);
  147.                 return $response;
  148.         }
  149.         $text "Incoming Call\nTeam: $team\n";
  150.         $response = new Response();
  151.         $data = [
  152.             'CallStatus',
  153.             'CallDuration',
  154.             'CallerName',
  155.             'Caller',
  156.             'CallerCity',
  157.             'CallerCountry',
  158.             'CallerState',
  159.             'CallerZip',
  160.         ];
  161.         foreach ($data as $datum) {
  162.             if ($request->get($datum)) {
  163.                 $text .= "$datum:".$request->get($datum) . "\n";
  164.             }
  165.         }
  166.         $this->send($team_id$text);
  167.         $shame->callLog($request0$team_id0);
  168.         $twiml =
  169.             '<?xml version="1.0" encoding="UTF-8"?>
  170. <Response>
  171.     <Play>https://chargebacks911.com/Apps/generichotlinegreeting.mp3?x=2</Play>
  172.     <Enqueue waitUrl="/generic/hold-music">' $team '</Enqueue> 
  173. </Response>';
  174.         $response->setContent($twiml);
  175.         return $response;
  176.     }
  177.     /**
  178.      * @Route("/generic/recording-events", name="generic-recording-events")
  179.      * @param Request $request
  180.      * @return string
  181.      *
  182.      * Receive a Callback Event
  183.      *
  184.      */
  185.     public function recordingEventAction(Request $request)
  186.     {
  187.         $response = new Response();
  188.         $dir $request->get('RecordingDuration');
  189.         $shame = new Shame($this);
  190.         $team_id $shame->getCallTeam($request->get('CallSID'));
  191.         if ($dir 20) {
  192.             $text "Call Recording ($dir seconds)\n";
  193.             $text .= $request->get('RecordingUrl');
  194.             $this->send($team_id$text);
  195.         }
  196.         return $response->setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  197.     }
  198.     /**
  199.      * @Route("/generic/dial-status-events", name="generic-dial-status-events")
  200.      * @param Request $request
  201.      * @return Response
  202.      *
  203.      * Receive a Callback Event
  204.      *
  205.      */
  206.     public function dialEventAction(Request $request)
  207.     {
  208.         $response = new Response();
  209.         return $response->setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  210.     }
  211.     /**
  212.      * @Route("/generic/call-status-events", name="generic-call-status-events")
  213.      * @param Request $request
  214.      * @return string
  215.      *
  216.      * Receive a Callback Event
  217.      *
  218.      */
  219.     public function callEventAction(Request $request)
  220.     {
  221.         $response = new Response();
  222.         $shame = new Shame($this);
  223.         $shame->callLog($request);
  224.         return $response->setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  225.     }
  226.     /**
  227.      * @Route("/generic/hold-music")
  228.      * @param Request $request
  229.      * @return string
  230.      *
  231.      * Hold Music
  232.      *
  233.      */
  234.     public function holdMusicAction(Request $request)
  235.     {
  236.         $response = new Response();
  237.         $shame = new Shame($this);
  238.         $shame->callLog($request);
  239.         // Send a Text and Initiate agent dialing before returning hold music to play.
  240.         $data = [
  241.             'CallStatus',
  242.             'CallerName',
  243.             'Caller',
  244.             'CallerCity',
  245.             'CallerCountry',
  246.             'CallerState',
  247.             'CallerZip',
  248.             'CurrentQueueSize',
  249.             'QueuePosition',
  250.             'QueueTime',
  251.             'AvgQueueTime',
  252.         ];
  253.         $text "Caller in Queue!\n";
  254.         foreach ($data as $datum) {
  255.             if ($request->get($datum)) {
  256.                 $text .= "$datum:".$request->get($datum) . "\n";
  257.             }
  258.         }
  259.         $twilio_sid getenv('GENERIC_SID');
  260.         $twilio_token getenv('GENERIC_TOKEN');
  261.         $twilio = new Twilio($twilio_sid$twilio_token);
  262.         $whatsapp_sid getenv('WHATSAPP_SID');
  263.         $whatsapp_token getenv('WHATSAPP_TOKEN');
  264.         $whatsapp = new Twilio($whatsapp_sid$whatsapp_token);
  265.         $who $shame->getUsers();
  266.         $activeCounter 0;
  267.         $oncallCounter 0;
  268.         $team_id $shame->getCallTeam($request->get('CallSid'));
  269.         if (!$team_id) {
  270.             $team_id 0;
  271.         }
  272.         foreach ($who as $he) {
  273.             $phone $he['phone'];
  274.             $name $he['name'];
  275.             $team $he['team_id'];
  276.             if ($team == $team_id) {
  277.                 $isActive $he['is_active'];
  278.                 $isOncall $he['is_oncall'];
  279.                 if ($isActive or $isOncall) {
  280.                     $activeCounter++;
  281.                     if ($this->isWhatsApp($phone)) {
  282.                         $what $whatsapp->messages->create(
  283.                             $phone,
  284.                             array(
  285.                                 'from' => $this->getp("TN_WHATSAPP_SUPPORTDESK"),
  286.                                 'body' => $text,
  287.                             )
  288.                         );
  289.                     } else {
  290.                         $twilio->messages->create(
  291.                             $phone,
  292.                             array(
  293.                                 'from' => $this->getp("TN_GENERIC_SUPPORTDESK"),
  294.                                 'body' => $text
  295.                             )
  296.                         );
  297.                     }
  298.                 }
  299.                 if ($isOncall) {
  300.                     $oncallCounter++;
  301.                     $phone str_replace('whatsapp:','',$phone);
  302.                     $call $twilio->calls
  303.                         ->create($phone,  // to
  304.                             $this->getp("TN_GENERIC_SUPPORTDESK"),     // from
  305.                             array("url" => "https://apichost.com/generic/agent")
  306.                         );
  307.                 }
  308.             }
  309.         }
  310.         $everyone = [];
  311.         if ($oncallCounter == 0) {
  312.             // get the team members
  313.             foreach ($who as $he) {
  314.                 $phone $he['phone'];
  315.                 $name $he['name'];
  316.                 $team $he['team_id'];
  317.                 if ($team == $team_id) {
  318.                     $everyone[] = $phone;
  319.                 }
  320.             }
  321.             $text "WARNING - NO ONE IS ON-CALL - SO I'M CALLING EVERYBODY!!\n\n" $text;
  322.             foreach ($everyone as $phone) {
  323.                 if ($this->isWhatsApp($phone)) {
  324.                     $what $whatsapp->messages->create(
  325.                         $phone,
  326.                         array(
  327.                             'from' => $this->getp("TN_WHATSAPP_SUPPORTDESK"),
  328.                             'body' => $text,
  329.                         )
  330.                     );
  331.                 } else {
  332.                     $twilio->messages->create(
  333.                         $phone,
  334.                         array(
  335.                             'from' => $this->getp("TN_GENERIC_SUPPORTDESK"),
  336.                             'body' => $text
  337.                         )
  338.                     );
  339.                 }
  340.             }
  341.             foreach ($everyone as $phone) {
  342.                 $phone str_replace('whatsapp:','',$phone);
  343.                 $call $twilio->calls
  344.                     ->create($phone,  // to
  345.                         $this->getp("TN_GENERIC_SUPPORTDESK"),     // from
  346.                         array("url" => "https://apichost.com/generic/agent")
  347.                     );
  348.             }
  349.         }
  350.         return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
  351. <Response>
  352.     <Play>' $this->getp("GENERIC_HOLDMUSIC") . '</Play>
  353. </Response>');
  354.     }
  355.     /**
  356.      * @Route("/generic/agent-hold-music")
  357.      * @param Request $request
  358.      * @return string
  359.      *
  360.      * Agent Hold Music
  361.      */
  362.     public function agentHoldMusicAction(Request $request)
  363.     {
  364.         $response = new Response();
  365.         return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
  366. <Response>
  367.     <Play>' $this->getp("GENERIC_HOLDMUSIC") . '</Play>
  368. </Response>');
  369.     }
  370.     /**
  371.      * @Route("/generic/agent")
  372.      * @param Request $request
  373.      * @return string
  374.      *
  375.      * Agent grab caller in Queue
  376.      *
  377.      */
  378.     public function agentAction(Request $request)
  379.     {
  380.         $response = new Response();
  381.         $shame = new Shame($this);
  382.         $team_id $shame->getUserTeam($request->get('Called'));
  383.         $team $shame->getTeamName($team_id);
  384.         if ($request->get('Digits')) {
  385.             return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
  386. <Response>
  387.     <Dial record="record-from-ringing-dual"
  388.     recordingStatusCallback="/generic/recording-events"
  389.     recordingStatusCallbackEvent="completed"
  390.     method="GET">
  391.        <Queue>' $team '</Queue>
  392.     </Dial>       
  393. </Response>');
  394.         } else {
  395.             return $response->setContent('<?xml version="1.0" encoding="UTF-8"?>
  396. <Response>
  397.     <Gather>
  398.     <Say>I have a call for you. Please press 1 at any time to access the queue.  If you hear music, then the queue is empty. You may stay on the line and if someone calls they will be transferred to you immediately.</Say>
  399.     </Gather>
  400.     <Say>Goodbye!</Say>
  401. </Response>');
  402.         }
  403.     }
  404.     public function isWhatsApp($phone) {
  405.         return strpos($phone,'whatsapp') === 0;
  406.     }
  407.     public function send($team_id$text)
  408.     {
  409.         $shame = new Shame($this);
  410.         $shame->storeText($text);
  411.         $this->output '';
  412.         $twilio_sid getenv('GENERIC_SID');
  413.         $twilio_token getenv('GENERIC_TOKEN');
  414.         $twilio = new Twilio($twilio_sid$twilio_token);
  415.         $whatsapp_sid getenv('WHATSAPP_SID');
  416.         $whatsapp_token getenv('WHATSAPP_TOKEN');
  417.         $whatsapp = new Twilio($whatsapp_sid$whatsapp_token);
  418.         $who $shame->getUsers();
  419.         $cnt 0;
  420.         foreach ($who as $he) {
  421.             $phone $he['phone'];
  422.             $name $he['name'];
  423.             $team $he['team_id'];
  424.             if ($team == $team_id) { // ($data == 'generic-supportdesk-active') { // add teams later
  425.                 $isActive $he['is_active'];
  426.                 $isOncall $he['is_oncall'];
  427.                 if ($isActive or $isOncall) {
  428.                     $cnt++;
  429.                     if (!strpos($text"from $name")) {
  430.                         if ($this->isWhatsApp($phone)) {
  431.                             $what $whatsapp->messages->create(
  432.                                 $phone,
  433.                                 array(
  434.                                     'from' => $this->getp("TN_WHATSAPP_SUPPORTDESK"),
  435.                                     'body' => $text,
  436.                                 )
  437.                             );
  438.                         } else {
  439.                             $twilio->messages->create(
  440.                                 $phone,
  441.                                 array(
  442.                                     'from' => $this->getp("TN_GENERIC_SUPPORTDESK"),
  443.                                     'body' => $text
  444.                                 )
  445.                             );
  446.                         }
  447.                     }
  448.                 }
  449.             }
  450.         }
  451.         return $cnt;
  452.     }
  453.     public function sendTo($text$recipients)
  454.     {
  455.         //$shame->storeText($text);
  456.         $twilio_sid getenv('GENERIC_SID');
  457.         $twilio_token getenv('GENERIC_TOKEN');
  458.         $twilio = new Twilio($twilio_sid$twilio_token);
  459.         $whatsapp_sid getenv('WHATSAPP_SID');
  460.         $whatsapp_token getenv('WHATSAPP_TOKEN');
  461.         $whatsapp = new Twilio($whatsapp_sid$whatsapp_token);
  462.         foreach ($recipients as $phone) {
  463.             if ($this->isWhatsApp($phone)) {
  464.                 $what $whatsapp->messages->create(
  465.                     $phone,
  466.                     array(
  467.                         'from' => $this->getp("TN_WHATSAPP_SUPPORTDESK"),
  468.                         'body' => $text,
  469.                     )
  470.                 );
  471.             } else {
  472.                 $twilio->messages->create(
  473.                     $phone,
  474.                     array(
  475.                         'from' => $this->getp("TN_GENERIC_SUPPORTDESK"),
  476.                         'body' => $text
  477.                     )
  478.                 );
  479.             }
  480.         }
  481.     }
  482.     /*
  483.      * 
  484.      * SMS & WhatsApp
  485.      * 
  486.      */
  487.     /**
  488.      * @Route("/generic/text", name="generic-text")
  489.      * @param Request $request
  490.      * @return string
  491.      *
  492.      * Handle an incoming text command
  493.      * Or
  494.      * Receive a 2FA text from Twilio or Nexmo and update the 2FA table
  495.      *
  496.      */
  497.     public function textAction(Request $request)
  498.     {
  499.         /*
  500.         Twilio sends To, From and Body
  501.         Nexmo sends to, msisdn, and text
  502.         */
  503.         $shame = new Shame($this);
  504.         $To $request->get('to');
  505.         $Body $request->get('text');
  506.         $From $request->get('msisdn');
  507.         if (!$To) {
  508.             $To $request->get('To');
  509.             $Body $request->get('Body');
  510.             $From $request->get('From');
  511.         }
  512. //        $To = '+' . ltrim($To, "+"); // allow GET testing
  513. //
  514. //        $From = '+' . ltrim($From, "+");
  515.         $response = new Response();
  516.         if ((!$To) or (!$Body)) {
  517.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  518.             return $response;
  519.         }
  520.         switch ($To) {
  521.             // Hotlines
  522.             case $this->getp("TN_GENERIC_SUPPORTDESK"):
  523.             case $this->getp("TN_WHATSAPP_SUPPORTDESK"):
  524.                 /** @var Shame $shame
  525.                  *
  526.                  */
  527.                 $userMeta $shame->getUser($From);
  528.                 if ($userMeta != false) {
  529.                     // shortcuts
  530.                     switch (strtoupper($Body)) {
  531.                         case "SI":
  532.                             $Body "show issues";
  533.                             break;
  534.                         case "ST":
  535.                             $Body "show teams";
  536.                             break;
  537.                         case "SC":
  538.                             $Body "show calls";
  539.                             break;
  540.                         case "SU":
  541.                             $Body "show users";
  542.                             break;
  543.                         case "SP":
  544.                             $Body "show users";
  545.                             break;
  546.                         case "who":
  547.                             $Body "W";
  548.                             break;
  549.                     }
  550.                     $bArr explode(' '$Body ' 0 0 0 0 0 0');
  551.                     if (empty($bArr[0])) {
  552.                         exit(Constants::SMS_INVALID);
  553.                     }
  554.                     $arg $bArr[1] ? $bArr[1] : $From;
  555.                     if ((strtoupper($bArr[0]) == 'A') or (strtoupper($bArr[0]) == 'O')) {
  556.                         $userMeta $shame->getUser($arg);
  557.                     }
  558.                     if ($userMeta === false) {
  559.                         // this is a new user, add this person
  560.                         //$shame->storeUser($arg);
  561.                         exit(Constants::SMS_NO_FEATURE);
  562.                     }
  563.                     switch (strtoupper($bArr[0])) {
  564.                         case 'ISSUES':
  565.                             $this->text($shame->showIssues());
  566.                             break;
  567.                         case 'TEAMS':
  568.                             $this->text($shame->showTeams());
  569.                             break;
  570.                         case 'CALLS':
  571.                             $this->text($shame->showCalls());
  572.                             break;
  573.                         case 'USERS':
  574.                             $this->text($shame->showUsers());
  575.                             break;
  576.                         case 'PRODUCTS':
  577.                             $this->text($shame->showProducts());
  578.                             break;
  579.                         case 'EMAIL':
  580.                         case 'EMAILS':
  581.                         case 'MAIL':
  582.                             $this->text($shame->showEmail($bArr[1]));
  583.                             break;
  584.                         case 'SHOW':
  585.                             switch (strtoupper($bArr[1])) {
  586.                                 case 'ISSUES':
  587.                                     $this->text($shame->showIssues());
  588.                                     break;
  589.                                 case 'TEAMS':
  590.                                     $this->text($shame->showTeams());
  591.                                     break;
  592.                                 case 'CALLS':
  593.                                     $this->text($shame->showCalls());
  594.                                     break;
  595.                                 case 'USERS':
  596.                                     $this->text($shame->showUsers());
  597.                                     break;
  598.                                 case 'PRODUCTS':
  599.                                     $this->text($shame->showProducts());
  600.                                     break;
  601.                                 case 'EMAIL':
  602.                                 case 'EMAILS':
  603.                                 case 'MAIL':
  604.                                     $this->text($shame->showEmail($bArr[2]));
  605.                                     break;
  606.                             }
  607.                             break;
  608.                         case '?'// Reply with Help Twig
  609.                             $this->text($this->renderView('help.html.twig'));
  610.                             break;
  611.                         case 'H'// Reply with Long Help Twig
  612.                         case 'HELP':
  613.                             $this->text($this->renderView('longhelp.html.twig'));
  614.                             break;
  615.                         case 'I'// Assign/Reopen Issue
  616.                         case 'ISSUE'// ISSUE 222 3
  617.                             $user $shame->getUserName($From);
  618.                             if (!$user) {
  619.                                 $user $From;
  620.                             }
  621.                             $team $shame->getTeamName($bArr[2]);
  622.                             if ($team === false) {
  623.                                 $this->text("Invalid Team");
  624.                             }
  625.                             $op $shame->getTicketOP($bArr[1]);
  626.                             if ($op === false) {
  627.                                 $this->text("Invalid Issue ID");
  628.                             }
  629.                             $shame->assignIssue($bArr[1],$bArr[2]);
  630.                             $this->generic_send("###############\nIssue ".$bArr[1]. "  $op has been opened and assigned the $team team");
  631.                             $this->text("OK");
  632.                             break;
  633.                         case 'A'// Active Team
  634.                             $this->text ($shame->setActive($arg));
  635.                             break;
  636.                         case 'X'// Exit Active Team
  637.                             $this->text($shame->setInActive($arg));
  638.                             break;
  639.                         case 'O'// OnCall Team
  640.                             $this->text($shame->setOnCall($arg));
  641.                             break;
  642.                         case 'N'// Not OnCall
  643.                             $this->text($shame->setNotOnCall($arg));
  644.                             break;
  645.                         case 'C'// Close Issue
  646.                         case 'CLOSE'// Close Issue
  647.                             $this->text($shame->closeIssue($bArr[1]));
  648.                             break;
  649.                         case 'W'// Who's OnCall and Active
  650.                         case 'E'// Who's Everyone
  651.                             if (strtoupper($bArr[0])=='W') {
  652.                                 $output "Who is Active/On Call\n";
  653.                             } else {
  654.                                 $output "List of All Users\n";
  655.                             }
  656.                             $who $shame->getUsers();
  657.                             foreach ($who as $he) {
  658.                                 $name '('.$he['name'].')';
  659.                                 if (true) { // ($data == $ACTIVE) { // break this into teams
  660.                                     $isActive $he['is_active'];
  661.                                     $isOncall $he['is_oncall'];
  662.                                     if (($isActive or $isOncall) or strtoupper($bArr[0])=='E') {
  663.                                         $output .= "\n$name - ";
  664.                                         if ($isActive) {
  665.                                             $output .= "Active ";
  666.                                         } else {
  667.                                             $output .= "- ";
  668.                                         }
  669.                                         if ($isOncall) {
  670.                                             $output .= "Call";
  671.                                         } else {
  672.                                             $output .= "-";
  673.                                         }
  674.                                     }
  675.                                 }
  676.                             }
  677.                             if (strtoupper($bArr[0])=='W') {
  678.                                 $output .= "\n\nSend E for Everyone";
  679.                             }
  680.                             $this->text($output);
  681.                             break;
  682.                         case 'R':
  683.                         case 'REPLY'// REPLY 22 Hey!
  684.                             // Get the sender of issue $bArr[1], then email the body of the email to him and then treat it like default text too
  685.                             $m = new MailerHelper();
  686.                             $user $shame->getUserName($From);
  687.                             if (!$user) {
  688.                                 $user $From;
  689.                             }
  690.                             $team $shame->getUserTeamName($From);
  691.                             $email $shame->getTicketOP($bArr[1]);
  692.                             $issue_id $bArr[1];
  693.                             if ($email !== false) {
  694.                                 $body "$Body<br/><br/>This is a mobile response from $user<br/><br/>Reply or call " $this->getp('TN_GENERIC_SUPPORT') . " using PIN " $issue_id;
  695.                                 $subject 'Support (' $issue_id ') - ' $user;
  696.                                 $shame->storeEmail($team$subject$body$email$issue_id);
  697.                                 $response $m->sendEmail([$email],
  698.                                     $this->getp('EM_GENERIC_SUPPORT'), $subject,
  699.                                     $body);
  700.                                 $cnt $this->generic_send("Msg from $user:\n$Body");
  701.                                 $this->text("SendEmail: $response");
  702.                             } else {
  703.                                 exit(Constants::SMS_NO_OP);
  704.                             }
  705.                             break;
  706.                         default:
  707.                             if (strlen($Body) > 1) {
  708.                                 $user $shame->getUserName($From);
  709.                                 if (!$user) {
  710.                                     $user $From;
  711.                                 }
  712.                                 switch ($To) {
  713.                                     case  $this->getp("TN_GENERIC_SUPPORTDESK"):
  714.                                     case  $this->getp("TN_WHATSAPP_SUPPORTDESK"):
  715.                                         $cnt $this->generic_send("Msg from $user:\n$Body");
  716.                                         break;
  717.                                     default:
  718.                                         $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  719.                                         return $response;
  720.                                 }
  721.                                 $sent $this->output;
  722.                                 $this->text("Sent to:\n$sent\n\n");
  723.                             } else {
  724.                                 exit(Constants::SMS_NO_FEATURE);
  725.                             }
  726.                     }
  727.                     exit(Constants::SMS_OK);
  728.                 } else {
  729.                     exit(Constants::SMS_ACCESS_DENIED);
  730.                 }
  731.             default:
  732.                 exit(Constants::SMS_NO_RESPONSE);
  733.         }
  734.     }
  735.     public function generic_send($text)
  736.     {
  737.         $shame = new Shame($this);
  738.         $shame->storeText($text);
  739.         $this->output '';
  740.         $twilio_sid getenv('GENERIC_SID');
  741.         $twilio_token getenv('GENERIC_TOKEN');
  742.         $twilio = new Twilio($twilio_sid$twilio_token);
  743.         $whatsapp_sid getenv('WHATSAPP_SID');
  744.         $whatsapp_token getenv('WHATSAPP_TOKEN');
  745.         $whatsapp = new Twilio($whatsapp_sid$whatsapp_token);
  746.         $who $shame->getUsers();
  747.         $cnt 0;
  748.         foreach ($who as $he) {
  749.             $phone $he['phone'];
  750.             $name $he['name'];
  751.             if (true) { // Texts or announcements should be sent to all teams
  752.                 $isActive $he['is_active'];
  753.                 $isOncall $he['is_oncall'];
  754.                 if ($isActive or $isOncall) {
  755.                     $cnt++;
  756.                     if (!strpos($text"from $name")) {
  757.                         $this->output .= "$name\n";
  758.                         // If the user is a WhatsApp user, do it thataway
  759.                         if ($this->isWhatsApp($phone)) {
  760.                             $what $whatsapp->messages->create(
  761.                                 $phone,
  762.                                 array(
  763.                                     'from' => $this->getp("TN_WHATSAPP_SUPPORTDESK"),
  764.                                     'body' => $text,
  765.                                 )
  766.                             );
  767.                         } else {
  768.                             $twilio->messages->create(
  769.                                 $phone,
  770.                                 array(
  771.                                     'from' => $this->getp("TN_GENERIC_SUPPORTDESK"),
  772.                                     'body' => $text
  773.                                 )
  774.                             );
  775.                         }
  776.                     }
  777.                 }
  778.             }
  779.         }
  780.         return $cnt;
  781.     }
  782.     function text($text)
  783.     {
  784.         $output '';
  785.         $ctr=0;
  786.         if (gettype($text) == 'array') {
  787.             foreach ($text as $t) {
  788.                 $ctr++;
  789.                 //$output .= "($ctr)";
  790.                 if (gettype($t) == 'array') {
  791.                     foreach ($t as $ff => $tt) {
  792.                         if (!is_numeric($ff)) {
  793.                             $output .= $tt ' ';
  794.                         }
  795.                     }
  796.                 } else {
  797.                     $output .= $t;
  798.                 }
  799.                 $output trim($output) . "\n";
  800.             }
  801.             $text str_replace(['<','>'],['(',')'],$output);
  802.         }
  803.         exit('<?xml version="1.0" encoding="UTF-8"?><Response><Message><Body>' $text '</Body></Message></Response>');
  804.     }
  805.     /**
  806.      * @Route("/generic-whatsapp/message", name="whatsapp-message")
  807.      * @param Request $request
  808.      * @return string
  809.      *
  810.      *     ApiVersion    "2010-04-01"
  811.      *     SmsSid    "SM850e0407f4a2d0258258d56ec0dbe4b3"
  812.      *     SmsStatus    "received"
  813.      *     SmsMessageSid    "SM850e0407f4a2d0258258d56ec0dbe4b3"
  814.      *     NumSegments    "1"
  815.      *     From    "whatsapp:+17273072253"
  816.      *     To    "whatsapp:+14155238886"
  817.      *     MessageSid    "SM850e0407f4a2d0258258d56ec0dbe4b3"
  818.      *     Body    "Forks"
  819.      *     AccountSid    "AC6e2afd53042aa7c31f655fa9c64c47ce"
  820.      *     NumMedia    "0"
  821.      *
  822.      */
  823.     public function messageAction(Request $request)
  824.     {
  825.         return $this->textAction($request);
  826.     }
  827.     /**
  828.      * @Route("/generic-whatsapp/events", name="whatsapp-events")
  829.      * @param Request $request
  830.      * @return string
  831.      *
  832.      * Receive a Callback Event
  833.      *
  834.      */
  835.     public function whatsappEventAction(Request $request)
  836.     {
  837.         return (new Response("OK"));
  838.     }
  839.    
  840.     
  841.     
  842. }