PHP: Skype Bots (php, Skype4COM)
As the title suggests, this is a story about how you can perform actions in Skype from an external application (written in PHP this time). The range of actions is quite impressive. From this rich set we will use the chat procedures - sending a message and receiving a message.
Although Skype was originally designed for voice, video and text communication between two or more participants, clever people found that wasn't enough. Until Skype itself released a COM module, through which you can then perform various Skype actions from another application.
As the title suggests, this is a story about how you can perform actions in Skype from an external application (written in PHP this time). The range of actions is quite impressive and can be viewed here https://developer.skype.com/Docs/Skype4COMLib. From this rich set we will use the chat procedures - sending a message and receiving a message.

Figure 1. Platform.
Skype COM applications
Let's now abstract away from the technical nuances (and possibilities :) ) and think about where and how such a construct could be used.
- All correspondence is automatically saved to a web page (blog, Twitter, etc.)
- A message is added to the web page only via a special command. For example, Add: Currently writing about skype4com.
- URL fixing. For example: document.doc gets rewritten as //server/user/document.doc, making that link real and usable.
- Mini Google. For example: google: skype4com and in response you receive the first 3 results. Or PageRank, or keyword position in search, or Alexa rank, or anything else.
- The bot itself periodically sends a brief notification to a specific conference (e.g., work) - invoice No. 123 has been paid, or today is Jānis Bērziņš's name day and Liena Bērziņa's birthday.
- Dictionary. We write, for example: en:lullaby and in response we receive - lv:šūpļa dziesma
- Info service. For example: ?how much disk space is left on the server. Answer: 1GB.
- Chatbot. However good it turns out to be... If there's enough intelligence to build a sensible AI-based one, it could turn out to be a decent conversation partner :)
- And of course, a bunch of various other types of commands.
Where to start?
First, you need Skype itself with a registered Skype user account. Given that the Skype protocol is closed (i.e. not publicly viewable), all the actions described below will only be possible if the Skype client is running (available for Windows, Linux and Mac). Note that the Skype client has only a GUI version...
Second. The Skype4COM component. In principle it is a DLL file which can be registered with the regsvr32 command. It didn't work :(. So the alternative approach: during installation, choose Options (in the very first window) and tick "Install Skype Extras Manager". With that, Skype4COM will be installed alongside. But the magic doesn't end there.

Third. Except for the case where PHP will be run from the command line (i.e. php –f skype.php), one thing must be understood - when initialising a connection via the COM object, another Skype instance will be launched (if Skype is not yet active) and authentication credentials will be requested.
In my experiments I did the following:
- - launched Skype and logged in;
- - Under Administrative Tools > Services > Apache > Properties > Log On and ticked

- - Launched PHP and allowed the Skype connection.

If PHP did not show some mysterious COM error or "Skype4COM unable to attach (Wait Timeout)", then everything works and you can proceed with PHP!
N.B. When writing PHP code, two things must be kept in mind:
1. Allow time for the command to execute. For example, use com_message_pump(4000);
2. If we want to "wait" for some Skype event, e.g. an incoming message, then somewhere at the beginning set_time_limit(0); and at the end - the infinite waiting loop:
while(!$sink->terminated) {
com_message_pump(1000);
}
Some good ideas and a real Skype bot:
http://www.voidstar.com/void.bot/source/skypebot.txt
Useful links:
Skype. Skype4COM reference - https://developer.skype.com/Docs/Skype4COM
Skype bots: http://www.voidstar.com/void.bot/
Linux skype wrappers - http://labs.gree.jp/Top/OpenSource/Skype-en.html
PHP Skype COM example:
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_time_limit(0);
define("ln", "");
class _ISkypeEvents {
var $terminated = false;
var $attached = false;
var $msg;
function CallStatus($call, $status) {
echo 'CallStatus: '.$status. ln;
}
function MessageStatus( $msg, $status ) {
global $skype;
// incoming messages (status = 2)
if($status == 2) {
// if the message is "exit", terminate the php script
// Stop button won't work, as the apache service will hang...
if($msg->Body == "exit") {
$this->terminated = true;
}
// who the message came from
$name = $msg->FromDisplayName;
$to = $msg->FromHandle;
error_log("$name ($to): $msg->Body");
// send back
$skype->SendMessage($to, "I am only chatbot...
My master is away, but I will leave your message to him!");
}
}
function UserStatus($status) {
echo 'UserStatus: '.$status. ln;
}
function AttachmentStatus($status) {
echo 'AttachmentStatus: '.$status. ln;
if($status==0)
{
$this->attached = true;
}
}
}
//Create skype object
$skype = new COM("Skype4COM.Skype");
$sink =& new _ISkypeEvents();
$sink->convert = $skype->convert();
//Increase timeout to allow more time.
$skype->timeout = 60000;
com_event_sink($skype, $sink, "_ISkypeEvents");
$convert = $skype->convert;
$convert->language = "en";
//Run Skype client if it is not running.
if (!$skype->client()->isRunning())
{
echo 'Skype is not running, trying to start up.'.ln;
$skype->client()->start(true, true);
}
//Try to attach to Skype.
try
{
$skype->Attach();
} catch(Exception $e) {
echo 'Exception: Not attached.'.ln;
}
com_message_pump(4000);
//If attached, check status and make a call.
if($sink->attached) {
echo 'Attached.'.ln;
// All ok, start waiting for incoming messages
error_log("ready");
while(!$sink->terminated) {
com_message_pump(1000);
}
} else {
echo 'Not attached.'.ln;
}
echo "Finish!";
comments