def send_mail_msg(to, subject='', html_body='', text_body='') -> dict: """ 发送邮件通知 """ if not IS_PROD_ENV: subject = f'{ENV_PREFIX} {subject}' assert isinstance(to, list), to to_address = ','.join(to) request = CommonRequest() request.set_accept_format('json') request.set_domain('dm.aliyuncs.com') request.set_protocol_type('https') request.set_version('2015-11-23') request.set_method('POST') request.set_action_name('SingleSendMail') request.add_query_param('RegionId', "cn-hangzhou") request.add_query_param('AccountName', "*****@*****.**") request.add_query_param('AddressType', "1") request.add_query_param('ReplyToAddress', "true") request.add_query_param('ReplyAddress', "*****@*****.**") request.add_query_param('TagName', "systemssss") request.add_query_param('FromAlias', "访道") request.add_query_param('ReplyAddressAlias', "PostMaster") request.add_query_param('ClickTrace', "1") request.add_query_param('Subject', subject) request.add_query_param('HtmlBody', html_body) request.add_query_param('TextBody', text_body) request.add_query_param('ToAddress', to_address) resp_dic = AliReqAction(request).do_req_action() return resp_dic
def cancel_call(): """ 取消点击呼叫 """ request = CancelCallRequest() request.set_accept_format('json') request.set_CallId('117059405036^10385912xx') resp_dic = AliReqAction(request).do_req_action() print(resp_dic) return resp_dic
def query_call_detail(): """ 查询通话记录详情 """ request = QueryCallDetailByCallIdRequest() request.set_accept_format('json') request.set_CallId('100625930001^10019107xx') request.set_ProdId(11000000300004) # 语音双呼 request.set_QueryDate(1577255564) resp_dic = AliReqAction(request).do_req_action() print(resp_dic) return resp_dic
def click_to_dial(): """ 点击呼叫 """ request = ClickToDialRequest() request.set_accept_format('json') request.set_CallerShowNumber('123xxxx') request.set_CallerNumber('1575xxxx') request.set_CalledShowNumber('12xxxx') request.set_CalledNumber('1885xxxx') resp_dic = AliReqAction(request).do_req_action() print(resp_dic) return resp_dic
def txt_spam_query(text): """ 文本反垃圾 """ data_id = str(generate_name_uuid(text)) ms = datetime.datetime.now().microsecond task = { 'dataId': data_id, 'content': text, 'time': ms, } params = { 'tasks': [task], 'scenes': ['antispam'], } content = json.dumps(params, sort_keys=True) resp = None try: request = TextScanRequest.TextScanRequest() request.set_accept_format('JSON') request.set_content(content) resp = AliReqAction(request).do_req_action() data_info = {row['dataId']: row for row in resp['data']}[data_id] code, msg = data_info['code'], data_info['msg'] if not (code == 200 and msg == 'OK'): logger.warning( f'txt_spam_query__fail {data_id} {code} {msg} resp: \n{resp}') return '内容检测失败' data = data_info['results'][0] suggestion = data['suggestion'] if not (suggestion == mc.Suggestion.Block): return '' rate = data['rate'] label = data['label'] label_desc = TextScanLabelDic[label] capture_message('txt_spam_query__hit', level='info') logger.warning( f'txt_spam_query__hit {data_id}: [{label}] {label_desc} {rate}') except Exception as exc: exc_type = type(exc).__name__ logger.info( f'txt_spam_query__error {data_id} {text} {exc_type} resp: \n{resp}' ) capture_exception(exc) logger.exception(exc) # 默认通过,为配合缓存 return '' return label_desc
def sms_query(send_at, phone, bizid, out_id) -> dict: """ 短信结果 """ national = phonenumbers.parse(phone, None).national_number send_date = time_floor_ts(send_at).format('YYYYMMDD') is_dev_fake = bizid.startswith('dd-mock-') if is_dev_fake: mock_status = random.randint(mc.SMSStatus.Waiting, mc.SMSStatus.Success) error_code = 'DELIVERED' if mock_status == mc.SMSStatus.Success else f'mock-{mock_status}' result = { 'OutId': out_id, 'SendDate': get_tzcn_format(send_at), 'SendStatus': 3, 'ReceiveDate': get_tzcn_format(send_at), 'ErrCode': error_code, 'TemplateCode': 'dev-mock-TemplateCode', 'Content': 'dev-mock-Content', 'PhoneNum': 'dev-mock-PhoneNum', } return result request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_protocol_type('https') request.set_version('2017-05-25') request.set_method('POST') request.set_action_name('QuerySendDetails') request.add_query_param('RegionId', 'cn-hangzhou') request.add_query_param('CurrentPage', 1) # 当前页码 request.add_query_param('PageSize', 10) # 每页显示 1~50 request.add_query_param('PhoneNumber', national) # 手机号码 request.add_query_param('SendDate', send_date) # 发送日期 request.add_query_param('BizId', bizid) # 发送回执ID resp_dic = AliReqAction(request).do_req_action() results = resp_dic['SmsSendDetailDTOs']['SmsSendDetailDTO'] result = {row['OutId']: row for row in results}[out_id] return result
def ocr_req(oss_url, data_id, card, scenes=None): """ OCR请求 """ if not scenes: scenes = ['ocr'] task = { 'dataId': data_id, 'url': oss_url, } params = { 'tasks': [task], 'scenes': scenes, 'extras': {'card': card}, } content = json.dumps(params, sort_keys=True) request = ImageSyncScanRequest.ImageSyncScanRequest() request.set_content(content.encode()) resp = AliReqAction(request).do_req_action() data_info = {row['dataId']: row for row in resp['data']}[data_id] code, msg = data_info['code'], data_info['msg'] if not (code == 200 and msg == 'OK'): logger.info(f'ocr_query__fail {data_id} {card} {code} {msg} resp: \n{resp}') return f'ocr_fail {code} {msg}' results = {row['scene']: row for row in data_info['results']} return results
def sms_send(template, params, phone, out_id, sign='') -> dict: """ 短信发送 """ assert isinstance(params, dict) sign = sign or sms_constant.SMS_SIGN national = phonenumbers.parse(phone, None).national_number if IS_DEV_ENV: from server.corelib.notice.async_tasks import send_dd_msg__task content = sms_mock_content(template, params) msg = f'模拟短信发送:{national}\n{content}' result = send_dd_msg__task(msg) mock_resp = { 'Message': 'OK', 'RequestId': f'dd-mock-RequestId-{out_id}', 'BizId': f'dd-mock-BizId-{out_id}', 'memo': 'dd-dev-mock', 'result': result, 'Code': 'OK', } logger.info(f'sms_send__dd_mock_resp: {mock_resp}') return mock_resp tpl_param = json.dumps(params, sort_keys=True) request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_protocol_type('https') request.set_version('2017-05-25') request.set_method('POST') request.set_action_name('SendSms') request.add_query_param('RegionId', 'cn-hangzhou') request.add_query_param('SignName', sign) # 签名 request.add_query_param('PhoneNumbers', national) # 手机号 request.add_query_param('TemplateCode', template) # 短信模板 request.add_query_param('TemplateParam', tpl_param) # 模板参数 request.add_query_param('OutId', out_id) # 外部流水扩展字段 resp_dic = AliReqAction(request).do_req_action() return resp_dic