class SupportAgent(Entity): ready = True sop = None currentIssue = None issues = property(lambda self: ( i for i in Issue.objects if not i.resolved and i.assignedTo == self)) online = property(lambda self: self.conversation.online) available = property( lambda self: self.online and self.ready and self.currentIssue is None and not self.conversation.askingQuestion) busy = property(lambda self: not self.available) teams = property(lambda self: (team for team in Team.objects if self in team.agents)) name = property(lambda self: self.conversation.buddyName) responsive = property(lambda self: self.conversation.responsive) def __init__(self, jid): self.jid = jid self.conversation = Conversation(jid) def __getstate__(self): return {'jid': self.jid, 'ready': self.ready} @staticmethod def construct(state): agent = SupportAgent(state['jid']) agent.ready = state['ready'] return agent def tell(self, message, **kwargs): self.conversation.say(message, **kwargs) def ask(self, question, answers, **kwargs): return self.conversation.ask(question, answers, **kwargs) def engage(self, issue): assert self.available self.currentIssue = issue issue.assignedTo = self issue.whenResolved(self.issueResolved) def issueResolved(self, issue): if issue != self.currentIssue: #got resolved by someone else return log("Agent %s resolved issue \"%s\" resolution=%s" % (self.name, issue.description, issue.resolution), type='issues') self.currentIssue = None self.sop = None if self.available and any(self.issues): self.engage(self.issues.next()) return issue
class SupportAgent(Entity): ready = True sop = None currentIssue = None issues = property(lambda self: (i for i in Issue.objects if not i.resolved and i.assignedTo == self)) online = property(lambda self: self.conversation.online) available = property(lambda self: self.online and self.ready and self.currentIssue is None and not self.conversation.askingQuestion) busy = property(lambda self: not self.available) teams = property(lambda self: (team for team in Team.objects if self in team.agents)) name = property(lambda self: self.conversation.buddyName) responsive = property(lambda self: self.conversation.responsive) def __init__(self, jid): self.jid = jid self.conversation = Conversation(jid) def __getstate__(self): return { 'jid' : self.jid, 'ready' : self.ready } @staticmethod def construct(state): agent = SupportAgent( state['jid'] ) agent.ready = state['ready'] return agent def tell(self, message, **kwargs): self.conversation.say(message, **kwargs) def ask(self, question, answers, **kwargs): return self.conversation.ask(question, answers, **kwargs) def engage(self, issue): assert self.available self.currentIssue = issue issue.assignedTo = self issue.whenResolved(self.issueResolved) def issueResolved(self, issue): if issue != self.currentIssue: #got resolved by someone else return log("Agent %s resolved issue \"%s\" resolution=%s" % (self.name, issue.description, issue.resolution), type='issues') self.currentIssue = None self.sop = None if self.available and any(self.issues): self.engage( self.issues.next() ) return issue
def receivedMessage(self, e): # Extract the body of the message try: message = str([c for c in e.children if c.name == 'body'][0]) except: log('discarding invalid message (has no body!): %s' % e.toXml()) return # Discard delayed messages delays = [x for x in e.children if x.name == 'delay'] stamps = [ x for x in e.children \ if x.name == 'x' and \ x.compareAttribute('xmlns','jabber:x:delay') and \ x.hasAttribute('stamp') ] #stampstring = str( stamps[0].getAttribute('stamp') ) #timestamp = time.mktime( time.strptime(stampstring, "%Y%m%dT%H:%M:%S") ) if delays or stamps: log('discarding delayed message: %s' % e.toXml()) return # Route message to the right Conversation or ChatRoom entity if e.getAttribute('type') == 'chat': buddy = str(e['from'].split('/')[0]) if not Conversation.exists(buddy): self.requestAuthorization(buddy) log('received message from %s: %s' % (buddy.split('@')[0], message)) Conversation(buddy).hear(message) elif e.getAttribute('type') == 'groupchat': room = e['from'].split('@')[0] log('received message [chatroom=%s]: %s' % (room, message)) ChatRoom(room).hear(message) else: log('received message of unknown type: %s' % e.toXml(), error=True)
def joinEnvironmentalChatRoom(event): """determine if we should join a chatroom""" chat = ChatRoom(config.ROMEO_ENV_NAME) #make sure the drone can be managed by the room username = config.ROMEO_ENV_NAME jbserver = jconfig.JABBER_CHAT_SERVICE jid = "%(username)s@%(jbserver)s" % locals() Team('support').addMember(jid) #get the conversation context set to some sane defaults conversation = Conversation(jid) #grant the room access to the server if jconfig.JABBER_TRUST_ROOM: conversation.grantAuthorization(notify=False) #be vain assume all conversations revolve around ourself context = { 'server': Server(config.HOSTNAME), 'subject': Server(config.HOSTNAME), } conversation.context.update(context) #finally join the room chat.join()
def auth_request(conversation): result = None try: if conversation.authorized: conversation.say("You are already authorized to use this droned.") raise AssertionError('Already authorized to use this droned') conversation.say("Ok, I will pass your request along to the environment administrator. I will let you know what they decide.") deputy = Conversation(jabber_config.DEPUTY) try: question = "%s has requested authorization, do you wish to <b>grant</b> or <b>deny</b> this request?" % conversation.buddy answers = ('grant','deny') d = deputy.ask(question, answers) wfd = defer.waitForDeferred(d) yield wfd answer = wfd.getResult() except AlreadyAskingQuestion: err = "Sorry, the environment administrator is busy dealing with something else right now. " err += "You should wait and try again later, or if it is urgent contact them directly. " err += "The environment administrator is %s" % jabber_config.DEPUTY conversation.say(err) raise AssertionError('Administrator is busy') if answer == 'grant': conversation.grantAuthorization() deputy.say("%s has been granted authorization." % conversation.buddy) else: err = "Your request for authorization has been denied. If you wish to discuss this further you " err += "should contact the environment administrator directly. " err += "The environment administrator is %s" % jabber_config.DEPUTY conversation.say(err) deputy.say("%s has been denied authorization." % conversation.buddy) except AssertionError: pass except: result = Failure() yield result
def __init__(self, jid): self.jid = jid self.conversation = Conversation(jid)