Robert Basic's blog

Posts tagged 'signals'

Creating a chat bot with PHP and Dbus

by Robert Basic on January 08, 2012.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

Now that we know how to use DBus to communicate with Pidgin from PHP and how to listen to DBus signals, it’s time to put it all together by creating a simple chat bot! Nothing fancy, just a simple script that runs somewhere on some server and, by using a Pidgin account, can respond to some basic queries we send it.

What did we get?

As we want our script to receive messages from an other account, first we need to listen to the ReceivedImMsg event on the im.pidgin.purple.PurpleInterface interface. The data we get with that event is the ID of receiver’s account, the sender of the message, the actual message and the conversation’s ID (and some flags which we’re not interested in):

$interface = "im.pidgin.purple.PurpleInterface";
$method = "ReceivedImMsg";
if ($signal->matches($interface, $method)) {
    $data = $signal->getData()->getData();
    $receiver = $data[0];
    $sender = $data[1];
    $message = $data[2];
    $conversation = $data[3];

Of course, this is only for this one event, for data associated with other events see Pidgin’s manual.

Who's there?

The event we are listening for will fire for any and all accounts, no matter who is the sender or the receiver of the message. We need to make sure that the receiving and the sending accounts are the correct ones, that the receiver is connected and that the receiver and the sender are contacts, “buddies”:

if ($receiver == 2034 && $proxy->PurpleAccountIsConnected($receiver)
    && $proxy->PurpleFindBuddy($receiver, $sender) == 3681) {

The numbers 2034 and 3681 are the account IDs for my accounts I used in this example; you’ll need to figure out yours.

Sending a response

Now that we know who’s talking to whom, we can act upon the received message, do something with it, create a response message and send it back! The data we got with the event, has the ID of the conversation between the two accounts. We create a new instant message for that conversation and send it on it’s merry way with our clever response message:

$im = $proxy->PurpleConvIm($conversation);
$proxy->PurpleConvImSend($im, $responseMessage);

As for what action the script can take upon a new message, is really up to the developer: it can do simple stuff like sending back the current uptime of the server or the current IP, running other tools like usher and sending that result back, or whatever is necessary.


As this little bot is supposed to run on some server, the easiest way to run it as a “daemon” is to put the script in a background job via nohup:

$ nohup php chat.php &

If needed, creating daemons in PHP can be done too.

And that’s about all what’s needed to create a chat bot. See a complete example here on Github.

As for, is PHP the right tool for creating this kind of thing, I don’t know, maybe, maybe not. What I do know, is that I had fun writing all this and I learned a lot along the way :)

Happy hackin’!

Tags: bot, dbus, events, php, pidgin, signals.
Categories: Development, Programming.

Listening to Dbus signals with PHP

by Robert Basic on December 26, 2011.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

In my previous post I described (tried, at least) how to communicate with Pidgin from PHP, by using the Dbus PHP extension.

The good part is that not can we only call different methods against Pidgin’s libpurple API, we can also listen to different signals on different events, that are sent via Dbus. Some of the events that are signalled are when a chat message is recieved, a friend comes online, a file is sent, or any other from a list of some 110 different events.

The PHP Dbus extension allows us to watch for one exact signal on an interface, or for all signals on an interface. Of course, we can add watches on multiple interfaces at once.

Watching for signals

Once we know the interface and/or the specific signal we’re interested in, we can add a watch on it. This is done by calling the addWatch method on the Dbus object, were the first parameter is the interface, and the second, optional parameter is the exact signal we want to listen to.


$dbus = new Dbus(Dbus::BUS_SESSION);

// watching for a specific signal
$dbus->addWatch("im.pidgin.purple.PurpleInterface", "ReceivedImMsg");
// or watching on an entire interface
// $dbus->addWatch("im.pidgin.purple.PurpleInterface");
// also can listen to different interfaces at the same time

Next, we need a way to actually get these signals when the events occur. For this we are using the waitLoop method of the Dbus class. That method accepts a number as a parameter, which is the number of miliseconds it should wait between requests. If an event happened on the interface we’re watching, it will return the signal, which is a DbusSignal; otherwise we’ll get a null:

do {
    $signal = $dbus->waitLoop(1000);

    if ($signal instanceof DbusSignal) {
        // even if we watch only for one signal on one interface
        // we still can get rubbish, so making sure this is what we need
            // data is in this weird DbusSet object thingy
            $data = $signal->getData()->getData();
            echo "Got stuff!\n";
} while (true);

Once we got the signal, to make sure that the signal is really the one we’re interested in, we call the matches method on it. The first parameter is the interface and the second is the signal.

Each event has (can have? not sure yet) additional data associated with it. To get to it, for some odd reason, we need to call getData()->getData() on the signal; note that this is in case of listening on libpurple’s interfaces, not sure about others. Experiment. Also, what kind of data is returned, again, depends on the interface and/or the event - some return arrays, some strings.

Have a look at the Github repo for some more examples (the dbus-signals* files).

In the third, and probably last post in this dbus mini-series, I’ll try to build a bot with which I can communicate and issue out commands to.

Happy hackin’!

Tags: dbus, events, listen, php, pidgin, signals, xmpp.
Categories: Development, Programming.

Passing arguments to custom slots in PyQt

by Robert Basic on November 30, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

While hacking on ape, I came to a situation where I need to pass some arguments to a custom defined slot. The slot is being called from different signals, one where the argument is passed by PyQt itself and a second one where I need to programmatically pass the argument to the slot.

First I tried with something like:

action = QAction("My action", parent)

which ended in an error: TypeError: connect() slot argument should be a callable or a signal, not ‘NoneType’

After a bit of poking around I passed a lambda function to the connect() method:

action = QAction("My action", parent)
action.triggered.connect(lambda arg=my_argument: my_slot(arg))

Works like a charm.

Also this is my first try to use github gists as a way to embed/highlight code. Hope it’ll work out.

Happy hackin’!

Tags: ape, lambda, pyqt, python, signals, slots.
Categories: Programming.

Connecting signals and slots with PyQt - the new style

by Robert Basic on November 09, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

While working on ape I had a problem with figuring out how to properly connect a signal to a slot, where the signal is emitted by a QTreeView widget. As this is not my first app with python and pyqt, I was doing something like (this is, btw, the “old style”):

self.connect(widget, SIGNAL("emitted_signal()"), self.my_slot)

but it simply didn’t work. Nothing happened. I was trying all different of connect/signal/slot combinations but everything was just dead silent. Google gave only pretty much old posts talking about QT3. Then I figured that, because the QTreeView is “sitting” inside a QDockWidget, maybe that dock widget thingy is somehow intercepting/taking over the signals. Nope. Wth? Wtf is going on? Current pyqt version is (on my machine) 4.6. Last time I used pyqt it was something like 4.2 or 4.3. Something must’ve been changed in the mean time. Off to the pyqt docs I go (btw, I use the official QT docs, the C++ version, there isn’t really a big difference from pyqt): PyQt reference, chapter 7 - “New-style Signal and Slot Support”. A-ha! They changed it! Here is an example of the “new style”:


Oh my, isn’t that just beautiful?! Much more readable and simpler, for me at least. And it works! Yay! The QTreeView signals are happily connected to slots, thus, I’m happy too.

A few paragraphs later, turns out that the “old style” isn’t thrown out, it should still work. Why it didn’t work for me escapes me at the moment, but honestly, I don’t really care as long as the new style is working.

Happy hackin’!

Tags: ape, pyqt, python, signals, slots.
Categories: Development, Programming.