Beispiel #1
0
    def _pull(self, sms_type, max_num):
        """Pull SMS message status.

        :param msg_type: SMS message type, Enum{0: normal SMS, 1: marketing SMS}
        :param max_num: maximum number of message status
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(
            self._url, self._appid, rand)
        req = HTTPRequest(
            url=url,
            method="POST",
            headers={"Content-Type": "application/json"},
            body={
                "sig": util.calculate_signature(
                    self._appkey, rand, now),
                "time": now,
                "type": sms_type,
                "max": max_num
            },
            connect_timeout=60,
            request_timeout=60
        )
        return util.api_request(req)
Beispiel #2
0
    def _pull(self, sms_type, max_num, url=None):
        """Pull SMS message status.

        :param msg_type: SMS message type, Enum{0: normal SMS, 1: marketing SMS}
        :param max_num: maximum number of message status
        :param url: custom url
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(url if self._url else url,
                                                self._appid, rand)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={"Content-Type": "application/json"},
                          body=json.dumps({
                              "sig":
                              util.calculate_signature(self._appkey, rand,
                                                       now),
                              "time":
                              now,
                              "type":
                              sms_type,
                              "max":
                              max_num
                          }))
        return util.api_request(req, self._httpclient)
Beispiel #3
0
    def _pull(self, msg_type, nation_code, mobile, begin_time, end_time, max_num):
        """Pull SMS messages status for single mobile.

        :param msg_type: SMS message type, Enum{0: normal SMS, 1: marketing SMS}
        :param nation_code: nation dialing code, e.g. China is 86, USA is 1
        :param mobile: mobile number
        :param begin_time: begin time, unix timestamp
        :param end_time: end time, unix timestamp
        :param max_num: maximum number of message status
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(
            self._url, self._appid, rand)
        req = HTTPRequest(
            url=url,
            method="POST",
            headers={"Content-Type": "application/json"},
            body={
                "sig": util.calculate_signature(
                    self._appkey, rand, now),
                "type": msg_type,
                "time": now,
                "max": max_num,
                "begin_time": begin_time,
                "end_time": end_time,
                "nationcode": str(nation_code),
                "mobile": str(mobile)
            },
            connect_timeout=60,
            request_timeout=60
        )
        return util.api_request(req)
Beispiel #4
0
    def send(self, sms_type, nation_code, phone_numbers, msg,
             extend="", ext=""):
        """Send a SMS messages to multiple phones at once.

        :param number: SMS message type, Enum{0: normal SMS, 1: marketing SMS}
        :param nation_code: nation dialing code, e.g. China is 86, USA is 1
        :param phone_numbers: phone number array
        :param msg: SMS message content
        :param extend: extend field, default is empty string
        :param ext: ext field, content will be returned by server as it is
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(
            self._url, self._appid, rand)
        req = HTTPRequest(
            url=url,
            method="POST",
            headers={"Content-Type": "application/json"},
            body={
                "tel": [{"nationcode": nation_code, "mobile": pn}
                        for pn in phone_numbers],
                "type": int(sms_type),
                "msg": str(msg),
                "sig": util.calculate_signature(
                    self._appkey, rand, now, phone_numbers),
                "time": now,
                "extend": str(extend),
                "ext": str(ext)
            },
            connect_timeout=60,
            request_timeout=60
        )
        return util.api_request(req)
Beispiel #5
0
    def send(self, nation_code, phone_number, msg, playtimes=2, ext=""):
        """Send a voice verify code message.

        :param nation_code: nation dialing code, e.g. China is 86, USA is 1
        :param phone_number:  phone number
        :param msg: voice verify code message
        :param playtimes: playtimes, optional, max is 3, default is 2
        :param ext: ext field, content will be returned by server as it is
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(self._url, self._appid, rand)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={"Content-Type": "application/json"},
                          body={
                              "tel": {
                                  "nationcode": str(nation_code),
                                  "mobile": str(phone_number)
                              },
                              "msg":
                              msg,
                              "playtimes":
                              int(playtimes),
                              "sig":
                              util.calculate_signature(self._appkey, rand, now,
                                                       [phone_number]),
                              "time":
                              now,
                              "ext":
                              str(ext)
                          },
                          connect_timeout=60,
                          request_timeout=60)
        return util.api_request(req)
Beispiel #6
0
    def upload(self, file_content, content_type="mp3", url=None):
        """Upload voice file.

        :param file_content: voice file content
        :param content_type: voice file content type
        :param url: custom url
        """
        if content_type not in self.__class__.CONTENT_TYPES:
            raise ValueError("invalid content")
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}&time={}".format(
            url if url else self._url, self._appid, rand, now)
        file_sha1sum = util.sha1sum(file_content)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={
                              "Content-Type":
                              self.__class__.CONTENT_TYPES[content_type],
                              "x-content-sha1":
                              file_sha1sum,
                              "Authorization":
                              util.calculate_auth(self._appkey, rand, now,
                                                  file_sha1sum)
                          },
                          body=file_content)
        return util.api_request(req, self._httpclient)
Beispiel #7
0
    def send_with_param(self,
                        nationcode,
                        phone_numbers,
                        template_id,
                        params,
                        sign="",
                        extend="",
                        ext="",
                        url=None):
        """
        Send a SMS messages with template parameters to multiple
        phones at once.

        :param nationcode: nation dialing code, e.g. China is 86, USA is 1
        :param phone_numbers: multiple phone numbers
        :param template_id: template id
        :param params: template parameters
        :param sign: Sms user sign
        :param extend: extend field, default is empty string
        :param ext: ext field, content will be returned by server as it is
        :param url: custom url
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(url if url else self._url,
                                                self._appid, rand)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={"Content-Type": "application/json"},
                          body=json.dumps({
                              "tel": [{
                                  "nationcode": nationcode,
                                  "mobile": pn
                              } for pn in phone_numbers],
                              "sign":
                              sign,
                              "tpl_id":
                              int(template_id),
                              "params":
                              params,
                              "sig":
                              util.calculate_signature(self._appkey, rand, now,
                                                       phone_numbers),
                              "time":
                              now,
                              "extend":
                              str(extend),
                              "ext":
                              str(ext)
                          }))
        return util.api_request(req, self._httpclient)
