承接 xaamin/whatsapi 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

xaamin/whatsapi

Composer 安装命令:

composer require xaamin/whatsapi

包简介

Wrapper for Whatsapp messenger

README 文档

README

See #38

Whatsapi

Wrapper for whatsapp/chat-api, that allows us send messages through WhatsApp. Thanks guys.

Installation

Non Laravel users

If you're not a Laravel user you only need to run the composer require command in order to install the needed package.

    composer require xaamin/whatsapi

Laravel users

Assuming you already have composer installed on your system, install a new Laravel project into whatsapidemo folder

    composer create-project laravel/laravel whatsapidemo --prefer-dist

Ensure that you set webserver to use whatsapidemo/public as it's webroot. Now, if you visit http://localhost (or whatever domain name you are using) you should see a nice Laravel welcome message.

Change into your new whatsapidemo folder.

    cd whatsapidemo

Require the needed package.

    composer require xaamin/whatsapi

If you get [InvalidArgumentException] Could not find package xaamin/whatsapi at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability you must add these lines to your composer.json an then re-run previous command.

    "minimum-stability": "dev",
    "prefer-stable" : true

We tell Laravel that there is a Whatsapi ServiceProvider. At the end of config/app.php file, in the providers array, add:

    'Xaamin\Whatsapi\WhatsapiServiceProvider'

Finally, Into the config/app.php file, add to aliases array each of these lines

    'Whatsapi' => 'Xaamin\Whatsapi\Facades\Laravel\Whatsapi',
    'WhatsapiTool' => 'Xaamin\Whatsapi\Facades\Laravel\Registration',

Configuration

Non Laravel users

If you're using another Framework, different from Laravel, you must put manually the config file Config/config.php on the right path.

    // 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');

Now, we tell Whatsapi about the config values.

    // Of course, you can use the Config class from your favorite Framework.
    $config = __DIR__ . 'config/whatsapi.php';

    Whatsapi::setConfig($config);

By default, the native implementation session use the $_SESSION global var, you can override this providing a instance that implements the contract 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 users

We need to publish the config file that will allow you to very easily add all your account numbers.

    php artisan vendor:publish --provider="Xaamin\Whatsapi\WhatsapiServiceProvider" --tag="config"

Now everything has been installed, you just need to add your Whatsapp account details into the config file. There will now be a personal config file created for you in whatsapidemo/config/whatsapi.php. Open this file and edit the details with your account info. Once saved, you're good to use the API!

Usage

Note: You must create the data storage path specified in configuration file. The path must be writable for webserver.

Request registration code

When requesting the code, you can do it via SMS or voice call, in both cases you will receive a code like 123-456, that we will use for register the number.

    $number = '5219511552222'; # Number with country code
    $type = 'sms'; # This can be either sms or voice

    $response = WhatsapiTool::requestCode($number, $type);

Example response:

    stdClass Object
    (
        [status] => sent
        [length] => 6
        [method] => sms
        [retry_after] => 1805
    )

Registration

If you received the code like this 123-456 you should register like this '123456'

    $number = '5219511552222'; # Number with country code
    $code = '132456'; # Replace with received code  

    $response = WhatsapiTool::registerCode($number, $code);

If everything went right, this should be the output:

    [status] => ok
    [login] => 34123456789
    [pw] => 0lvOVwZUbvLSxXRk5uYRs3d1E=
    [type] => existing
    [expiration] => 1443256747
    [kind] => free
    [price] => EUR€0.99
    [cost] => 0.89
    [currency] => EUR
    [price_expiration] => 1414897682

See the entire registration process on https://github.com/WHAnonymous/Chat-API/wiki/WhatsAPI-Documentation#number-registration

Send messages

    // 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)
    {
        ...
    }

Check for new messages

    $messages = $messages = Whatsapi::getNewMessages();

    if($messages)
    {
        foreach($messages as $message)
        {
            ...
        }
    }

Sync contacts

    $result = Whatsapi::syncContacts(['5219512222222', '5219512222223']);
    
    foreach ($result->existing as $number => $account)
    {
        ... 
    }

    foreach ($result->nonExisting as $number)
    {
        ...
    }

Example response

    SyncResult Object
    (
        [index] => 0
        [syncId] => 130926960960000000
        [existing] => Array
            (
                [+5219512222222] => 5219512222222@s.whatsapp.net
            )

        [nonExisting] => Array
            (
                [0] => 5219512222223
            )

    )

Missing methods

You can use all power of the whatsapp/chat-api getting and instance of Whatsprot class. Read the whatsapp/chat-api wiki for available methods.

    $whatsprot = Whatsapi::gateway();

    // Do stuff...

You can use on routes, cli... you got the idea.

xaamin/whatsapi 适用场景与选型建议

xaamin/whatsapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.79k 次下载、GitHub Stars 达 131, 最近一次更新时间为 2015 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「chat」 「laravel」 「whatsapp」 「Messenger」 「whatsapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xaamin/whatsapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xaamin/whatsapi 我们能提供哪些服务?
定制开发 / 二次开发

基于 xaamin/whatsapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 131
  • Watchers: 17
  • Forks: 66
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-20