예제 #1
0
class GroupMessage:
    # Coolsms Object
    cool = None

    ## @brief initialize
    #  @param string api_key [required]
    #  @param string api_secret [required]
    #  @param boolean use_http_connection [optional]
    #  @throws CoolsmsException
    def __init__(self, api_key, api_secret, use_http_connection=False):
        self.cool = Coolsms(api_key, api_secret)

        # if True. use http connection
        if use_http_connection == True:
            self.cool.use_http_connection()

    ## @brief create create group ( HTTP Method GET )
    #  @param dictionary params {
    #  @param string charset [optional] [default:"utf8"]
    #  @param string srk [optional]
    #  @param string mode [optional]
    #  @param string delay [optional] [default:"0"]
    #  @param boolean force_sms [optional] [default:"false"]
    #  @param string os_platform [optional]
    #  @param string dev_lang [optional]
    #  @param string sdk_version [optional]
    #  @param string app_version [optional]
    #  @param string site_user [optional] [default:"__private__"]
    #  @param string only_ata [optional] [default:"false"] }
    #  @return JSONObject
    #  @throws CoolsmsException
    def create_group(self, params=None):
        response = self.cool.request_get('new_group', params)

        return response

    ## @brief get group list ( HTTP Method GET )
    #  @param None
    #  @return JSONArray
    #  @throws CoolsmsException
    def get_group_list(self):
        response = self.cool.request_get('group_list')

        return response

    ## @brief delete groups ( HTTP Method POST )
    #  @param string group_ids [required]
    #  @return JSONobject
    #  @throws CoolsmsException
    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

    ## @brief get group info ( HTTP Method GET )
    #  @param string group_id [required]
    #  @return JSONObject
    #  @throws CoolsmsException
    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

    ## @brief add message to group ( HTTP Method POST )
    #  @param dictionary params {
    #  @param string group_id [required]
    #  @param string to [required]
    #  @param string from [required]
    #  @param string text [required]
    #  @param string type [required] [default:"sms"]
    #  @param string image_id [optional]
    #  @param string refname [optional]
    #  @param string country [optional] [default:"82"]
    #  @param string datetime [optional]
    #  @param string subject [optional]
    #  @param integer delay [optional] [default:"0"]
    #  @param string template_code [optional]
    #  @param string sender_key [optional] }
    #  @return JSONObject
    #  @throws CoolsmsException
    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

    ## @brief add json type message to group ( HTTP Method POST )
    #  @param string group_id [required]
    #  @param JSONArray messages [required] [{
    #  @param string group_id [required]
    #  @param string to [required]
    #  @param string from [required]
    #  @param string text [required]
    #  @param string type [required] [default:"sms"]
    #  @param string image_id [optional]
    #  @param string refname [optional]
    #  @param string country [optional] [default:"82"]
    #  @param string datetime [optional]
    #  @param string subject [optional]
    #  @param integer delay [optional] [default:"0"]
    #  @param string template_code [optional]
    #  @param string sender_key [optional] }]
    #  @return JSONObject
    #  @throws CoolsmsException
    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

    ## @brief get message list ( HTTP Method GET )
    #  @param dictionary params {
    #  @param string group_id [required]
    #  @param integer offset [optional] [default:"0"]
    #  @param integer limit [optional] [default:"20"] }
    #  @return JSONObject
    #  @throws CoolsmsException
    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

    ## @brief delete message from group ( HTTP Method POST )
    #  @param string group_id [required]
    #  @param string message_ids [required]
    #  @return JSONObject
    #  @throws CoolsmsException
    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

    ## @brief send group message ( HTTP Method POST )
    #  @param string group_id [required]
    #  @return JSONObject
    #  @throws CoolsmsException
    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
예제 #2
0
class Message:
    # Coolsms Object
    cool = None

    ## @brief initialize
    #  @param string api_key [required]
    #  @param string api_secret [required]
    #  @param boolean use_http_connection [optional]
    def __init__(self, api_key, api_secret, use_http_connection=False):
        self.cool = Coolsms(api_key, api_secret)

        # if True. use http connection
        if use_http_connection == True:
            self.cool.use_http_connection()

    ## @brief send messages ( HTTP Method POST )
    #  @param dictionary params {
    #  @param string to [required]
    #  @param string from [required]
    #  @param string text [required]
    #  @param string type [optional] [default:"sms"]
    #  @param mixed image [optional]
    #  @param string image_encoding [optional]
    #  @param string refname [optional]
    #  @param mixed country [optional] [default:"82"]
    #  @param string datetime [optional]
    #  @param string subject [optional]
    #  @param string charset [optional] [default:"utf8"]
    #  @param string srk [optional]
    #  @param string mode [optional]
    #  @param string extension [optional]
    #  @param integer delay [optional] [default:"0"]
    #  @param boolean force_sms [optional]
    #  @param string app_version [optional]
    #  @param string template_code [optional]
    #  @param string sender_key [optional]
    #  @param string only_ata [optional] [default:"false"] }
    #  @return JSONObject
    #  @throws CoolsmsException
    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

    ## @brief get status ( HTTP Method GET )
    #  @param dictionary params {
    #  @param integer count [optional] [default:"1"]
    #  @param string unit [optional]
    #  @param string date [optional] [default:현재시각]
    #  @param integer channel [optional] [default:"1"] }
    #  @return JSONObject
    #  @throws CoolsmsException
    def status(self, params=None):
        response = self.cool.request_get('status', params)
        return response

    ## @brief sent messages ( HTTP Method GET )
    #  @param dictionary params {
    #  @param integer offset [optional]
    #  @param integer limit [optional] [default:"20"]
    #  @param string rcpt [optional]
    #  @param string start [optional]
    #  @param string end [optional]
    #  @param string status [optional]
    #  @param string status [optional]
    #  @param string resultcode [optional]
    #  @param string message_id [optional]
    #  @param string group_id [optional] }
    #  @return JSONObject
    #  @throws CoolsmsException
    def sent(self, params=None):
        response = self.cool.request_get('sent', params)
        return response

    ## @brief get remaining balance ( HTTP Method GET )
    #  @param None
    #  @return JSONobject
    #  @throws CoolsmsException
    def balance(self):
        response = self.cool.request_get('balance')
        return response

    ## @brief cancel reserve message. mid or gid either one must be entered. ( HTTP Method POST )
    #  @param dictionary params {
    #  @param string mid [optional]
    #  @param string gid [optional] }
    #  @return None
    #  @throws CoolsmsException
    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