Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
megoxv committed Jul 6, 2024
0 parents commit d4ecf70
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [megoxv]
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Empty file added SECURITY.md
Empty file.
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"require": {
"php": "^8.1|^8.2"
}
}
Empty file added config/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions config/zeromsg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'api_key' => env('ZEROMSG_API_KEY'),
'device_id' => env('ZEROMSG_DEVICE_ID'),
];
13 changes: 13 additions & 0 deletions src/Facades/ZeroMsg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Megoxv\ZeroMsg\Facades;

use Illuminate\Support\Facades\Facade;

class ZeroMsg extends Facade
{
protected static function getFacadeAccessor()
{
return 'zeromsg';
}
}
149 changes: 149 additions & 0 deletions src/Services/ZeroMsgClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Megoxv\ZeroMsg\Services;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class ZeroMsgClient
{
protected $client;
protected $apiKey;
protected $deviceId;
protected $message;
protected $to;
protected $type;
protected $url;
protected $filename;
protected $link;
protected $latitude;
protected $longitude;
protected $title;
protected $address;
protected $buttonText;
protected $sections;

public function __construct()
{
$this->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);
}
}
Loading

0 comments on commit d4ecf70

Please sign in to comment.