MOON
Server: Apache
System: Linux ip-208-109-13-31.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els4.x86_64 #1 SMP Sat Aug 31 06:58:57 UTC 2024 x86_64
User: durgeshpandey215 (1013)
PHP: 8.1.29
Disabled: NONE
Upload Files
File: /home/durgeshpandey215/public_html/subsidycrm/app/Services/MsgService.php
<?php

namespace App\Services;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Log;

class MsgService
{
    protected $client;
    protected $authKey;

    public function __construct()
    {
        $this->client = new Client();
        $this->authKey = env('MSG91_AUTH_KEY');
    }

    //SMS
    public function sendSMS($mobileNumber, $message)
    {
        try {
            $url = 'https://api.msg91.com/api/v2/sendsms';
            $response = $this->client->post($url, [
                'json' => [
                    'sender' => 'SENDERID',
                    'route' => '4',
                    'country' => '91',
                    'sms' => [
                        [
                            'message' => $message,
                            'to' => [$mobileNumber]
                        ]
                    ]
                ],
                'headers' => [
                    'authkey' => $this->authKey,
                    'Content-Type' => 'application/json',
                ],
            ]);
            return json_decode($response->getBody(), true);
        } catch (ClientException $e) {
            Log::info($e);
            $responseBody = $e->getResponse()->getBody(true);
            return json_decode($responseBody, true);
        } catch (Exception $e) {
            Log::info($e);
            return ['error' => $e->getMessage()];
        }
    }

    public function sendFlowMessage($templateId, $shortUrl, $realTimeResponse, $recipients)
    {
        try {
            $response = $this->client->request('POST', 'https://control.msg91.com/api/v5/flow', [
                'headers' => [
                    'Accept' => 'application/json',
                    'authkey' => $this->authKey,
                    'Content-Type' => 'application/json',
                ],
                'json' => [
                    'template_id' => $templateId,
                    'short_url' => $shortUrl,
                    'realTimeResponse' => $realTimeResponse,
                    'recipients' => $recipients,
                ],
            ]);

            return json_decode($response->getBody()->getContents(), true);
        } catch (ClientException $e) {
            Log::error($e);
            $responseBody = $e->getResponse()->getBody(true);
            return json_decode($responseBody, true);
        } catch (Exception $e) {
            Log::error($e);
            return ['error' => $e->getMessage()];
        }
    }

    //WhatsApp
    public function sendWhatsAppMessageSimple($mobileNumber, $message)
    {
        try {
            $response = $this->client->request('POST', 'https://control.msg91.com/api/v5/whatsapp/whatsapp-outbound-message/', [
                'headers' => [
                    'Accept' => 'application/json',
                    'authkey' => $this->authKey,
                    'Content-Type' => 'application/json',
                ],
                'json' => [
                    'integrated_number' => '+91 9967806575', 
                    'recipient_number' => $mobileNumber,
                    'content_type' => 'text',
                    'text' => $message,
                ],
            ]);

            return json_decode($response->getBody()->getContents(), true);
        } catch (ClientException $e) {
            Log::error($e);
            $responseBody = $e->getResponse()->getBody(true);
            return json_decode($responseBody, true);
        } catch (Exception $e) {
            Log::error($e);
            return ['error' => $e->getMessage()];
        }
    }

    public function sendWhatsAppMessageDetailed($mobileNumber, $integratedNumber, $headerText, $bodyText, $footerText, $buttonText, $sections)
    {
        try {
            $response = $this->client->request('POST', 'https://control.msg91.com/api/v5/whatsapp/whatsapp-outbound-message/', [
                'headers' => [
                    'Accept' => 'application/json',
                    'authkey' => $this->authKey,
                    'Content-Type' => 'application/json',
                ],
                'json' => [
                    'recipient_number' => $mobileNumber,
                    'integrated_number' => $integratedNumber,
                    'content_type' => 'interactive',
                    'interactive' => [
                        'type' => 'list',
                        'header' => [
                            'type' => 'text',
                            'text' => $headerText,
                        ],
                        'body' => [
                            'text' => $bodyText,
                        ],
                        'footer' => [
                            'text' => $footerText,
                        ],
                        'action' => [
                            'button' => $buttonText,
                            'sections' => $sections,
                        ],
                    ],
                ],
            ]);

            return json_decode($response->getBody()->getContents(), true);
        } catch (ClientException $e) {
            Log::error($e);
            $responseBody = $e->getResponse()->getBody(true);
            return json_decode($responseBody, true);
        } catch (Exception $e) {
            Log::error($e);
            return ['error' => $e->getMessage()];
        }
    }
}


/*
FOR REFERENCE 

<?php
$curl = curl_init();
curl_setopt_array($curl, [
  CURLOPT_URL => "https://control.msg91.com/api/v5/whatsapp/whatsapp-outbound-message/?integrated_number=&recipient_number=&content_type=&text=",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => [
    "accept: application/json",
    "authkey: ",
    "content-type: application/json"
  ],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://control.msg91.com/api/v5/whatsapp/whatsapp-outbound-message/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>
'{
"recipient_number": "919999999999",
"integrated_number": "919898989898",
"content_type": "interactive",
"interactive": {
    "type": "list",
    "header": {
    "type": "text",
    "text": "HEADER_TEXT"
    },
    "body": {
    "text": "BODY_TEXT"
    },
    "footer": {
    "text": "FOOTER_TEXT"
    },
    "action": {
    "button": "BUTTON_TEXT",
    "sections": [
        {
        "title": "SECTION_1_TITLE",
        "rows": [
            {
            "id": "SECTION_1_ROW_1_ID",
            "title": "SECTION_1_ROW_1_TITLE",
            "description": "SECTION_1_ROW_1_DESCRIPTION"
            },
            {
            "id": "SECTION_1_ROW_2_ID",
            "title": "SECTION_1_ROW_2_TITLE",
            "description": "SECTION_1_ROW_2_DESCRIPTION"
            }
        ]
        },
        {
        "title": "SECTION_2_TITLE",
        "rows": [
            {
            "id": "SECTION_2_ROW_1_ID",
            "title": "SECTION_2_ROW_1_TITLE",
            "description": "SECTION_2_ROW_1_DESCRIPTION"
            },
            {
            "id": "SECTION_2_ROW_2_ID",
            "title": "SECTION_2_ROW_2_TITLE",
            "description": "SECTION_2_ROW_2_DESCRIPTION"
            }
        ]
        }
    ]
    }
}
}',
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authkey: yourauthkeyvalue",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://control.msg91.com/api/v5/flow",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => 
'{
    "template_id": "EntertemplateID",
    "short_url": "1 (On) or 0 (Off)",
    "realTimeResponse": "1 (Optional)",
    "recipients": [
    {
        "mobiles": "919XXXXXXXXX",
        "VAR1": "VALUE 1",
        "VAR2": "VALUE 2"
    }
    ]
}',
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authkey: Enter your MSG91 authkey",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
*/