Beispiel #8
0
    def send(self,
             sms_type,
             nationcode,
             phone_number,
             msg,
             extend="",
             ext="",
             url=None):
        """Send single SMS message.

        :param msg_type: SMS message type, Enum{0: normal SMS, 1: marketing SMS}
        :param nationcode: nation dialing code, e.g. China is 86, USA is 1
        :param phone_number: phone number
        :param msg: SMS message content
        :param extend: extend field, default is empty string
        :param ext: ext field, content will be returned by server as it is
        :param url: custom url
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(url if url else self._url,
                                                self._appid, rand)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={"Content-Type": "application/json"},
                          body=json.dumps({
                              "tel": {
                                  "nationcode": str(nationcode),
                                  "mobile": str(phone_number)
                              },
                              "type":
                              int(sms_type),
                              "msg":
                              str(msg),
                              "sig":
                              util.calculate_signature(self._appkey, rand, now,
                                                       [phone_number]),
                              "time":
                              now,
                              "extend":
                              str(extend),
                              "ext":
                              str(ext)
                          }))
        return util.api_request(req, self._httpclient)
Beispiel #9
0
    def send(self,
             nationcode,
             phone_number,
             prompttype,
             msg,
             playtimes=2,
             ext="",
             url=None):
        """Send a voice prompt message.

        :param naction_code: nation dialing code, e.g. China is 86, USA is 1
        :param phone_number: phone number
        :param prompttype: voice prompt type, currently value is 2
        :param msg: voice prompt message
        :param playtimes: playtimes, optional, max is 3, default is 2
        :param ext: ext field, content will be returned by server as it is
        :param url: custom url
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(url if url else self._url,
                                                self._appid, rand)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={"Content-Type": "application/json"},
                          body=json.dumps({
                              "tel": {
                                  "nationcode": str(nationcode),
                                  "mobile": str(phone_number)
                              },
                              "prompttype":
                              prompttype,
                              "promptfile":
                              str(msg),
                              "playtimes":
                              int(playtimes),
                              "sig":
                              util.calculate_signature(self._appkey, rand, now,
                                                       [phone_number]),
                              "time":
                              now,
                              "ext":
                              str(ext)
                          }))
        return util.api_request(req, self._httpclient)
Beispiel #10
0
    def send(self,
             template_id,
             params,
             phone_number,
             nationcode="86",
             playtimes=2,
             ext="",
             url=None):
        """Send tts voice.

        :param template_id: template id
        :param params: template parameters
        :param phone_number: phone number
        :param nationcode: nation dialing code, e.g. China is 86, USA is 1
        :param playtimes: playtimes, optional, max is 3, default is 2
        :param ext: ext field, content will be returned by server as it is
        :param url: custom url
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(url if url else self._url,
                                                self._appid, rand)
        req = HTTPRequest(url=url,
                          method="POST",
                          headers={"Content-Type": "application/json"},
                          body=json.dumps({
                              "tel": {
                                  "nationcode": str(nationcode),
                                  "mobile": phone_number
                              },
                              "tpl_id":
                              int(template_id),
                              "params":
                              params,
                              "sig":
                              util.calculate_signature(self._appkey, rand, now,
                                                       [phone_number]),
                              "time":
                              now,
                              "playtimes":
                              playtimes,
                              "ext":
                              str(ext)
                          }))
        return util.api_request(req, self._httpclient)
Beispiel #11
0
    def send_with_param(self, nation_code, phone_number, template_id,
                        params, sign="", extend="", ext=""):
        """Send single SMS message with template paramters.

        :param nation_code: nation dialing code, e.g. China is 86, USA is 1
        :param phone_number: phone number
        :param template_id: template id
        :param params: template parameters
        :param sign: Sms user sign
        :param extend: extend field, default is empty string
        :param ext: ext field, content will be returned by server as it is
        """
        rand = util.get_random()
        now = util.get_current_time()
        url = "{}?sdkappid={}&random={}".format(
            self._url, self._appid, rand)
        req = HTTPRequest(
            url=url,
            method="POST",
            headers={"Content-Type": "application/json"},
            body={
                "tel": {
                    "nationcode": str(nation_code),
                    "mobile": str(phone_number)
                },
                "sign": str(sign),
                "tpl_id": int(template_id),
                "params": params,
                "sig": util.calculate_signature(
                    self._appkey, rand, now, [phone_number]),
                "time": now,
                "extend": str(extend),
                "ext": str(ext)
            },
            connect_timeout=60,
            request_timeout=60
        )
        return util.api_request(req)