独角鲸同步合作方公司数据项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.4 KiB

10 months ago
import logging
import traceback
from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from ChaCeRndTrans import settings
error_logger = logging.getLogger("error")
def get_client():
access_key_id = settings.ACCESS_KEY_ID
access_secret = settings.ACCESS_SECRET
client = AcsClient(access_key_id, access_secret, 'cn-hangzhou')
return client
# 获取reques对象
# method_type 请求方式 get/post
# protocol_type https/http请求
# action_name 方法名
def get_request(method_type, protocol_type, action_name):
request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method(method_type) # get | POST
request.set_protocol_type(protocol_type) # https | http
request.set_version('2017-05-25')
request.set_action_name(action_name) # 方法名
return request
# 发送短信接口
def send_msg(phone_number, sign_name, template_code, template_param):
request = get_request('post', 'https', 'SendSms')
client = get_client()
# 调用发送短信接口
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('PhoneNumbers', phone_number)
request.add_query_param('SignName', sign_name) # 签名
request.add_query_param('TemplateCode', template_code)
request.add_query_param('TemplateParam', template_param)
try:
response = client.do_action_with_exception(request)
except ServerException as e:
# ServerException的处理逻辑
error_logger.error(traceback.format_exc())
except ClientException as e:
# ClientException的处理逻辑
error_logger.error(traceback.format_exc())
print(str(response, encoding='utf-8'))
return response
# source_url:原始链接地址,不超过1000个字符长度。
# short_url_name:短链服务名称,不超过12个字符长度。
# effective_days:短链服务使用有效期,以天为单位,不超过30天。
def add_short_url(source_url, short_url_name, effective_days):
request = get_request('post', 'https', 'AddShortUrl')
client = get_client()
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('SourceUrl', source_url)
request.add_query_param('ShortUrlName', short_url_name)
request.add_query_param('EffectiveDays', effective_days)
try:
response = client.do_action_with_exception(request)
except ServerException as e:
error_logger.error(traceback.format_exc())
except ClientException as e:
error_logger.error(traceback.format_exc())
print(str(response, encoding='utf-8'))
return response
# 添加模板接口
def add_template(template_type, template_name, template_content, remark):
request = get_request('post', 'https', 'AddSmsTemplate')
client = get_client()
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('TemplateType', template_type)
request.add_query_param('TemplateName', template_name)
request.add_query_param('TemplateContent', template_content)
request.add_query_param('Remark', remark)
try:
response = client.do_action_with_exception(request)
except ServerException as e:
error_logger.error(traceback.format_exc())
except ClientException as e:
error_logger.error(traceback.format_exc())
print(str(response, encoding='utf-8'))
return response
# 删除模板接口
def delete_template(template_code):
request = get_request('post', 'https', 'DeleteSmsTemplate')
client = get_client()
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('TemplateCode', template_code)
try:
response = client.do_action_with_exception(request)
except ServerException as e:
error_logger.error(traceback.format_exc())
except ClientException as e:
error_logger.error(traceback.format_exc())
print(str(response, encoding='utf-8'))
return response
# 修改模板接口
def edit_template(template_type, template_name, template_content, remark, template_code):
request = get_request('post', 'https', 'ModifySmsTemplate')
client = get_client()
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('TemplateType', template_type)
request.add_query_param('TemplateName', template_name)
request.add_query_param('TemplateCode', template_code)
request.add_query_param('TemplateContent', template_content)
request.add_query_param('Remark', remark)
try:
response = client.do_action_with_exception(request)
except ServerException as e:
error_logger.error(traceback.format_exc())
except ClientException as e:
error_logger.error(traceback.format_exc())
print(str(response, encoding='utf-8'))
return response
# 查询模板接口
def query_template(template_code):
request = get_request('post', 'https', 'QuerySmsTemplate')
client = get_client()
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('TemplateCode', template_code)
try:
response = client.do_action_with_exception(request)
except ServerException as e:
error_logger.error(traceback.format_exc())
except ClientException as e:
error_logger.error(traceback.format_exc())
print(str(response, encoding='utf-8'))
return response