Code Monkey home page Code Monkey logo

tmvwhatsapi's Introduction

Build Status Code Coverage Scrutinizer Code Quality

WhatsAPI

Status: development

Do not use it in production environment!

About WhatsAPI

WhatsAPI is a client library to use Whatsapp services.

This is a new project based on the original WhatsAPI: Please see the original project

Why a new project?

The original WhatsAPI library is not compatible with composer, no PSR compatible, and it's very old. I want to develop this new library in order to make it more usable. If you want to help, just do it :)

How to start using this library

Initializing client

use Tmv\WhatsApi\Service\LocalizationService;
use Tmv\WhatsApi\Entity\Phone;
use Tmv\WhatsApi\Entity\Identity;
use Tmv\WhatsApi\Client;

// Initializing client
// Creating a service to retrieve phone info
$localizationService = new LocalizationService();
$localizationService->setCountriesPath(__DIR__ . '/data/countries.csv');

// Creating a phone object...
$phone = new Phone(''); // your phone number with international prefix
// Injecting phone properties
$localizationService->injectPhoneProperties($phone);
// Creating identity
$identity = new Identity();
$identity->setNickname(''); // your name
$identity->setIdentityToken('');    // your token
$identity->setPassword(''); // your password
$identity->setPhone($phone);

// Initializing client
$client = new Client($identity);
$client->setChallengeDataFilepath(__DIR__ . '/data/nextChallenge.dat');

// Attaching events...
// ...

// Connecting and login...
$client->connect();
$client->login();

// Actions
// ...

// Polling incoming messages
$time = time();
while (true) {
    $client->pollMessages();
    if (time() - $time >= 10) {
        $time = time();
        // we send a presence message every 10 seconds to avoid server disconnection
        $client->send(new Action\Presence($identity->getNickname()));
    }
}

Sending a message

use Tmv\WhatsApi\Message\Action;

$number = ''; // number to send message
// Sending composing notification (simulating typing)
$client->send(new Action\ChatState($number, Action\ChatState::STATE_COMPOSING));
// Sending paused notification (typing end)
$client->send(new Action\ChatState($number, Action\ChatState::STATE_PAUSED));

// Creating message action
$message = new Action\MessageText($identity->getNickname(), $number);
$message->setBody('Hello');

// Sending message...
$client->send($message);

Receiving message

use Tmv\WhatsApi\Event\MessageReceivedEvent;
use Tmv\WhatsApi\Message\Received;

// onMessageReceived event
$client->getEventManager()->attach(
    'onMessageReceived',
    function (MessageReceivedEvent $e) {
        $message = $e->getMessage();
        echo str_repeat('-', 80) . PHP_EOL;
        echo '** MESSAGE RECEIVED **' . PHP_EOL;
        echo sprintf('From: %s', $message->getFrom()) . PHP_EOL;
        if ($message->isFromGroup()) {
            echo sprintf('Group: %s', $message->getGroupId()) . PHP_EOL;
        }
        echo sprintf('Date: %s', $message->getDateTime()->format('Y-m-d H:i:s')) . PHP_EOL;

        if ($message instanceof Received\MessageText) {
            echo PHP_EOL;
            echo sprintf('%s', $message->getBody()) . PHP_EOL;
        } elseif ($message instanceof Received\MessageMedia) {
            echo sprintf('Type: %s', $message->getMedia()->getType()) . PHP_EOL;
        }
        echo str_repeat('-', 80) . PHP_EOL;
    }
);

Debugging

It's possible to debug attaching events. It's possible to listen all events attaching to '*' event.

use Zend\EventManager\Event;

// Debug events
$client->getEventManager()->attach(
    'node.received',
    function (Event $e) {
        $node = $e->getParam('node');
        echo sprintf("\n--- Node received:\n%s\n", $node);
    }
);
$client->getEventManager()->attach(
    'node.send.pre',
    function (Event $e) {
        $node = $e->getParam('node');
        echo sprintf("\n--- Sending Node:\n%s\n", $node);
    }
);

Public Events

  • onMessageReceived (generic event for all messages)
  • onMessageTextReceived
  • onMessageMediaImageReceived
  • onMessageMediaAudioReceived
  • onMessageMediaVideoReceived
  • onMessageMediaVcardReceived
  • onMessageMediaLocationReceived
  • onLoginSuccess
  • onLoginFailed
  • onReceiptServer
  • onReceiptClient
  • onPresenceReceived
  • onGroupParticipantAdded
  • onGroupParticipantRemoved
  • onGetGroupsResult
  • onGetGroupInfoResult

tmvwhatsapi's People

Contributors

jonnywilliamson avatar scrutinizer-auto-fixer avatar thomasvargiu avatar

Watchers

 avatar  avatar

Forkers

dheia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.