def send(self, params): # params type check if type(params) is not dict: raise CoolsmsSDKException("parameter type is not dictionary", 201) params = self.cool.check_send_data(params) # system info params['os_platform'] = platform.system() params['dev_lang'] = "Python %s" % platform.python_version() params['sdk_version'] = "sms-python %s" % Coolsms.sdk_version # type이 mms일때 image file check files = {} if 'type' in params and params['type'] == 'mms': if 'image' not in params: raise CoolsmsSDKException('image file is required', 201) try: with open(params['image'], 'rb') as content_file: content = base64.b64encode(content_file.read()) content = content.decode() except Exception as e: raise CoolsmsSystemException(e, 399) files = { 'image': { 'filename': params['image'], 'content': content } } params['image_encoding'] = 'base64' # request post multipart-form response = self.cool.request_post_multipart("send", params, files) return response
def check_send_data(self, params): # require fields check if all(k in params for k in ("to", "from", "text")) == False: raise CoolsmsSDKException( "parameter 'to', 'from', 'text' are required", 201) for key, val in params.items(): # ptyhon 2 version 에서 unicode 문제 해결 if key == "text" and sys.version_info[0] == 2: text = val t_temp = text.decode('utf-8') text = t_temp.encode('utf-8') text = unicode(text, encoding='utf-8') params['text'] = text # convert list to a comma seperated string if key == "to" and val == list: params['to'] = ','.join(to) # message type check if key == "type" and val.lower() not in [ 'sms', 'lms', 'mms', 'ata', 'cta' ]: raise CoolsmsSDKException("message type is not supported", 201) return params
def add_messages_json(self, group_id, messages): # require fields check if group_id == None or messages == None: raise CoolsmsSDKException( "parameter 'group_id', 'messages' are required", 201) messages = json.loads(messages) for data in messages: data = self.cool.check_send_data(data) # messages setting params = dict() params['messages'] = json.dumps(messages) # system info params['os_platform'] = platform.system() params['dev_lang'] = "Python %s" % platform.python_version() params['sdk_version'] = "sms-python %s" % Coolsms.sdk_version # request post resource = "groups/%s/add_messages.json" % group_id response = self.cool.request_post(resource, params) return response
def cancel(self, params): if 'message_id' not in params and 'group_id' not in params: raise CoolsmsSDKException( "message_id or group_id either one must be entered", 201) response = self.cool.request_post('cancel', params) return response
def get_group_info(self, group_id): if group_id == None: raise CoolsmsSDKException("parameter 'group_id' is required", 201) resource = "groups/%s" % group_id response = self.cool.request_get(resource) return response
def delete_groups(self, group_ids): if group_ids == None: raise CoolsmsSDKException("parameter 'group_ids' is required", 201) params = {'group_ids': group_ids} response = self.cool.request_post('delete_groups', params) return response
def get_image_info(self, image_id): if image_id == None: raise CoolsmsSDKException("'image_id' is required", 201); resource = "images/" + image_id; response = self.cool.request_get(resource) return response
def delete(self, handle_key): if handle_key == None: raise CoolsmsSDKException("'handle_key' is required", 201); params = dict() params = {'handle_key':handle_key} response = self.cool.request_post('delete', params) return response
def delete_images(self, image_ids): if image_ids == None: raise CoolsmsSDKException("'image_ids' is required", 201); params = dict() params = {'image_ids':image_ids} response = self.cool.request_post('delete_images', params) return response
def send(self, group_id): # require filed check if group_id == None: raise CoolsmsSDKException("parameter 'group_id' is required", 201) # request post resource = "groups/%s/send" % group_id response = self.cool.request_post(resource) return response
def get_message_list(self, params): # require fields check if "group_id" not in params: raise CoolsmsSDKException("parameter 'group_id' is required", 201) # request post resource = "groups/%s/message_list" % params['group_id'] response = self.cool.request_get(resource, params) return response
def add_messages(self, params): # params type check if type(params) is not dict: raise CoolsmsSDKException("parameter type is not dictionary", 201) # require fields check if "group_id" not in params: raise CoolsmsSDKException("parameter 'group_id' is required", 201) params = self.cool.check_send_data(params) # system info params['os_platform'] = platform.system() params['dev_lang'] = "Python %s" % platform.python_version() params['sdk_version'] = "sms-python %s" % Coolsms.sdk_version # request post resource = "groups/%s/add_messages" % params['group_id'] response = self.cool.request_post(resource, params) return response
def register(self, phone, site_user=None): if phone == None: raise CoolsmsSDKException("'phone' is required", 201); params = dict() params = {'phone':phone} if site_user: params['site_user'] = site_user response = self.cool.request_post('register', params) return response
def set_default(self, handle_key, site_user=None): if handle_key == None: raise CoolsmsSDKException("'handle_key' is required", 201); params = dict() params = {'handle_key':handle_key} if site_user: params['site_user'] = site_user response = self.cool.request_post('set_default', params) return response
def delete_messages(self, group_id, message_ids): # require fields check if group_id == None or message_ids == None: raise CoolsmsSDKException("parameter 'group_id', 'message_ids' are required", 201) params = dict() params['message_ids'] = message_ids # request post resource = "groups/%s/delete_messages" % group_id response = self.cool.request_post(resource, params) return response
def upload_image(self, image): if image == None: raise CoolsmsSDKException("'image' is required", 201); params = dict() params = {'image':image} params['image_encoding'] = 'base64' files = {} try: with open(image, 'rb') as content_file: content = base64.b64encode(content_file.read()) content = content.decode() except Exception as e: raise CoolsmsSystemException(e, 399) files = {'image': {'filename': image, 'content': content}} response = self.cool.request_post_multipart("upload_image", params, files) return response