Yoan Blanc’s weblog

Another swiss guy lost in Paris

Twootr, do more with your bot

Yoan BlancWed, 27 Feb 2008, , , , , , ,

I’ve already did some experiments here with a Jabber bot that was kinda basic, an interface to Tumblr. It toke me a consequent amount of time to make it more (or finally) useful and there it is. A web application composed of a website and a IM bot that aims to be a Twitter-like. You have your buddies, and are able to interact with them using either the web interface or the IM (with a GTalk or Jabber account). This is a proof of concept and you can do whatever you want with it at your own risk.

First, JabberBot (src) received some precious enhancement especially to manage the roster (formerly the buddy list). I was wondering about using the status message of the users but finally forget it. In order to get the status of the user, you have to make it on of your buddies. So the current bot, can had someone to his buddy roster and ask him to become friends which is called subscribe.

myBot.add("you@jabber.org")
myBot.sendPresence("you@jabber.com", "subscribe")

The default behavior of the current bot is to keep a mutual relation either we are friends (both) or I don’t know you. Jabber permits more states I’m your friend (from) , you’re mine (to) or we aren’t friends at all (none) but I’m keeping you in my buddy list (which is useful for your ex-girlfriends).

Upon this bot, there is a Twootrbot (src) that will publish the messages that he gets from his buddies to the website and will broadcast the message entered inside the website. The main question was how having a bot (which is like an infinite process) and a website (which is like lots of processes) that communicates together.

I tried the named pipes but they implies that you’re on the same machine and how you create one, open it, write in it has to be done in a specific order. Find here a little Python code that plays with named pipe (for Unices only). And finally used XML-RPC (via xmlrpclib) with is very simple to use and permits the bot to be distant from the web server. It’s a kinda heavy IPC system but it does the job.

A sample on the client side, that send a “Hello World!” to the echo method on the given server. XML-RPC is very very simple.

from xmlrpclib import ServerProxy
xmlrpc = ServerProxy('http://example.org/')
xmlrpc.echo('Hello World!')

The purpose of the bot is to get messages from IM and write them into the database. Or receive a XML-RPC call asking it to add a new buddy or to send a message to the recipients list. In fact the Jabber and XML-RPC bots are separate classes but runs in the same process and communicate together by simple function calls.

Now the big part, the website. I’ve chosen web.py to get something very malleable and that you don’t have to understand that much before doing something. To make simple, web.py offers you to build easily a web site but, like PHP (sorry for the comparison), which means you have to clean it up afterwards. You may like that or preferring having a true framework that, in a way, forces you a way of working. Basically, I’m telling here that every tool that has a create_project first operation falls into this second category.

It’s an endless troll but it seems that the MVC doesn’t fit that well the web, and for example Django ended up with a different acronym: MTV. I liked being able to try finding what kind of approach I would prefer. I’m still not convinced by what I’ve done but is happy of been able to try something in that field.

What I like about web.py:

  • simple;
  • the documentation fit in one page;
  • good with the bottom up principle;
  • SQL inside, kind of (I still like doing some);
  • small and friendly community.

To resume that, as I like doing small things, quickly, to test an idea and then throw away (or blog about it). But Django have, from what I know, a lot more of good gems for a long term and more that one guy work. And as I need my daily programming.reddit.com, using web.py (which was built for it) came naturally.

Last but not least, the JavaScript part is built upon Base2 like my previous JavaScript blog posts. Base2 is still young (like web.py) and needs some attention. What is interesting with it is that it doesn’t provide you a framework to work with like most of the popular JavaScript tools (YUI, jQuery, Dojo, Prototype, MooTools). So no animations, no Ajax, no XPath selectors only methods that are parts of some specifications and not yet part of all the browsers (like querySelectorAll which only exists in Webkit at the moment), a packaging system (which seems to me very similar to the Perl ones) or a decent Event system (you may be aware that both MSIE and the W3C still think differently on that one).

I had to build an Ajax implementation (src) in order to do some of it because I didn’t find any. Anyways it was fun, it’s usage is very similar to the interface that Prototype offers. XmlHttpRequest works in a way that is close to that: some parameters like the url, the method, the body, ... and a lot of callbacks.

I’m inviting you to download it and test it. You need a jabber account, a GTalk one may not work for the bot. The python dependencies are: web.py, sqlite3 and twisted.words. Or if you are lazy, ping me: greut@swissjabber.ch, I should be able to run it on my computer for you.

