예제 #1
0
class RtmBot(object):
    def __init__(self, apiKey, apiSecret, adminJidHash, storage):
        self.adminJidHash = adminJidHash
        self.api = RtmApi(apiKey, apiSecret)
        self.__storage = storage
        
    def __fromStorage(self, name, default = None):
        if (self.__storage.exist(name)):
            return self.__storage.get(name)
        else:
            return default
    
    def processCommand(self, message):
        try:
            message = message.strip();
            
            if self.__fromStorage("frob") != None:
                # user has authenticated
                # obtain auth token
                
                frob = self.__fromStorage("frob")
                if frob != None:
                    try:
                        o = self.api.getToken(frob)
                        self.__storage.set("auth", o["token"])
                        self.__storage.delete("frob")
                        
                        return "Authenticated!\n\n" + self.getHelpMessage()
                    except RtmApiException, e:
                        self.__storage.delete("frob")
                        logging.exception(str(e))
                        return "Something went wrong. Please try to say something more"
                else:
                    return "No frob found. Please try to say something more"
            elif self.__fromStorage("auth") == None:
                # authentication
                
                frob = self.api.getFrob()
                self.__storage.set("frob", frob)
                return "Jabber2RTM\n\nFirst you need to grant access to RTM to me.\nPlease follow the URL: " + self.api.getDesktopAuthUrl(RtmApi.PERMS_DELETE, frob) + "\n\nThen say 'hey' to me"
            else:
예제 #2
0
 def __init__(self, apiKey, apiSecret, adminJidHash, storage):
     self.adminJidHash = adminJidHash
     self.api = RtmApi(apiKey, apiSecret)
     self.__storage = storage