Python Bot

Hello,

I am trying to build a sample Bot in our local environment using ejabber. I am not able to make a simple component to work using any of the sample codes that are available online. I am sure it may be the setting, or configuration on my end. I tried the code from http://metajack.im/2008/09/25/an-xmpp-echo-bot-with-twisted-and-wokkel/. I build both the server bot and to test it, a client component.

The Python server component seem to be running working without any error and I would the following message in the ejabber log file

=INFO REPORT==== 2010-12-09 03:01:52 ===
I(<0.7551.0>:ejabberd_service:208) : Route registered for service "localhost"

When the client tries to connect, I would see the message:

=INFO REPORT==== 2010-12-09 00:49:32 ===
I(<0.7480.0>:ejabberd_service:118) : ({socket_state,gen_tcp,#Port<0.6169>,<0.747 9.0>}) External service connected

However, I am not getting any response from the Python server component (which is supposed to echo back the same message). I am not sure where the issue is.

In the ejabberd config file, I have the following settings:

{listen,
[
{5222, ejabberd_c2s, [
{access, c2s},
{shaper, c2s_shaper},
{max_stanza_size, 65536},
starttls, {certfile, "/etc/ejabberd/ejabberd.pem"}
]},
{8888, ejabberd_service, [
{access, all},
{hosts, ["localhost"],
[{password, "ssshh"}]
}
]},
{5269, ejabberd_s2s_in, [
{shaper, s2s_shaper},
{max_stanza_size, 131072}
]},

{5280, ejabberd_http, [
http_poll,
http_bind,
web_admin
]}

]}.

....

Here is the code:

echobot.py:

from twisted.words.xish import domish
from wokkel.xmppim import MessageProtocol, AvailablePresence

class EchoBotProtocol(MessageProtocol):
def connectionMade(self):
print "Connected!"

# send initial presence
self.send(AvailablePresence())

def connectionLost(self, reason):
print "Disconnected!"

def onMessage(self, msg):
print str(msg)

if msg["type"] == 'chat' and hasattr(msg, "body"):
reply = domish.Element((None, "message"))
reply["to"] = msg["from"]
reply["from"] = msg["to"]
reply["type"] = 'chat'
reply.addElement("body", content="echo: " + str(msg.body))

self.send(reply)

echobot.tac file:

from twisted.application import service
from twisted.words.protocols.jabber import jid
from wokkel.client import XMPPClient

from echobot import EchoBotProtocol

application = service.Application("echobot")

xmppclient = XMPPClient(jid.internJID("someuser@localhost/echobot"), password="ssshh", host='localhost', port=8888)
xmppclient.logTraffic = False
echobot = EchoBotProtocol()
echobot.setHandlerParent(xmppclient)
xmppclient.setServiceParent(application)

Here is the client code
from twisted.words.protocols.jabber import client, jid
from twisted.words.xish import domish
from twisted.internet import reactor

class JabberClient:
xmlstream = None

def __init__(self, myJid):
self.myJid = myJid

def authd(self,xmlstream):
print "authenticated"
self.xmlstream = xmlstream
presence = domish.Element(('jabber:client','presence'))
xmlstream.send(presence)

xmlstream.addObserver('/message', self.debug)
xmlstream.addObserver('/presence', self.debug)
xmlstream.addObserver('/iq', self.debug)
reactor.callLater(5, self.sendMessage, 'test@server.jabber','test','test')

def sendMessage(self, to, subject, body):

message = domish.Element(('jabber:client','message'))
message['to'] = to
message['type'] = 'chat'

# message.addElement('subject',None,subject)
message.addElement('body',None,body)

self.xmlstream.send(message)

def debug(self, elem):
#print elem.toXml().encode('utf-8')
#print "="*20
pass

myJid = jid.JID('username@localhost/twisted_words')
factory = client.basicClientFactory(myJid, 'ssshh')

jabber = JabberClient(myJid)

factory.addBootstrap('//event/stream/authd',jabber.authd)
factory.addBootstrap("//event/client/basicauth/invaliduser", jabber.debug)
factory.addBootstrap("//event/client/basicauth/authfailed", jabber.debug)
factory.addBootstrap("//event/stream/error", jabber.debug)

reactor.connectTCP('localhost',8888,factory)
reactor.run()

Sorry for the long email. Please advise on how to fix this.

Thank you for your time.

Regards,

VT wrote: {hosts,

VT wrote:

{hosts, ["localhost"]}.

{listen, [
{8888, ejabberd_service, [
{access, all},
{hosts, ["localhost"],
[{password, "ssshh"}]
}
]},
...
]}.

=INFO REPORT==== 2010-12-09 03:01:52 ===
I(<0.7551.0>:ejabberd_service:208) : Route registered for service "localhost"

Bad idea.

If your XMPP server is called "localhost", then the users are called "vt@localhost". And the services must have names like "service1.localhost".

You can't configure a service with the same host name that the XMPP server!

Syndicate content