Voici un post que j’ai sur la conscience depuis un bon moment et qu’il me fait plaisir de jeter à la lumière. Bon ou mauvais, juste un peu ambicieux et ça prend du temps tout ça. Ce même temps qu’il est difficile de mettre bout à bout à dessein. Donc, il s’agit d’une application internet, honteusement recopiée sur Twitter, permettant d’intéragir avec ses amis (ou juste des gens) en se servant autant du site internet que par messagerie instantanée (via Jabber ou Google Talk). Tout ceci sans prétention aucune et par pur plaisir de mettre rendu compte de comment on peut architecturer un système tel que celui-ci.

Premièrement, le bot utilisé précédemment (JabberBot) a reçu une mise à jour assez importante dans l’optique de pouvoir gèrer ce qu’on appelle le roster, autrement dit, la liste d’amis. Il sait désormais se faire des amis tout seul pour autant qu’on le lui demande gentillement. Un détail important du système est qu’on ne peut connaître le status de ces contacts, donc savoir s’ils sont connectés ou pas et au mieux lire leur message de status, sans gérer le roster. Ce qui implique un peu plus de boulot. Jabber ou son protocole XMPP étant basé sur du XML, il y a de nombreuses manières de l’étendre pour accèder à différentes fonctionnalité, comme l’accès aux emails GMail depuis le client GTalk pour ne cite que lui. En somme, c’est un sujet passionnant.

Les fonctionnalités de notre bot nouveau (TwootrBot) seront de collecter les messages de l’externe pour les afficher sur le site internet (pour faire simple, les écrire en base de donnée) et également envoyer les notifications aux personnes le souhaitant. Il va également faire la demande d’ajout de lui-même lors de l’inscription.

Un point sensible a été de savoir comment le bot (qui fontionne en boucle, toujours prêt à recevoir un message de l’extérieur) va-t-il communiquer avec le site internet qui rend des de multiples pages. C’est faire de la communication inter-processus. J’ai tenté la version très bas niveau: les tubes nommés (source) qui s’ils sont efficaces restreigne l’usage à une machine uniquement, de manière unidirectionnelle (ce qui est contournable) et me semble complexe si on veut éviter tout interblocage. Puis j’ai testé XML-RPC (qui travaille au niveau HTTP), qui séduit directement de par sa très simple mise en œuvre (avec xmlrpclib et Twisted notamment).

Ensuite vient le site web en lui-même qui, pour faire simple (et dans la suite de précédant posts), est basé sur web.py qui me permet de très simplement mettre en œuvre deux trois pages sans trop se soucier de détails inintéressant à ce stade là. Voir le schéma de la base de donnée pour avoir une idée de ce qui a été fait. Web.py a été conçu pour reddit et je ne vous cacherai pas que j’en suis un féru consommateur (merci Batiste). Dans les points qui me plaisent:

Il est possible d’explorer différents aspects d’une application web, ce qui est plaisant et pratique pour ce genre d’expérimentation sans objectif précis. Il est certain que Django ou Pylons offrent un vrai plus dès qu’il y a un souhait d’aller plus loin.

Et dernier point, tout ceci contient un peu de JavaScript permettant la mise à jour dynamique des flux de messages. J’ai continué avec Base2, ce qui m’a demandé de faire l’implantation d’Ajax from scratch (car ce dernier n’en contient simplement pas). Encore Base2 pour la simple raison que comme web.py, il est minimaliste et n’apporte que les choses qui manquent, de manière uniforme, entre les différents navigateurs tel que querySelectorAll, le modèle évènementiel, des classes comme Array2, Date2 plus complète et également un système de packages (qui me semble être familier avec celui de Perl, allez savoir pourquoi). Vous n’y trouverez pas d’animations préfaites ou de carousel par exemple.

Je vous invite à télécharger tout ceci, à le tester ou n’hésitez pas à me laisser un message Jabber/GTalk à greut@swissjabber.ch j’aurai plaisir à vous donner une adresse où le tester au besoin.

About

meYoan Blanc is a web developer that lives in Paris (19, rue Bichat75010France) works for Yahoo! and comes from Switzerland ( La Chaux-de-Fonds). This web site is for this weblog, a playground for random stuffs and can help keepingconnected with some friends out there.

Get my vCard or contact me by phone (+33 1 74 30 12 92) or email ().

Misc

RSS, list.blogug.ch

This site reflects only my opinion and is not affiliated with my employer.

copyright 2006-2007 — doSimple.ch