定制 dmeys/laravel-whatsapp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dmeys/laravel-whatsapp

Composer 安装命令:

composer require dmeys/laravel-whatsapp

包简介

A laravel wrapper

README 文档

README

The repo which this package relied on is no longer working, sorry. Hopefully whatsapp will bring out their business API soon. If so I'll implement the new API in this laravel package.

Laravel Whatsapp

This is a Laravel wrapper for the whatsapp/chat-api package

Getting started

  1. Include the package in your application
  2. Register the service provider and aliases
  3. Create a listener
  4. Registration
  5. Sending a message

Include the package in your application

composer require dmeys/laravelwhatsapp:dev-master

Or add a requirement to your project's composer.json

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

Register the service provider and aliases

Edit the config/app.php file. Append the following to the providers array:

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

Register the aliases:

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

Create a listener

<?php namespace App\Listeners;

use Lucasvdh\LaravelWhatsapp\Abstracts\Listener as WhatsappListener

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

Registration

The registration flow.

Request a code

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

Verify the registration code

$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']

Connecting to the network

$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

Sending a message

After connecting to the network you can send a message like this

$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);

Receiving messages

I recommend using Supervisor to run an artisan command in the background, but there are other ways to solve this. I've written an example for receiving messages with an artisan command using Supervisor.

Setting up a background worker

Configure Supervisor

Events to listen for

统计信息

  • 总下载量: 2.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-03-29