diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..bb587ea --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [megoxv] diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e66364e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2aa3f5a --- /dev/null +++ b/README.md @@ -0,0 +1,144 @@ +# ZeroMsg Laravel + +[![Latest Stable Version](https://poser.pugx.org/megoxv/zeromsg/version.svg)](https://packagist.org/packages/megoxv/zeromsg) +[![PHP Version Require](http://poser.pugx.org/megoxv/zeromsg/require/php)](https://packagist.org/packages/megoxv/zeromsg) +[![License](https://poser.pugx.org/megoxv/zeromsg/license.svg)](https://packagist.org/packages/megoxv/zeromsg) +[![Downloads](https://poser.pugx.org/megoxv/zeromsg/d/total.svg)](https://packagist.org/packages/megoxv/zeromsg) + + +The ZeroMsg Laravel package provides a fluent interface to interact with the ZeroMsg API, supporting various message types such as text, image, voice, media, list messages, link previews, and locations. + +## Installation + +### Step 1: Require the Package + +You can install the package via Composer. Run the following command in your terminal: + +```bash +composer require megoxv/zeromsg +``` + +### Step 2: Configuration + +Add your ZeroMsg API key and Device ID to your `.env` file: + +```env +ZEROMSG_API_KEY=your_api_key_here +ZEROMSG_DEVICE_ID=your_device_id_here +``` + +## Usage + +To use the ZeroMsg package, include the `ZeroMsg` facade in your Laravel project. + +### Sending a Text Message + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->message('Hello, this is a test message') + ->to('34676010101') + ->send(); +``` + +### Sending an Image + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->message('Check out this image!') + ->image('https://example.com/image.jpg', 'image.jpg') + ->to('34676010101') + ->send(); +``` + +### Sending a Voice Message + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->voice('https://example.com/voice.mp3') + ->to('34676010101') + ->send(); +``` + +### Sending a Media Message + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->message('Check out this media file!') + ->media('https://example.com/media.mp4', 'media.mp4', '') + ->to('34676010101') + ->send(); +``` + +### Sending a List Message + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->message('Check out this list!') + ->listMessage( + 'Options', + 'Select', + [ + [ + 'title' => 'Section 1', + 'value' => 'option1', + 'description' => 'First option' + ], + [ + 'title' => 'Section 2', + 'value' => 'option2', + 'description' => 'Second option' + ] + ] + ) + ->to('34676010101') + ->send(); +``` + +### Sending a Link Preview + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->message('Check out this link!') + ->linkPreview('https://zeromsg.com') + ->to('34676010101') + ->send(); +``` + +### Sending a Location + +```php +use Megoxv\ZeroMsg\Facades\ZeroMsg; + +ZeroMsg::create() + ->location('37.7749', '-122.4194', 'San Francisco', 'California, USA') + ->to('34676010101') + ->send(); +``` + +## Publish Assets + +you can publish config file by use this command + +```bash +php artisan vendor:publish --tag="zeromsg-config" +``` + +## Credits + +- [Abdelmjid Saber](https://github.com/megoxv) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6851afe --- /dev/null +++ b/composer.json @@ -0,0 +1,38 @@ +{ + "name": "megoxv/zeromsg", + "type": "library", + "description": "ZeroMsg fluent API for Whatsapp diverse message types integration.", + "keywords": [ + "php", + "laravel", + "zeromsg", + "whatsapp" + ], + "license": "MIT", + "autoload": { + "psr-4": { + "Megoxv\\ZeroMsg\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Megoxv\\ZeroMsg\\ZeroMsgServiceProvider" + ] + } + }, + "authors": [ + { + "name": "Abdelmjid Saber", + "email": "abdelmjiDev@gmail.com" + } + ], + "require": { + "php": "^8.1|^8.2" + } +} \ No newline at end of file diff --git a/config/.gitkeep b/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/config/zeromsg.php b/config/zeromsg.php new file mode 100644 index 0000000..f5a0b15 --- /dev/null +++ b/config/zeromsg.php @@ -0,0 +1,6 @@ + env('ZEROMSG_API_KEY'), + 'device_id' => env('ZEROMSG_DEVICE_ID'), +]; diff --git a/src/Facades/ZeroMsg.php b/src/Facades/ZeroMsg.php new file mode 100644 index 0000000..1efa2b8 --- /dev/null +++ b/src/Facades/ZeroMsg.php @@ -0,0 +1,13 @@ +client = new Client(); + $this->apiKey = config('zeromsg.api_key'); + $this->deviceId = config('zeromsg.device_id'); + } + + public static function create() + { + return new static(); + } + + public function message($message) + { + $this->message = $message; + return $this; + } + + public function to($phone) + { + $this->to = $phone; + return $this; + } + + public function image($url, $filename) + { + $this->type = 'image'; + $this->url = $url; + $this->filename = $filename; + return $this; + } + + public function voice($url) + { + $this->type = 'voice'; + $this->url = $url; + return $this; + } + + public function media($url, $filename) + { + $this->type = 'media'; + $this->url = $url; + $this->filename = $filename; + return $this; + } + + public function listMessage($title, $buttonText, $sections) + { + $this->type = 'list-message'; + $this->title = $title; + $this->buttonText = $buttonText; + $this->sections = $sections; + return $this; + } + + public function linkPreview($link) + { + $this->type = 'link-preview'; + $this->link = $link; + return $this; + } + + public function location($latitude, $longitude, $title = '', $address = '') + { + $this->type = 'location'; + $this->latitude = $latitude; + $this->longitude = $longitude; + $this->title = $title; + $this->address = $address; + return $this; + } + + protected function postRequest($endpoint, $body) + { + try { + $response = $this->client->post('https://app.zeromsg.com/api' . $endpoint, [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $this->apiKey, + 'Content-Type' => 'application/json', + ], + 'body' => json_encode($body), + ]); + + return json_decode($response->getBody()->getContents(), true); + } catch (GuzzleException $e) { + throw $e; + } + } + + public function send() + { + $body = [ + 'device' => $this->deviceId, + 'phone' => $this->to, + 'type' => $this->type ?? 'text', + 'message' => $this->message, + ]; + + if ($this->type === 'image' || $this->type === 'media' || $this->type === 'voice') { + $body['url'] = $this->url; + $body['filename'] = $this->filename; + } + + if($this->type === 'list-message') { + $body['title'] = $this->title; + $body['button_text'] = $this->buttonText; + $body['sections'] = $this->sections; + } + + if ($this->type === 'link-preview') { + $body['url'] = $this->link; + } + + if ($this->type === 'location') { + $body['lat'] = $this->latitude; + $body['lng'] = $this->longitude; + $body['title'] = $this->title; + $body['address'] = $this->address; + } + + return $this->postRequest('/send-message', $body); + } +} diff --git a/src/ZeroMsgServiceProvider.php b/src/ZeroMsgServiceProvider.php new file mode 100644 index 0000000..54fb835 --- /dev/null +++ b/src/ZeroMsgServiceProvider.php @@ -0,0 +1,29 @@ +mergeConfigFrom(__DIR__ . '/../config/zeromsg.php', 'zeromsg'); + + //Publish Config + $this->publishes([ + __DIR__ . '/../config/zeromsg.php' => config_path('zeromsg.php'), + ], 'zeromsg-config'); + } + + public function boot(): void + { + $this->mergeConfigFrom(__DIR__ . '/../config/zeromsg.php', 'zeromsg'); + + $this->app->singleton('zeromsg', function ($app) { + return new ZeroMsgClient(); + }); + } +} diff --git a/tests/Feature/.gitkeep b/tests/Feature/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/.gitkeep b/tests/Unit/.gitkeep new file mode 100644 index 0000000..e69de29