[Twisted-Python] Creating a protocol-agnostic chatbot with Twisted words
![](https://secure.gravatar.com/avatar/112d61920c9daa3192b59458acf1c8d2.jpg?s=120&d=mm&r=g)
Hello, I am trying to create a chat bot that can work over both the XMPP and the MSN protocol using Twisted words. I have a couple of questions as a Twisted newbie :-) Is there any documentation on how to create a protocol-agnostic chatbot with Twisted words? Or are there applications using Twisted words that offer multiple front-ends? I have a XMPP version working, but had some problems finding my way around a few features. I don't know how to get a list of contacts, or initiatiate a group chat. Even if I succeed in doing this, would it be possible to generalize this for both XMPP and MSN? Finally, I was wondering how mature Twisted words is. It seems to work well for XMPP, but how mature is the MSN support? Are there other libraries I could consider, or is it possible to generalize other libraries within Twisted? Thanks in advance! -- Jo Vermeulen
![](https://secure.gravatar.com/avatar/521b986cb6bb1912f71793aa76507a19.jpg?s=120&d=mm&r=g)
There's a twisted MSNP8 (or is it MSNP9) implementation. It's experimental but it works. I managed to write an MSN gateway for XMPP deriving most of the codes from it. ---- Alvin Delagon On Mon, May 26, 2008 at 6:49 PM, Jo Vermeulen <jo.vermeulen@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/112d61920c9daa3192b59458acf1c8d2.jpg?s=120&d=mm&r=g)
Hello Alvin, Thanks, MSNP8 should be enough for a chat bot. I'll have a look at the twisted.im module, it seems to contain a basic multi-protocol chat client. I had another question about the Jabber example (xmpp_client.py) in Twisted words. When I quit the application with Ctrl+C, it throws an exception: Disconnected. Traceback (most recent call last): File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 228, in mainLoop self.runUntilCurrent() File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 561, in runUntilCurrent call.func(*call.args, **call.kw) File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 414, in _continueSystemEvent callable(*args, **kw) File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 375, in disconnectAll failure.Failure(main.CONNECTION_LOST)) --- <exception caught here> --- File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 48, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 33, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 59, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 37, in callWithContext return func(*args,**kw) File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 576, in connectionLost Connection.connectionLost(self, reason) File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 416, in connectionLost protocol.connectionLost(reason) File "/usr/lib/python2.5/site-packages/twisted/words/xish/xmlstream.py", line 82, in connectionLost self.dispatch(self, STREAM_END_EVENT) File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 227, in dispatch self._eventObservers[(priority, event)].callback(object) File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 37, in callback methodwrapper(*args, **kwargs) File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 21, in __call__ self.method(*nargs, **nkwargs) File "xmpp_client.py", line 58, in disconnected reactor.stop() File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 342, in stop raise RuntimeError, "can't stop reactor that isn't running" exceptions.RuntimeError: can't stop reactor that isn't running Is there any way to modify this example so that it stops gracefully when Ctrl+C is pressed? Embedding the main script code in a try-except statement that catches KeyboardInterrupt doesn't help since Twisted seems to catch all exceptions itself. I read about deferred's and errbacks, but I don't know where to add an errback to in the XMPP example. To the client? The XmlStream? The reactor? Any pointers would be greatly appreciated ... Thanks! -- Jo On Mon, May 26, 2008 at 2:37 PM, Alvin Delagon <adelagon@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/51c264443c5d66a9345a92af933ea4f5.jpg?s=120&d=mm&r=g)
On Mon, 2008-05-26 at 15:03 +0200, Jo Vermeulen wrote:
I haven't seen that behavior before, but I can confirm this happens. I poked around a bit with the example. It seems that the socket is closed before its factory is shut down. It might be that the reactor shutdown procedure has changed, so I think this behavior is the result of a bug, either in the example or somewhere else. It needs more investigation, so it would be nice if you could file a ticket for it.
I don't know of a good solution, at the moment. If the above bug gets resolved, you shouldn't need to do anything beyond that. -- Groetjes, ralphm
![](https://secure.gravatar.com/avatar/6338a08e3ddf6a3534d9329bdfea3bee.jpg?s=120&d=mm&r=g)
On Mon, May 26, 2008 at 6:02 PM, Ralph Meijer <twisted@ralphm.ik.nu> wrote:
Done: http://twistedmatrix.com/trac/ticket/3270
OK thanks! -- Jo
![](https://secure.gravatar.com/avatar/d6328babd9f9a98ecc905e1ccac2495e.jpg?s=120&d=mm&r=g)
On 01:03 pm, jo.vermeulen@gmail.com wrote:
Thanks! I would like to encourage you to look at this code, but be warned: it's pretty old, not very well tested, and not very well documented. I really hope that you stick with it and help us fix it up. Just don't expect it to work out of the box :). (It should actually be in twisted.words.im, in any version of Twisted that you should even consider using today...)
![](https://secure.gravatar.com/avatar/51c264443c5d66a9345a92af933ea4f5.jpg?s=120&d=mm&r=g)
On Mon, 2008-05-26 at 12:49 +0200, Jo Vermeulen wrote:
There is no such abstraction in Twisted for XMPP, yet. I've worked on a framework for implementing XMPP's 'subprotocols' in the Wokkel library [1] and this framework has been contributed back into Twisted recently, and has been part of the Twisted 8.0.1 release. On top of that there are several of such subprotocols implemented in Wokkel, among them the protocol implementations for dealing with rosters (client and server side). All of those should eventually go back in Twisted when I get around to it. As far as I know, there is no implementation for the client side of the multi-user chat (MUC) protocol yet built on Twisted, although there is a MUC service called Palaver. Its authors are considering rewriting the protocol handling part to match the subprotocol abstraction I mentioned above.
Even if I succeed in doing this, would it be possible to generalize this for both XMPP and MSN?
Sure. No one has stepped up to make that happen, recently. Personally, I have very little interest in supporting closed protocols like MSN's. However, there is the concept of XMPP gateways to other IM networks that might be useful for you. It would allow you to create a bot that talks using one protocol (XMPP).
I would say that the XMPP and IRC parts of Words have seen the most activity recently. The XMPP stuff works well, supports a fair number of projects, but is a bit spartan. I'm working hard to address the latter, though. I cannot comment much on the other parts, although I know there has been work on Twisted based XMPP gateways to AIM and ICQ, that have more mature implementations of those than what is currently in Words. So far there hasn't been much progress to move those improvements back into Words [2]. [1] http://wokkel.ik.nu/ [2] http://twistedmatrix.com/trac/ticket/1081 -- Groetjes, ralphm
![](https://secure.gravatar.com/avatar/521b986cb6bb1912f71793aa76507a19.jpg?s=120&d=mm&r=g)
There's a twisted MSNP8 (or is it MSNP9) implementation. It's experimental but it works. I managed to write an MSN gateway for XMPP deriving most of the codes from it. ---- Alvin Delagon On Mon, May 26, 2008 at 6:49 PM, Jo Vermeulen <jo.vermeulen@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/112d61920c9daa3192b59458acf1c8d2.jpg?s=120&d=mm&r=g)
Hello Alvin, Thanks, MSNP8 should be enough for a chat bot. I'll have a look at the twisted.im module, it seems to contain a basic multi-protocol chat client. I had another question about the Jabber example (xmpp_client.py) in Twisted words. When I quit the application with Ctrl+C, it throws an exception: Disconnected. Traceback (most recent call last): File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 228, in mainLoop self.runUntilCurrent() File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 561, in runUntilCurrent call.func(*call.args, **call.kw) File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 414, in _continueSystemEvent callable(*args, **kw) File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 375, in disconnectAll failure.Failure(main.CONNECTION_LOST)) --- <exception caught here> --- File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 48, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 33, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 59, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 37, in callWithContext return func(*args,**kw) File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 576, in connectionLost Connection.connectionLost(self, reason) File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 416, in connectionLost protocol.connectionLost(reason) File "/usr/lib/python2.5/site-packages/twisted/words/xish/xmlstream.py", line 82, in connectionLost self.dispatch(self, STREAM_END_EVENT) File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 227, in dispatch self._eventObservers[(priority, event)].callback(object) File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 37, in callback methodwrapper(*args, **kwargs) File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 21, in __call__ self.method(*nargs, **nkwargs) File "xmpp_client.py", line 58, in disconnected reactor.stop() File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 342, in stop raise RuntimeError, "can't stop reactor that isn't running" exceptions.RuntimeError: can't stop reactor that isn't running Is there any way to modify this example so that it stops gracefully when Ctrl+C is pressed? Embedding the main script code in a try-except statement that catches KeyboardInterrupt doesn't help since Twisted seems to catch all exceptions itself. I read about deferred's and errbacks, but I don't know where to add an errback to in the XMPP example. To the client? The XmlStream? The reactor? Any pointers would be greatly appreciated ... Thanks! -- Jo On Mon, May 26, 2008 at 2:37 PM, Alvin Delagon <adelagon@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/51c264443c5d66a9345a92af933ea4f5.jpg?s=120&d=mm&r=g)
On Mon, 2008-05-26 at 15:03 +0200, Jo Vermeulen wrote:
I haven't seen that behavior before, but I can confirm this happens. I poked around a bit with the example. It seems that the socket is closed before its factory is shut down. It might be that the reactor shutdown procedure has changed, so I think this behavior is the result of a bug, either in the example or somewhere else. It needs more investigation, so it would be nice if you could file a ticket for it.
I don't know of a good solution, at the moment. If the above bug gets resolved, you shouldn't need to do anything beyond that. -- Groetjes, ralphm
![](https://secure.gravatar.com/avatar/6338a08e3ddf6a3534d9329bdfea3bee.jpg?s=120&d=mm&r=g)
On Mon, May 26, 2008 at 6:02 PM, Ralph Meijer <twisted@ralphm.ik.nu> wrote:
Done: http://twistedmatrix.com/trac/ticket/3270
OK thanks! -- Jo
![](https://secure.gravatar.com/avatar/d6328babd9f9a98ecc905e1ccac2495e.jpg?s=120&d=mm&r=g)
On 01:03 pm, jo.vermeulen@gmail.com wrote:
Thanks! I would like to encourage you to look at this code, but be warned: it's pretty old, not very well tested, and not very well documented. I really hope that you stick with it and help us fix it up. Just don't expect it to work out of the box :). (It should actually be in twisted.words.im, in any version of Twisted that you should even consider using today...)
![](https://secure.gravatar.com/avatar/51c264443c5d66a9345a92af933ea4f5.jpg?s=120&d=mm&r=g)
On Mon, 2008-05-26 at 12:49 +0200, Jo Vermeulen wrote:
There is no such abstraction in Twisted for XMPP, yet. I've worked on a framework for implementing XMPP's 'subprotocols' in the Wokkel library [1] and this framework has been contributed back into Twisted recently, and has been part of the Twisted 8.0.1 release. On top of that there are several of such subprotocols implemented in Wokkel, among them the protocol implementations for dealing with rosters (client and server side). All of those should eventually go back in Twisted when I get around to it. As far as I know, there is no implementation for the client side of the multi-user chat (MUC) protocol yet built on Twisted, although there is a MUC service called Palaver. Its authors are considering rewriting the protocol handling part to match the subprotocol abstraction I mentioned above.
Even if I succeed in doing this, would it be possible to generalize this for both XMPP and MSN?
Sure. No one has stepped up to make that happen, recently. Personally, I have very little interest in supporting closed protocols like MSN's. However, there is the concept of XMPP gateways to other IM networks that might be useful for you. It would allow you to create a bot that talks using one protocol (XMPP).
I would say that the XMPP and IRC parts of Words have seen the most activity recently. The XMPP stuff works well, supports a fair number of projects, but is a bit spartan. I'm working hard to address the latter, though. I cannot comment much on the other parts, although I know there has been work on Twisted based XMPP gateways to AIM and ICQ, that have more mature implementations of those than what is currently in Words. So far there hasn't been much progress to move those improvements back into Words [2]. [1] http://wokkel.ik.nu/ [2] http://twistedmatrix.com/trac/ticket/1081 -- Groetjes, ralphm
participants (5)
-
Alvin Delagon
-
glyph@divmod.com
-
Jo Vermeulen
-
Jo Vermeulen
-
Ralph Meijer