def send_msg(receiver, msg_info, sms_client=None): """ 给某人发送带有签名的短信 :param receiver: 18009090101 :param msg_info: {"invoke_id":"签名id", "template_id":"短信模板id","content_var": "短信模板参数“} :param sms_client: :return: """ request_id = None try: invoke_id = msg_info["invoke_id"] template_id = msg_info["template_id"] content_var = msg_info["content_var"] except Exception as e: LOG.error("send_msg传参错误,%s" % (str(e))) return request_id try: if sms_client is None: sms_client = sms.SmsClient(CONF.config) response = sms_client.send_message_2(invoke_id, template_id, receiver, content_var) request_id = response.request_id except ex.BceHttpClientError as e: if isinstance(e.last_error, ex.BceServerError): LOG.error('send request failed. Response %s, code: %s, msg: %s' % (e.last_error.status_code, e.last_error.code, e.last_error.message)) else: LOG.error('send request failed. Unknown exception: %s' % e) except Exception as e: LOG.error(str(e)) return request_id
def setUp(self): self.sms = sms.SmsClient(CONF.config) # 签名 self.invoke_id_valid = 'y2W4LRun-9sMD-MeGd' self.invoke_id_inValid = 'not-ready-invoke-id' # 模版 self.template_valid = 'smsTpl:78d20e6c-201c-4f97-b57b-240e9dc7d831' self.template_inValid = 'smsTpl:ae63a600-1474-4d40-b8d8-9ed9f924bf3b'
def send_msg_to_group(receivers, msg_info): """ :param receivers: ['19098988778', '18978988767'] :param msg_info: {"invoke_id":"签名id", "template_id":"短信模板id","content_var": "短信模板参数“} :return: """ receivers = list(set(receivers)) sms_client = sms.SmsClient(CONF.config) for phone in receivers: _ret = send_msg(phone, msg_info, sms_client) _success, _request_id = ('success', _ret) if _ret else ('failed', 0) _log_msg = "Send msg to %s,%s,requestId:%s" % (phone, _success, _request_id) LOG.info(_log_msg)
sys.setdefaultencoding('utf-8') file_path = os.path.normpath(os.path.dirname(__file__)) sys.path.append(file_path + '/../../') import sms_sample_conf from baidubce.services.sms import sms_client as sms from baidubce import exception as ex logging.basicConfig(level=logging.DEBUG, filename='./sms_sample.log', filemode='w') LOG = logging.getLogger(__name__) CONF = sms_sample_conf if __name__ == '__main__': sms_client = sms.SmsClient(CONF.config) try: # query user quota LOG.debug('\n\n\nSample 1: Query quota\n\n\n') response = sms_client.query_quota() print(response.max_send_per_day, response.max_receive_per_phone_number_day,\ response.sent_today) # create template LOG.debug('\n\n\nSample 2: Create template \n\n\n') response = sms_client.create_template('python-sdk-test', '${PYTHON} SDK ${CODE}', 'dkwL6mUT-7JNv-H5Z3') template_id = response.data.template_id print(template_id)
def push_messenger(message_body): global worker_config try: from baidubce.services.sms import sms_client as sms from baidubce import exception as ex from baidubce.bce_client_configuration import BceClientConfiguration from baidubce.auth.bce_credentials import BceCredentials except ImportError: print "Path error!!" print sys.path sys.exit() if worker_config is not None: app_path = get_app_path() worker_config = get_worker_config("SMSWorker", app_path['conf']) HOST = worker_config['app']['HOST'] AK = worker_config['app']['AK'] SK = worker_config['app']['SK'] logger = logging.getLogger('baidubce.services.sms.smsclient') fh = logging.FileHandler('sms_sample.log') fh.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logger.setLevel(logging.DEBUG) logger.addHandler(fh) bce_config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST) logging.basicConfig(level=logging.DEBUG, filename='./sms.log', filemode='w') LOG = logging.getLogger(__name__) sms_client = sms.SmsClient(bce_config) try: """ # query template list LOG.debug('Query template list') response = sms_client.get_template_list() valid_template_id = None valid_template_content = None for temp in response.template_list: print temp.template_id, temp.name, temp.content, \ temp.status, temp.create_time, temp.update_time if temp.status == u'VALID': valid_template_id = temp.template_id valid_template_content = temp.content """ LOG.debug('Send Message') response = sms_client.send_message( 'smsTpl:8504139f-2b8e-49f3-adc4-957b8b3bfa67', [message_body], {'username': "******"}) message_id = response.message_id return message_id """ #query message LOG.debug('query Message') response = sms_client.query_message_detail(message_id) print response.message_id, response.receiver, response.content, response.send_time """ except ex.BceHttpClientError as e: if isinstance(e.last_error, ex.BceServerError): LOG.error('send request failed. Response %s, code: %s, msg: %s' % (e.last_error.status_code, e.last_error.code, e.last_error.message)) else: LOG.error('send request failed. Unknown exception: %s' % e)