xaamin / whatsapi
WhatsApp 消息应用封装器
Requires
- php: >=5.6
- whatsapp/chat-api: ^3.2
Suggests
- illuminate/support: For use on Laravel 5.
README
查看 #38
Whatsapi
封装 whatsapp/chat-api,允许我们通过 WhatsApp 发送消息。谢谢大家。
安装
非 Laravel 用户
如果您不是 Laravel 用户,您只需要运行 composer require 命令来安装所需的包。
composer require xaamin/whatsapi
Laravel 用户
假设您已经在系统上安装了 composer,将新的 Laravel 项目安装到 whatsapidemo
文件夹中
composer create-project laravel/laravel whatsapidemo --prefer-dist
确保将 web 服务器设置为使用 whatsapidemo/public
作为其 webroot。现在,如果您访问 https://(或您使用的任何域名),您应该看到漂亮的 Laravel 欢迎消息。
切换到您的新 whatsapidemo
文件夹。
cd whatsapidemo
需要所需的包。
composer require xaamin/whatsapi
如果您收到 [InvalidArgumentException] 无法找到 xaamin/whatsapi 包的任何版本,以符合您的最低稳定性(稳定)。检查包拼写或您的最低稳定性
,您必须将这些行添加到您的 composer.json 中,然后重新运行之前的命令。
"minimum-stability": "dev",
"prefer-stable" : true
我们告诉 Laravel 存在一个 Whatsapi ServiceProvider。在 config/app.php
文件末尾,在 providers 数组中添加
'Xaamin\Whatsapi\WhatsapiServiceProvider'
最后,在 config/app.php
文件中,将以下每行添加到 aliases 数组中
'Whatsapi' => 'Xaamin\Whatsapi\Facades\Laravel\Whatsapi',
'WhatsapiTool' => 'Xaamin\Whatsapi\Facades\Laravel\Registration',
配置
非 Laravel 用户
如果您使用的是不同于 Laravel 的其他框架,您必须手动将配置文件 Config/config.php
放置在正确的路径上。
// Include the composer autoload file
include_once "vendor/autoload.php";
// Import the necessary classes
use Xaamin\Whatsapi\Facades\Native\Whatsapi;
use Xaamin\Whatsapi\Facades\Native\Registration;
// Or, if you want you can add a class alias
// class_alias('Xaamin\Whatsapi\Facades\Native\Whatsapi', 'Whatsapi');
// class_alias('Xaamin\Whatsapi\Facades\Native\Registration', 'Registration');
现在,我们告诉 Whatsapi 关于配置值的。
// Of course, you can use the Config class from your favorite Framework.
$config = __DIR__ . 'config/whatsapi.php';
Whatsapi::setConfig($config);
默认情况下,本地实现会话使用全局变量 $_SESSION
,您可以通过提供实现 Xaamin\Whatsapi\Sessions\SessionInterface
接口的对象来覆盖此行为。
# Codeigniter 3 example
use CI_Session;
use Xaamin\Whatsapi\Sessions\SessionInterface;
class SessionManager implements SessionInterface{
# The key used in the Session.
protected $key = 'itnovado_whatsapi';
# Session object.
protected $session;
public function __construct(CI_Session $session)
{
$this->session = $session;
}
public function getKey()
{
return $this->key;
}
public function put($value)
{
$this->session->set_userdata($this->getKey(), $value);
}
public function pull()
{
$data = $this->session->userdata($this->getKey());
$this->session->unset_userdata($this->getKey());
return $data;
}
}
// Get some resources
$ci =& get_instance();
$ci->load->driver('session');
$sessionManager = new SessionManager($ci->session);
// Override the default session implementation
Whatsapi::setSessionManager($sessionManager);
Laravel 用户
我们需要发布配置文件,这将允许您轻松添加所有账户号码。
php artisan vendor:publish --provider="Xaamin\Whatsapi\WhatsapiServiceProvider" --tag="config"
现在一切已安装,您只需将 WhatsApp 账户详细信息添加到配置文件中。现在将在 whatsapidemo/config/whatsapi.php
中为您创建一个个人配置文件。打开此文件,使用您的账户信息进行编辑。保存后,您就可以使用 API 了!
使用方法
注意:您必须创建配置文件中指定的数据存储路径。该路径必须可由 web 服务器写入。
请求注册代码
在请求代码时,您可以通过短信或语音通话进行,在两种情况下,您都将收到类似于 123-456 的代码,我们将使用该代码来注册号码。
$number = '5219511552222'; # Number with country code
$type = 'sms'; # This can be either sms or voice
$response = WhatsapiTool::requestCode($number, $type);
示例响应
stdClass Object
(
[status] => sent
[length] => 6
[method] => sms
[retry_after] => 1805
)
注册
如果您收到的代码类似于这样 123-456,您应该注册为 '123456'。
$number = '5219511552222'; # Number with country code
$code = '132456'; # Replace with received code
$response = WhatsapiTool::registerCode($number, $code);
如果一切顺利,这将输出
[status] => ok
[login] => 34123456789
[pw] => 0lvOVwZUbvLSxXRk5uYRs3d1E=
[type] => existing
[expiration] => 1443256747
[kind] => free
[price] => EUR€0.99
[cost] => 0.89
[currency] => EUR
[price_expiration] => 1414897682
在 https://github.com/WHAnonymous/Chat-API/wiki/WhatsAPI-Documentation#number-registration 上查看整个注册过程
发送消息
// Retrieve user data from database, web service, and so on.
// Dummy method, fake data.
$user = new stdClass;
$user->name = 'Benjamín Martínez Mateos';
$user->phone = '5219512222222';
$message = "Hello $user->name and welcome to our site";
$messages = Whatsapi::send($message, function($send) use ($user)
{
$send->to($user->phone);
// Add an audio file
$send->audio('http://itnovado.com/example.mp3');
// Add an image file
$send->image('http://itnovado.com/example.jpg', 'Cool image');
// Add a video file
$send->video('http://itnovado.com/example.mp4', 'Fun video');
// Add a location (Longitude, Latitude)
$send->location(-89.164138, 19.412405, 'Itnovado Location');
// Add a VCard
$vcard = new Xaamin\Whatsapi\Media\VCard();
$vcard->set('data', array(
'first_name' => 'John',
'last_name' => 'Doe',
'tel' => '9611111111',
));
$send->vcard('Xaamin', $vcard);
// Add new text message
$send->message('Thanks for subscribe');
});
foreach($messages as $message)
{
...
}
检查新消息
$messages = $messages = Whatsapi::getNewMessages();
if($messages)
{
foreach($messages as $message)
{
...
}
}
同步联系人
$result = Whatsapi::syncContacts(['5219512222222', '5219512222223']);
foreach ($result->existing as $number => $account)
{
...
}
foreach ($result->nonExisting as $number)
{
...
}
示例响应
SyncResult Object
(
[index] => 0
[syncId] => 130926960960000000
[existing] => Array
(
[+5219512222222] => 5219512222222@s.whatsapp.net
)
[nonExisting] => Array
(
[0] => 5219512222223
)
)
缺失的方法
您可以使用whatsapp/chat-api提供的所有功能,获取Whatsprot
类的实例。请阅读whatsapp/chat-api的wiki了解可用的方法。
$whatsprot = Whatsapi::gateway();
// Do stuff...
您可以在路由、cli等地方使用,您应该明白了。