XMPP Echobot


This is a simple automatic forwarding bot basing on python-jabberbot. Each message sent to this bot will be forwarded to its buddies. Although XMPP supports conferencing, I think this bot’s mode provides an easier approach to extend any peer-to-peer encrypted messaging into reliable conferences. This bot’s code is modified from an example code of python-jabberbot. The following is my code’s explanation.

# -*- coding: utf-8 -*-

from jabberbot import JabberBot
import threading,time,logging,xmpp

# Fill in the JID + Password of your JabberBot here...
(JID, PASSWORD) = ('【用户名】','【密码】')

class BroadcastingJabberBot(JabberBot):

    def __init__( self, jid, password, res = None):
        super( BroadcastingJabberBot, self).__init__( jid, password, res)

        self.message_queue = []
        self.thread_killed = False


The class inherits from Jabberbot. Initialize to clear up message queue and others.

    def callback_message(self, conn, mess):
        type = mess.getType()
        jid = mess.getFrom()
        props = mess.getProperties()
        text = mess.getBody()
        username = self.get_sender_username(mess)

        if type not in ("groupchat", "chat"):

        # Ignore messages from before we joined
        if xmpp.NS_DELAY in props:

        # Ignore messages from myself
        if self.jid.bareMatch(jid):

        if jid == None:

        sender = jid.getStripped()
        if sender == None:

        self.message_queue.append(u'\n%s 说:\n%s' % (str(sender),text))

上面重写了 callback_message 方法,注意到里面一些代码用于识别聊天信息、过滤空内容。之后将消息送入消息队列中。

The above rewrites callback_message. Notice some code is used to identify chatting messages and filter out nulls. Then message is queued.

    def idle_proc( self):
        if not len(self.message_queue):

        # copy the message queue, then empty it
        messages = self.message_queue
        self.message_queue = []

        for message in messages:
            for user in self.roster.getItems():#self.users:
                self.send( user, message)


Send message when idle.

    def thread_proc( self):
#        return
        while not self.thread_killed:
            self.message_queue.append('this is an automatic message, sent all 60 seconds :)')
            for i in range(60):
                time.sleep( 1)
                if self.thread_killed:


Timed 60s to send a regular message, and handle exit events. Seems XMPP protocol requires user acting within given period to avoid being kicked offline. That’s the purpose.

bc = BroadcastingJabberBot( JID, PASSWORD)

th = threading.Thread( target = bc.thread_proc)
bc.serve_forever( connect_callback = lambda: th.start())
bc.thread_killed = True


Fire the program finally.