alresia / laravel-wassenger
使用Wassenger Api的Laravel WhatsApp Business Api
Requires
- php: >=5.6.0
- ext-curl: *
README
使用Wassenger Api的Laravel WhatsApp Business Api
有关Wassenger的更多信息,请访问 https://www.wassenger.com。
介绍
本API文档提供了有关LARAVEL WASSENGER和WASSINGER端点请求/响应API合约的详细信息。
您可以使用PHP轻松连接到WASSINGER,发送请求并接收响应,使用最常用和重要的端点。
本软件包旨在帮助缩短将WhatsApp集成到我们的应用程序中的时间。
借助Wassenger Api,它已针对Laravel框架进行了优化。
如果您喜欢此软件包,请考虑通过添加星标并向您的朋友介绍来支持它。
安装和设置
在Composer上
Laravel Wassenger可在Packagist上获得(使用语义版本控制),并且通过Composer安装是推荐的方式。只需将以下行添加到您的composer.json
文件中
"alresia/laravel-wassenger": "^1.2.0"
或运行
composer require alresia/laravel-wassenger
从Laravel控制器
<?php namespace App\Http\Controllers; use Alresia\LaravelWassenger\Messages; class SampleController extends Controller { public function sendMessage() { $phone = '+1234567890'; $message = 'Hello world! This is a simple test message'; Messages::message($phone, $message)->send(); } }
直接使用Composer
直接使用Composer要求引入自动加载文件
<?php require_once 'vendor/autoload.php'; use Alresia\LaravelWassenger\Wassenger; $phone = '+1234567890'; $message = 'Hello world! This is a simple test message'; Messages::message($phone, $message)->send();
不使用Composer
或者,如果您不使用Composer或Laravel应用程序,您可以下载Laravel-Wassenger作为zip文件,然后复制文件夹内容到您的php应用程序目录中,并加载wassenger-loader.php
<?php use Alresia\LaravelWassenger\Messages; require 'PATH_TO_LARAVEL_WASSENGER/laravel-wassenger/src/wassenger-loader.php'; $phone = '+1234567890'; $message = 'Hello world! This is a simple test message'; Messages::message($phone, $message)->send();
配置
Laravel
Laravel Wassenger旨在开箱即用,您可以通过在Laravel中发布配置文件来覆盖配置。
php artisan vendor:publish --provider="Alresia\LaravelWassenger\WassengerServiceProvider" --tag="config"
之后,您将收到类似以下的config/wassenger.php
配置文件
<?php return [ 'authorisation' => [ 'api_key' => env('WASSENGER_API_KEY', ''), 'api_host' => env('WASSENGER_API_URL', 'https://api.wassenger.com'), 'api_version' => 1, 'default_device' => env('DEFAULT_DEVICE', '') ], 'http_client' => [ 'return_json_errors' => false, ], ];
其他应用程序
配置文件位于laravel-wassenger/src/Config.php
<?php namespace Alresia\LaravelWassenger; class Config { public const API_KEY = ''; public const API_HOST = 'https://api.wassenger.com'; public const API_VERSION = 1; public const DEFAULT_DEVICE = null; public const RETURN_JSON_ERRORS = false;
API密钥
需要WASSENGER_API_KEY才能使用此软件包
要获取API密钥,请访问Wassanger控制台并获取API密钥
如果您使用Larevel,只需转到您的.env
文件来设置API密钥和默认设备
WASSENGER_API_KEY=<YOUR_KEY_HERE> DEFAULT_DEVICE=<DEVICE_ID_HERE>
如果您希望应用程序返回JSON响应而不是错误异常
将配置文件中的返回JSON错误
更改为true
'http_client' => [ 'return_json_errors' => true, ],
public const RETURN_JSON_ERRORS = true;
这很有用,如果您想要管理错误响应
用法
这处理了Wassenger的四个端点。它只包含最常用和最重要的函数
类
主Wassenger类
要使用,请将
LaravelWassenger\Wassenger
命名空间包含到您的文件中;
use Alresia\LaravelWassenger\Wassenger;
以下可用方法是
示例
号码存在
Wassenger::numberExist('+1234567890');
同步设备会话
Wassenger::sessionSync('63615bb1db0ssb686cee2fa4a');
Wassenger::sync('63615bb1db0ssb686cee2fa4a');
同步所有设备会话
Wassenger::syncAll();
消息
要使用,请将
LaravelWassenger\Messages
命名空间包含到您的文件中;
use Alresia\LaravelWassenger\Messages;
本包中提供以下方法用于发送消息,您可以在此处查看详细信息
示例用法
向电话号码发送短信
Messages::message('+1234567890', 'Hello world! This is a simple test message') ->send();
向群组发送高优先级短信
Messages::messageGroup('123456789000000000@g.us', 'Hello world! This is a simple test message', 'high') ->send();
向用户发送媒体消息。注意,必须先更新文件,请参阅API端点:文件 > 上传文件
Messages::messageGroup('+1234567890', 'Hello world! This is a test media message.', 'high') ->media(['file', '<24 characters length file ID>']) ->send();
发送应立即投递的短信
Messages::message('+1234567890', 'Hello world! This is a simple test message', false) ->send();
发送具有自定义延迟的计划消息。
查看 "schedule.delayTo" 日期时间缩写:https://i.ibb.co/g3DJLSH/datetime-shortcuts.png
Messages::message('+1234567890', 'Hello world! This is a simple test message') ->schedule('1h') ->send();
使用有效的ISO 8601日期在具体日期发送计划消息
Messages::message('+1234567890', 'Hello world! This is a simple test message') ->deliverAt('2000-01-01T01:00:00+1200') ->send();
如果您想将操作按钮附加到您的消息上
$buttons = [ [ "id" => "id1", "kind" => "call", "text" => "Call us", "value" => "+2348055995704" ], [ "id" => "id2", "kind" => "url", "text" => "Open me", "value" => "https://company.com/shop"\ ], [ "id" => "id3", "text" => "Get help" ] ]; Messages::message('+1234567890', 'Hello world! This is a simple test message') ->buttons($buttons) ->send();
获取消息
Messages::findById('635889e9133a76fddfdfy65r5') ->get();
更新消息
Messages::findById('635889e9133a76fddfdfy65r5') ->message('+1234567890', 'Hello world! This is a simple test message') ->update();
删除消息
Messages::findById('635889e9133a76fddfdfy65r5') ->delete();
或者,您可以在
delete()
方法中通过使用findById()
方法传递消息ID来删除消息
Messages::delete('635889e9133a76fddfdfy65r5');
设备
要使用,请将
LaravelWassenger\Device
命名空间包含在您的文件中;
use Alresia\LaravelWassenger\Device;
以下可用方法是
示例
获取账户设备
Devices::get();
按ID获取设备
Devices::findById('63615bb1db0b686cee2fa4af')->get();
按电话号码、别名或ID搜索设备
Devices::search('+2348113536471,61b37a069cba0c15d6c81000')->get();
仅获取已验证的设备
Devices::status('verified')->get();
仅获取待处理的设备
Devices::status('pending')->get();
按在线会话获取设备
Devices::session('online')->get();
获取设备会话
Devices::session('any')->get();
按电话号码、别名或ID搜索已验证的设备
Devices::search('+2348113536471,61b37a069cba0c15d6c81000')->status('verified')->get();
按电话号码、别名或ID搜索在线设备
Devices::search('+2348113536471,61b37a069cba0c15d6c81000')->session('online')->get();
按电话号码、别名或ID搜索在线设备
Devices::search('+2348113536471,61b37a069cba0c15d6c81000')->session('online')->get();
排序结果
Devices::limit(20, 2)->get(); // size 20 page 2
Devices::search('+2348113536471,61b37a069cba0c15d6c81000')->session('online')->limit(20, 3)->get(); // size 20 page 3
所有方法都可以相互使用
Devices::session('any')->status('any')->limit(20, 0)->get(); // get() should always be the last
此外,您还可以选择或使用未在此列出但可以使用get的所有参数
Devices::get([ 'search' => '+2348113536471,61b37a069cba0c15d6c81000', 'status' => 'any', 'sessionStatus' => 'online', 'size' => '20', 'page' => '3', ]);
会话
此功能已在其他类方法中存在
use Alresia\LaravelWassenger\Session; Session::sync('63615bb1db0b686cee2fa4af');
请参阅https://app.wassenger.com/docs/#tag/Devices/operation/searchDevices以正确使用此方法
贡献
欢迎所有贡献。请随时给我发邮件Fidelis E Peter。
您也可以在观察到任何问题时提交问题
感谢您考虑为Laravel Wassenger做出贡献!
包
此包不需要任何额外包
您也可以将这些包用于非Laravel应用程序
注意:除非您知道自己在做什么,否则无需从Config.php或wassenger.php更改主机和版本