dmeys/laravel-whatsapp

此包最新版本(dev-master)没有可用的许可证信息。

Laravel包装器

dev-master 2018-03-30 10:09 UTC

This package is auto-updated.

Last update: 2024-09-11 10:13:55 UTC


README

该包依赖的仓库已不再工作,对此表示歉意。希望WhatsApp能尽快推出其商业API。如果是这样,我将在本Laravel包中实现新API。

Laravel Whatsapp

这是whatsapp/chat-api包的Laravel包装器

入门指南

  1. 将包包含到您的应用中
  2. 注册服务提供者和别名
  3. 创建监听器
  4. 注册
  5. 发送消息

将包包含到您的应用中

composer require dmeys/laravelwhatsapp:dev-master

或在项目的composer.json中添加依赖项

  "require": {
    "dmeys/laravelwhatsapp": "dev-master"
  },

注册服务提供者和别名

编辑config/app.php文件。将以下内容追加到providers数组中

  'providers' => [
    // ...
    Lucasvdh\LaravelWhatsapp\WhatsappServiceProvider::class,
    // ...
  ],

注册别名

  'aliases' => [
    // ...
    'Whatsapp' => Lucasvdh\LaravelWhatsapp\Facades\Whatsapp::class,
    // ...
  ],

创建监听器

<?php namespace App\Listeners;

use Lucasvdh\LaravelWhatsapp\Abstracts\Listener as WhatsappListener

class WhatsappEventListener extends WhatsappListener
{
  // Check the Lucasvdh\LaravelWhatsapp\Interface\Listener for all events
}

注册

注册流程。

请求验证码

$phone_number = '31612345678' // Your phone number including country code
$type = 'sms';
// $type = 'voice';
$result = Whatsapp::requestCode($phone_number, $type);

验证注册验证码

$phone_number = '31612345678' // Your phone number including country code
$code = '123-456'; // The code you've received through sms or voice

try {
  $result = Whatsapp::register('$phone_number, $code);
}
catch(Exception $e) {
  die('Something went wrong during the registration: ' . $e->getMessage());
}

// Store this password, you'll need this to connect to the network
$password = $result['pw']

连接到网络

$phone_number = '31612345678' // Your phone number including country code
$display_name = 'Foo Bar'; // This is the name that other Whatsapp users will see
$password = '****************************'; // The password you received from the registration process 

// Fetch the connection
$connection = Whatsapp::getConnection($phone_number, $display_name);

// Initialize your listener
$listener = new WhatsappEventListener($connection);

// Connect to the network
$connection = Whatsapp::connect($connection, $password, $listener);

// You are now connected

发送消息

连接到网络后,您可以发送如下消息

$target = '3112345678'; // The phone number of the person you are sending the message to
$message = 'This is a message';

$message_hash = $connection->sendMessage($target , $message);

接收消息

我建议使用Supervisor在后台运行artisan命令,但还有其他解决方法。我已经为使用Supervisor接收消息编写了一个 artisan 命令的示例。

设置后台工作进程

配置Supervisor

要监听的事件