📜 ⬆️ ⬇️

Access to Skype API using PHP on * nix systems

A long time ago I wanted to have my own answering machine or bot for Skype, call it what you want. In Google, I could not find anything, and even because I use Ubuntu, the task became more difficult several times. And if to consider that I know only PHP, and that, not so well, my dream became unreal.

But recently, looking through the official Skype API documentation, I drew attention to the “D-BUS messaging”, I don’t know why I hadn’t paid attention to it before. Having dealt with everything, I finally did what I wanted for a long time! I got access to Skype API in PHP. This is what I want to tell in my article.


')
To install dbus-php you need phpize, which comes in the package php5-dev, install it:
sudo apt-get install php5-dev 

Now install php-pear by executing the following commands:
 sudo apt-get install php-pear pear install PEAR 

Install the dbus extension for php by first installing the dependencies (you must have make installed, otherwise the dbus-beta installation will fail!):
 sudo apt-get install libdbus-1-dev libxml2-dev pecl install dbus-beta 

Add the extension load to the php configuration:
 echo -e "; configuration for php DBus module\nextension=dbus.so"| sudo tee -a /etc/php5/conf.d/dbus.ini 

Check whether the extension works, enter the php -i command in the terminal and find there:
 dbus Dbus support => enabled Version => 0.1.0 

Found? Fine! You can continue!
Not found? Well, repeat everything, find the error, correct and continue reading, everything will work out for you.

Everything! Now we have everything you need for work.

Let's get down to the php code itself.
The simplest and most important thing is to connect to Skype and request access to api, we will do it this way:
 $dbus = new Dbus(Dbus::BUS_SESSION, true); // Dbus $n = $dbus->createProxy('com.Skype.API', '/com/Skype', 'com.Skype.API'); //   $n -> Invoke('NAME PHP'); //  ,    $n -> Invoke('PROTOCOL 8'); //   

The Invoke () method will be the main one for sending instructions to Skype.
Now, let's teach our script to receive Skype notifications:
 //    class phpSkype { public static function notify ($notify) { echo $notify."\n"; } } $dbus -> registerObject('/com/Skype/Client', 'com.Skype.API.Client', 'phpSkype'); //    while(1) { $s = $dbus -> waitLoop(1); } 

By running this script, you can watch all Skype notifications, like this:
 CONNSTATUS ONLINE CURRENTUSERHANDLE *my_user* USERSTATUS DND CHATMESSAGE 5150665 STATUS READ CHATMESSAGE 5149961 STATUS READ CHATMESSAGE 5149993 STATUS READ CHATMESSAGE 5150025 STATUS READ CHATMESSAGE 5150057 STATUS READ CHATMESSAGE 5150697 STATUS SENDING CHAT #zaidin16/$e00fc2f75170ec9e ACTIVITY_TIMESTAMP 1345401315 CHATMESSAGE 5150697 STATUS SENT 

We supplement the phpSkype class and train our script to process and respond to messages, that's what happened with me:
 <?php $dbus = new Dbus(Dbus::BUS_SESSION, true); // Dbus $n = $dbus->createProxy('com.Skype.API', '/com/Skype', 'com.Skype.API'); //   $n -> Invoke('NAME PHP'); //  ,    $n -> Invoke('PROTOCOL 8'); //   class phpSkype { /* *           . *    ,      ,  * preg_match('/RECEIVED/', $notify) */ public static function notify ($notify) { if (preg_match('#RECEIVED|SENT#Uis', $notify)) { $message_id = explode(' ', $notify); bot::get_details($message_id[1]); //   } } } class bot { private static $last_id; public static function get_details ($message_id) { global $n; $ch = $n -> Invoke('GET CHATMESSAGE '.$message_id.' CHATNAME'); // id ,    $mess = $n -> Invoke('GET CHATMESSAGE '.$message_id.' BODY'); //   $aut = $n -> Invoke('GET CHATMESSAGE '.$message_id.' FROM_DISPNAME'); //   /* *     ,     ,   . *  :  , id    . */ $author = explode('FROM_DISPNAME ', $aut); $chat = explode('CHATNAME ', $ch); $message = explode('BODY ', $mess); echo $author[1].': '.$message[1]."\n"; //      /* *   ,        , -  *          "!". */ if ($message[1][0] == '!') { self::reply($chat[1], $message[1], $message_id); } } public function reply ($chat, $message, $id) { global $n; /* * ,   id ,    ,   ,  *        "!test"   ,     *   ,      . -       id * ,    ,   id ,    . */ self::$last_id = $message; if (self::$last_id <= $message) { switch ($message) { case '!test': $reply = 'It\'s work!'; break; case '!help': $reply = ',  '; break; default: $reply = ' !help'; break; } if ($reply != '') $n -> Invoke('CHATMESSAGE '.$chat.' '.$reply); //  } else { echo ' !'."\n"; } } } $dbus -> registerObject('/com/Skype/Client', 'com.Skype.API.Client', 'phpSkype'); //    while(1) { $s = $dbus -> waitLoop(1); } ?> 


Now our script is able to respond to certain messages, and if you change it a bit, it will be able to respond to any messages. If you think offhand, you can implement something interesting on the site, if it is hosted on your computer, well, or through a bunch of scripts on localhost and hosting, this is how you come up with.

A detailed list of commands that are available through the Invoke () method can be found here .
And [Irrelevant because of the actions of Microsoft'a] source code and a more functional script.

I did all this in the Ubuntu 12.04 LTS system, I don’t guarantee work on other systems and I will be glad if someone checks it.
Any ideas are welcome.
Thank you for reading!

Source: https://habr.com/ru/post/149850/


All Articles