def __init__(self, configfile='team.cfg'): self.configuration = TeamConfiguration(configfile) self.Jid, self.Password = self.configuration.getBotId() sleekxmpp.ClientXMPP.__init__(self, self.Jid, self.Password) self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0199') # XMPP Ping self.logger = StatsLog() self.staffstate = StaffState() # The session_start event will be triggered when # the bot establishes its connection with the server # and the XML streams are ready for use. We want to # listen for this event so that we we can initialize # our roster. self.add_event_handler('connected', self.connected) self.add_event_handler('disconnected', self.disconnected) self.add_event_handler('presence_available', self.available) self.add_event_handler('presence_unavailable', self.unavailable) self.add_event_handler("session_start", self.sessionStart, threaded=True) #self.add_event_handler("message", self.messageCB()) self.add_event_handler("message", self.message)
class TeamBot(sleekxmpp.ClientXMPP): #GtalkServer = '74.125.142.125' ''' use DepInj so we can easy test ''' def __init__(self, configfile='team.cfg'): self.configuration = TeamConfiguration(configfile) self.Jid, self.Password = self.configuration.getBotId() sleekxmpp.ClientXMPP.__init__(self, self.Jid, self.Password) self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0199') # XMPP Ping self.logger = StatsLog() self.staffstate = StaffState() # The session_start event will be triggered when # the bot establishes its connection with the server # and the XML streams are ready for use. We want to # listen for this event so that we we can initialize # our roster. self.add_event_handler('connected', self.connected) self.add_event_handler('disconnected', self.disconnected) self.add_event_handler('presence_available', self.available) self.add_event_handler('presence_unavailable', self.unavailable) self.add_event_handler("session_start", self.sessionStart, threaded=True) #self.add_event_handler("message", self.messageCB()) self.add_event_handler("message", self.message) def connected(self, event=None): self.logger.logConsole('connected...') def disconnected(self, event=None): self.logger.logConsole('disconnected...re-connected GTalk server') self.connectToGtalk() def available(self, presence): pto = presence['to'].bare pfrom = presence['from'].bare self.logger.logConsole('presence available : ' + pto + ' from: ' + pfrom) def unavailable(self, presence): pto = presence['to'].bare pfrom = presence['from'].bare self.logger.logConsole('presence unavailable : ' + pto + ' from: ' + pfrom) """ connect to GTalk thru tls http proxying wwwgate0.mot """ def connectToGtalk(self): gtalk = self.configuration.getGtalkServer() self.use_proxy = self.configuration.useProxy() if self.use_proxy: print 'use proxy', self.use_proxy #raise AssertionError, 'use proxy wrong' h,p,u,pwd = self.configuration.getProxyInfo() self.proxy_config = { 'host': h, 'port': int(p), 'username': u, 'password': pwd} if self.connect((gtalk, 5222), True, True, False): self.process(block=True) print("Done") else: print("Unable to connect.") """ the main entry point of bot logic """ def sessionStart(self, event): """ Process the session_start event. Typical actions for the session_start event are requesting the roster and broadcasting an initial presence stanza. """ self.send_presence() self.get_roster() day = self.configuration.getDayOfWeek() hour = self.configuration.getHour() minute = self.configuration.getMinute() # add schedule cron job sched.add_cron_job(self.collectStats, day_of_week=day, hour=hour, minute=minute) # Using wait=True ensures that the send queue will be # emptied before ending the session. #self.disconnect(wait=True) self.collectStats() #@sched.cron_schedule(day_of_week='mon-sat', hour='10-17', minute=12) def collectStats(self): memlist = self.configuration.getTeamMember() for name,jid in memlist: self.staffstate.updateQuestionState(name, '1') # first question self.sendMsg(jid, self.configuration.getQuestion('1')) # send msg to jid def sendMsg(self, toid, msg): self.logger.logConsole('sending msg to : ' + toid + ' : ' + msg) self.send_message(mto=toid, mbody=msg, mtype='chat') def message(self, msg): """ Process incoming message stanzas. Be aware that this also includes MUC messages and error messages. It is usually a good idea to check the messages's type before processing or sending replies. Arguments: msg -- The received message stanza. See the documentation for stanza objects and the Message stanza to see how it may be used. """ if msg['type'] in ('chat', 'normal'): from_id = str(msg['from']).split('@')[0] to_id = msg['to'] body = msg['body'] print 'message : ', from_id, ' -> ', to_id, ' :: ', body self.logger.logFile(from_id, body) #msg.reply("Thanks for sending status!\n%(body)s" % msg).send() nextqidx, nextq = self.getNextQuestion(from_id) if nextq is not None: self.staffstate.updateQuestionState(from_id, nextqidx) # first question msg.reply("Thanks!\n%s" % nextq).send() def messageCB(self): def cb(msg): if msg['type'] in ('chat', 'normal'): from_id = str(msg['from']).split('@')[0] # need to convert to string to_id = msg['to'] body = msg['body'] print 'cb log msg: ', from_id, ' -> ', to_id, ' :: ', body self.logger.logFile(from_id, body) msg.reply("Thanks for sending status!\n%(body)s" % msg).send() return cb def getNextQuestion(self, name): qidx = int(self.staffstate.getQuestionState(name)) return str(qidx+1), self.configuration.getQuestion(str(qidx+1))