Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
4.0 kB
3
Indexable
Never
<?php

namespace App\Utilities;

use GuzzleHttp\Client as Http;
use GuzzleHttp\Exception\RequestException;

class Whatsapp
{
	/**
     * Request type GET
     */
    const TYPE_GET = 'get';

    /**
     * Request type POST
     */
    const TYPE_POST = 'post';

    /**
     * Request type DELETE
     */
    const TYPE_DELETE = 'delete';

	public function __construct()
	{
		$this->url = 'https://wasap.jomhomestay.my/';
		$this->api_key = 'aq7IWhL8zOzkA31WrCQhYhSdk7TUFWV7m2GtVEcB9U9Cijo0Md';
        $this->sender_id = 'JomHomestay';
	}

    public function setUrl($prefix)
    {
        return $this->url.$prefix;
    }

    public function checkAuth()
    {
        $call = $this->call('auth/checkauth', [] , self::TYPE_GET);

        return $call;
    }

    public function resetAuth()
    {
        $this->call('auth/destroy-auth');
        $this->call('auth/create', [
            'json'  =>  [
                'id'    =>  $this->sender_id,
                'description'   =>  $this->sender_id,
            ],
        ]);
    }

    public function getQr()
    {
        $call = $this->call('auth/checkauth', [] , self::TYPE_POST);

        $res = [];

        if(@$call['status_code'] == 200 && !empty(@$call['data']['qrcode'])){
            $res = [
                'show'  =>  true,
                'qr'    =>  $call['data']['qrcode'],
                'msg'   =>  'Please scan',
            ];
        }else{
            $res = [
                'show'  =>  false,
                'msg'   =>  @$call['data']['message'],
                'tel_no'    =>  @$call['data']['info']['me']['user'],
            ];
        }
        
        return $res;
    }

	public function sendMessage($phone_no , $message)
	{
		$data = [
			'json'  =>  [
                'message'   =>  $message,
            ]
		];

        $call = $this->call('chat/sendmessage/'.phone_number_formatter($phone_no), $data);

		return $call;
	}

	public function call($prefix , array $options = [] , $method = self::TYPE_POST)
    {
        try{

            $client = new Http(
                array(
                    'timeout' => 120,
                )
            );

            $extraHeader = [];

            $extraHeader = [
                'headers' => [
                    'Content-Type'  =>  'application/json', 
                    'Accept'        =>  'application/json',
                    'Api-Key'       =>  $this->api_key,
                    'Sender-Id'     =>  $this->sender_id
                ],
            ];

            switch ($method) {
                case self::TYPE_DELETE:
                case self::TYPE_POST:
                    $options = array_merge_recursive($options, $extraHeader);
                    break;
                case self::TYPE_GET:
                    $options = array_merge_recursive($options, $extraHeader);
                    break;
            }

            $response = $client->request($method, $this->setUrl($prefix) , $options);

            $data = [
                'data'          =>  json_decode($response->getBody()->getContents(), JSON_UNESCAPED_SLASHES),
                'status_code'   =>  $response->getStatusCode()
            ];

            return $data;

        }catch(RequestException $ex){

            $response = [];
            $status_code = $ex->getCode() != 0 ? $ex->getCode() : 500;

            if(method_exists($ex , 'getResponse')){
                if(method_exists(@$ex->getResponse(), 'getBody') && method_exists(@$ex->getResponse(), 'getStatusCode')){
                    $response = json_decode(@$ex->getResponse()->getBody(), JSON_UNESCAPED_SLASHES);
                    $status_code = @$ex->getResponse()->getStatusCode();
                }
            }
            
            $data = [
                'message' => $ex->getMessage(),
                'response' => $response,
                'status_code' => $status_code,
            ];

            return $data;
        }
	}
}