public function sendMessage($toNumber, $message)
 {
     $this->connectAndLogin();
     $this->simulateTypingStart($toNumber);
     $this->message->setFromName($this->client->getIdentity()->getNickname());
     $this->message->setTo($toNumber);
     $this->message->setBody($message);
     $this->simulateTypingEnd();
     // Sending message...
     $this->client->send($this->message);
 }
示例#2
0
 /**
  * @param Client        $client
  * @param NodeInterface $node
  */
 protected function sendNotificationAck(Client $client, NodeInterface $node)
 {
     $ackNode = new Node();
     if ($node->hasAttribute("to")) {
         $ackNode->setAttribute('from', $node->getAttribute("to"));
     }
     if ($node->hasAttribute("participant")) {
         $ackNode->setAttribute('participant', $node->getAttribute("participant"));
     }
     $ackNode->setAttribute('to', $node->getAttribute("from"));
     $ackNode->setAttribute('class', $node->getName());
     $ackNode->setAttribute('id', $node->getAttribute("id"));
     $ackNode->setAttribute('type', $node->getAttribute("type"));
     $client->sendNode($ackNode);
 }
示例#3
0
 /**
  * @param Client $client
  * @param string $id
  * @param int    $timeout
  */
 protected function waitForServer(Client $client, $id, $timeout = 5)
 {
     $time = time();
     do {
         $client->pollMessages();
     } while ($this->receivedId !== $id && time() - $time < $timeout);
 }
示例#4
0
 /**
  * @param  Client        $client
  * @param  NodeInterface $node
  * @return $this
  */
 protected function processGetGroupInfoResult(Client $client, NodeInterface $node)
 {
     $groupList = $this->getGroupsFromNode($node);
     $client->getEventManager()->trigger('onGetGroupInfoResult', $this, array('groups' => $groupList));
     return $this;
 }
 protected function logoutAndDisconnect()
 {
     $this->client->disconnect();
 }
示例#6
0
 protected function sendReceipt(Client $client, NodeInterface $node)
 {
     $receipt = new Receipt();
     $receipt->setTo($node->getAttribute('from'));
     $receipt->setId($node->getAttribute('id'));
     $client->send($receipt);
     return $this;
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     //Set up how the create the Identity when one is asked to be created
     $this->app->bindShared('Tmv\\WhatsApi\\Entity\\Identity', function () {
         //Setup Account details.
         $account = Config::get("larawhatsapi::useAccount");
         $nickName = Config::get("larawhatsapi::accounts.{$account}.nickName");
         $number = Config::get("larawhatsapi::accounts.{$account}.number");
         $password = Config::get("larawhatsapi::accounts.{$account}.password");
         $userIdent = Config::get("larawhatsapi::accounts.{$account}.identity");
         // Initializing client
         // Creating a service to retrieve phone info
         $localizationService = new LocalizationService();
         // Creating a phone object...
         $phone = new Phone($number);
         // Injecting phone properties
         $localizationService->injectPhoneProperties($phone);
         // Creating identity
         $identity = new Identity();
         $identity->setPhone($phone)->setNickname($nickName)->setPassword($password)->setIdentityToken($userIdent);
         return $identity;
     });
     //Set up how the create TMV's Client Object when one is asked to be created (which needs the Identity)
     $this->app->bindShared('Tmv\\WhatsApi\\Client', function () {
         $debug = Config::get("larawhatsapi::debug");
         $account = Config::get("larawhatsapi::useAccount");
         $number = Config::get("larawhatsapi::accounts.{$account}.number");
         $nextChallengeFile = Config::get("larawhatsapi::nextChallengeDir") . "/" . $number . "-NextChallenge.dat";
         $identity = App::make('Tmv\\WhatsApi\\Entity\\Identity');
         // Initializing client
         $client = new Client($identity);
         $client->setChallengeDataFilepath($nextChallengeFile);
         // Attaching events...
         if (class_exists('MGP25WhatapiEvents')) {
             $events = new MGP25WhatapiEvents($client);
             foreach ($events->activeEvents as $eventName) {
                 $client->getEventManager()->attach($eventName, function () use($events, $eventName) {
                     $events->{$eventName}();
                 });
             }
         }
         //            TODO I don't want to attach events here, but this is just for demo.
         //            $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;
         //                }
         //            );
         // Debug events
         if ($debug) {
             $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);
             });
         }
         dd('done');
         return $client;
     });
     //Which concret implementation will we use when an SMSInterface is asked for? User can pick in the config file.
     $this->app->bindShared('Williamson\\Larawhatsapi\\Repository\\SMSMessageInterface', function () {
         $fork = strtoupper(Config::get('larawhatsapi::fork'));
         switch ($fork) {
             case $fork == 'MGP25':
                 return App::make('Williamson\\Larawhatsapi\\Clients\\LaraWhatsapiMGP25Client');
                 break;
             default:
                 return App::make('Williamson\\Larawhatsapi\\Clients\\LaraWhatsapiTMVClient');
                 break;
         }
     });
     //Set up how the create the WhatsProt object when using MGP25 fork
     $this->app->bindShared('WhatsProt', function () {
         //Setup Account details.
         $debug = Config::get("larawhatsapi::debug");
         $account = Config::get("larawhatsapi::useAccount");
         $nickName = Config::get("larawhatsapi::accounts.{$account}.nickName");
         $number = Config::get("larawhatsapi::accounts.{$account}.number");
         $userIdent = Config::get("larawhatsapi::accounts.{$account}.identity");
         $nextChallengeFile = Config::get("larawhatsapi::nextChallengeDir") . "/" . $number . "-NextChallenge.dat";
         $identityFileNoDat = Config::get("larawhatsapi::nextChallengeDir") . "/" . $number . "-Identity";
         $identityFileDat = $identityFileNoDat . '.dat';
         if (!File::exists($identityFileDat) || File::get($identityFileDat) !== $userIdent) {
             File::put($identityFileDat, $userIdent);
         }
         $whatsProt = new WhatsProt($number, $identityFileNoDat, $nickName, $debug);
         $whatsProt->setChallengeName($nextChallengeFile);
         if (class_exists('MGP25WhatapiEvents')) {
             $events = new MGP25WhatapiEvents($whatsProt);
             $events->setEventsToListenFor($events->activeEvents);
         }
         return $whatsProt;
     });
 }