PYTHON SDK
v2.0.3
message.py
이 파일의 문서화 페이지로 가기
1 # vi:set sw=4 ts=4 expandtab:
2 # -*- coding: utf8 -*-
3 
4 import sys
5 import platform
6 import base64
7 sys.path.insert(0, "../../")
8 
9 from sdk.coolsms import Coolsms
10 from sdk.exceptions import CoolsmsException
11 from sdk.exceptions import CoolsmsSDKException
12 from sdk.exceptions import CoolsmsSystemException
13 from sdk.exceptions import CoolsmsServerException
14 
15 ## @class Message
16 # @brief management message, using Rest API
17 class Message:
18  # Coolsms Object
19  cool = None
20 
21  ## @brief initialize
22  # @param string api_key [required]
23  # @param string api_secret [required]
24  # @param boolean use_http_connection [optional]
25  def __init__(self, api_key, api_secret, use_http_connection = False):
26  self.cool = Coolsms(api_key, api_secret)
27 
28  # if True. use http connection
29  if use_http_connection == True:
30  self.cool.use_http_connection()
31 
32  ## @brief send messages ( HTTP Method POST )
33  # @param dictionary params {
34  # @param string to [required]
35  # @param string from [required]
36  # @param string text [required]
37  # @param string type [optional] [default:"sms"]
38  # @param mixed image [optional]
39  # @param string image_encoding [optional]
40  # @param string refname [optional]
41  # @param mixed country [optional] [default:"82"]
42  # @param string datetime [optional]
43  # @param string subject [optional]
44  # @param string charset [optional] [default:"utf8"]
45  # @param string srk [optional]
46  # @param string mode [optional]
47  # @param string extension [optional]
48  # @param integer delay [optional] [default:"0"]
49  # @param boolean force_sms [optional]
50  # @param string app_version [optional]
51  # @param string template_code [optional]
52  # @param string sender_key [optional]
53  # @param string only_ata [optional] [default:"false"] }
54  # @return JSONObject
55  # @throws CoolsmsException
56  def send(self, params):
57  # params type check
58  if type(params) is not dict:
59  raise CoolsmsSDKException("parameter type is not dictionary", 201)
60 
61  params = self.cool.check_send_data(params)
62 
63  # system info
64  params['os_platform'] = platform.system()
65  params['dev_lang'] = "Python %s" % platform.python_version()
66  params['sdk_version'] = "sms-python %s" % Coolsms.sdk_version
67 
68  # type이 mms일때 image file check
69  files = {}
70  if 'type' in params and params['type'] == 'mms':
71  if 'image' not in params:
72  raise CoolsmsSDKException('image file is required', 201)
73 
74  try:
75  with open(params['image'], 'rb') as content_file:
76  content = base64.b64encode(content_file.read())
77  content = content.decode()
78  except Exception as e:
79  raise CoolsmsSystemException(e, 399)
80  files = {'image': {'filename': params['image'], 'content': content}}
81  params['image_encoding'] = 'base64'
82 
83  # request post multipart-form
84  response = self.cool.request_post_multipart("send", params, files)
85  return response
86 
87  ## @brief get status ( HTTP Method GET )
88  # @param dictionary params {
89  # @param integer count [optional] [default:"1"]
90  # @param string unit [optional]
91  # @param string date [optional] [default:현재시각]
92  # @param integer channel [optional] [default:"1"] }
93  # @return JSONObject
94  # @throws CoolsmsException
95  def status(self, params=None):
96  response = self.cool.request_get('status', params)
97  return response
98 
99  ## @brief sent messages ( HTTP Method GET )
100  # @param dictionary params {
101  # @param integer offset [optional]
102  # @param integer limit [optional] [default:"20"]
103  # @param string rcpt [optional]
104  # @param string start [optional]
105  # @param string end [optional]
106  # @param string status [optional]
107  # @param string status [optional]
108  # @param string resultcode [optional]
109  # @param string message_id [optional]
110  # @param string group_id [optional] }
111  # @return JSONObject
112  # @throws CoolsmsException
113  def sent(self, params=None):
114  response = self.cool.request_get('sent', params)
115  return response
116 
117  ## @brief get remaining balance ( HTTP Method GET )
118  # @param None
119  # @return JSONobject
120  # @throws CoolsmsException
121  def balance(self):
122  response = self.cool.request_get('balance')
123  return response
124 
125  ## @brief cancel reserve message. mid or gid either one must be entered. ( HTTP Method POST )
126  # @param dictionary params {
127  # @param string mid [optional]
128  # @param string gid [optional] }
129  # @return None
130  # @throws CoolsmsException
131  def cancel(self, params):
132  if 'message_id' not in params and 'group_id' not in params:
133  raise CoolsmsSDKException("message_id or group_id either one must be entered", 201)
134 
135  response = self.cool.request_post('cancel', params)
136  return response
def sent(self, params=None)
sent messages ( HTTP Method GET )
Definition: message.py:113
def cancel(self, params)
cancel reserve message.
Definition: message.py:131
def balance(self)
get remaining balance ( HTTP Method GET )
Definition: message.py:121
management message, using Rest API
Definition: message.py:17
Gateway access url : https://api.coolsms.co.kr/{api type}/{verson}/{resource name}.
Definition: coolsms.py:44
def status(self, params=None)
get status ( HTTP Method GET )
Definition: message.py:95
def __init__(self, api_key, api_secret, use_http_connection=False)
initialize
Definition: message.py:25
def send(self, params)
send messages ( HTTP Method POST )
Definition: message.py:56