示例#1
0
文件: BotApi.py 项目: junoteam/pybot
    def get_command(self):
        path_to_file = os.getcwd()
        file_with_id = path_to_file + "/update_id_file.txt"

        with open(file_with_id, 'r') as myfile:
            last_id = myfile.read()
            myfile.close()

        offset = int(last_id)
        offset = int(offset) - 1

        values = {}
        values['offset'] = offset
        values['limit'] = '5'

        data = urllib.urlencode(values)
        response = urllib2.Request(self.update_url, data)
        response = urllib2.urlopen(response)
        block_from_json_response = response.read()

        self.json_response = json.loads(block_from_json_response)
        last_update_id_from_json = self.json_response['result'][-1]['update_id']
        last_user_id = self.json_response['result'][-1]['message']['from']['id']

        update_id_array = []
        for key in self.json_response['result']:
            update_id_array.append(key['update_id'])

        path_to_file = os.getcwd()
        file_with_id = path_to_file + "/update_id_file.txt"

        if os.path.exists(file_with_id):
            with open(file_with_id, 'r+') as myfile:
                last_update_id = myfile.read()

                if int(last_update_id) == int(last_update_id_from_json):
                    myfile.close()
                else:
                    myfile.read()
                    myfile.seek(0)
                    myfile.write(str(last_update_id_from_json))
                    myfile.close()

                    command = self.json_response['result'][-1]['message']['text']
                    system = SystemUtils()

                    if last_user_id == self.user_id:
                        if command == BotApi.info_command:
                            inf = system.get_info_from_server(command)
                            information = inf.to_string()
                            self.send_message(command, information)
                        elif command == BotApi.help_command:
                            self.send_message(command, '')
                        elif command == BotApi.load_command:
                            load = system.get_load_from_from_server(command)
                            self.send_message(command, load)
                        elif command == BotApi.mem_commmand:
                            mem = system.get_memory(command)
                            self.send_message(command, mem)
                        elif command == BotApi.apache_restart:
                            restart = system.restart_apache(command)
                            self.send_message(command, restart)
                        elif command == BotApi.mysql_restart:
                            restart = system.restart_mysqld(command)
                            self.send_message(command, restart)
                        elif command == BotApi.check_site_status:
                            online_status = system.get_status_online(command)
                            self.send_message(command, online_status)
                        else:
                            self.send_message(command, '')
                            print Bcolors.OKGREEN + "Nothing to send, error command has been entered" + Bcolors.ENDC
                    else:
                        self.detect_wrong_user()
        else:
            print "File: " + file_with_id + " doesn't exist"