2008-08-07

Dbus, knotify and pidgin

Threw together an ugly python script sendind pidgin notifications to knotify:

#!/usr/bin/env python

import re

html_tags = re.compile('<.*?>')

def my_func(account, sender, message, conversation, flags):
kn.event("receivedImMsg", "pidgin", [], '<b>' + sender + '</b>: ' + html_tags.sub('', message), [0,0,0,0], [], 0, dbus_interface="org.kde.KNotify")

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()

kn = bus.get_object("org.kde.knotify", "/Notify")

bus.add_signal_receiver(my_func,
dbus_interface="im.pidgin.purple.PurpleInterface",
signal_name="ReceivedImMsg")

loop = gobject.MainLoop()
loop.run()

Combine with this file: $HOME/.kde/share/config/pidgin.notifyrc


[Event/receivedImMsg]
Action=Sound|Popup|Taskbar
Execute=
KTTS=
Logfile=
Sound=KDE-Im-Message-In.ogg

... and your're good to go ;)

Pidgin reference

11 comments:

James Muscat said...

Thanks for this - a great demonstration of how easy dbus is (and how useful KNotify can be)!

Erik said...

Hi, I tried to figure out how to use this little howto, I think the second script must be in a file I had to create, which is as you said $HOME/.kde/share/config/pidgin.notifyrc, I'm trying to understand where I have to type the first script tho, can you help me please?:)

Thomas A said...

It does not really matter what you name it, but as it is a python script maybe pidgin_notify.py

Then make it executable (chmod +x pidgin_notify.py) and run it (./pidgin_notify.py).

To make it autostart you could put it in ~/.kde/Autostart

Erik said...

When I try to execute the .py I have this error:

File "/usr/bin/pidgin.py", line 8
kn.event("receivedImMsg", "pidgin", [], '' + sender + ': ' + html_tags.sub('', message), [0,0,0,0], [], 0, dbus_interface="org.kde.KNotify")
^

I tried to figure out what the problem is but cant :-\

Thomas A said...

Wasn't there any error message? Also I can't make out where the '^' is pointing because of the blog formatting, please use a pastebin and link it.

Erik said...

This should be enough :)

http://img193.imageshack.us/i/schermata2g.png/

Thomas A said...

There should be a tab character right before
kn.event("receivedImMsg" ...

Seems it got lost when I posted the code on the blog.

Anonymous said...

I'm trying to use this script and I got the same error on line 8 that another user posted. I'm trying to add the missing tab character as you indicated but I still get an error about "expecting an indented block" on line 8. I simply inserted '/t' in front of kn.event("rec... I've tried every which way but loose trying to make this work with no success. I'm sure the answer is simple...for a programmer! Can you PLEASE add the corrected script text and a clearer howto to get this to work?!? We're not all programmers but we can follow clear directions. Thanks!

Thomas A said...

I have a lot of stuff to do right now, but I can offer you some advice on your problem:

By tab character I mean just pressing the tab key to insert a tab, alternatively insert four spaces.

Python does not have { and } to create blocks but does instead use identation, hence the problem. As the line is part of the function my_func (should have named that one better, but it was more a proof of concept) it needs to be indented.

Christopher said...

Works! Thank you! Any plans to refine this? KDE4 users are sorely in need of better notifications for favorites like Pidgin (where Kopete doesn't fill the bill.) Thanks for the quick help.

Anonymous said...

For KDE4 there's a Perl plugin that works well:
http://code.google.com/p/pidgin-knotifications/

It can switch to Gnome (libnotify) instead of knotify. There's a Windows version too that works with Growl.