public async Task <SmsSenderResult> SendSmsAsync( NotificationSystemBroker.SmsProviderClient.SendSmsRequestModel model) { var result = new SmsSenderResult(); // Try to send SMS via Infobip client and propagate proper audit message try { _log.Info("Sending SMS with Infobip...", new { model.MessageId }); var response = await TryExecute(() => _infobipClient.Api.SendSmsAsync(new SendSmsRequestModel { From = _fromSender, Text = model.Message, To = model.PhoneNumber })); // Valid statuses that are treated as SMS is sent // 0 - OK, 1 - PENDING, 3 - DELIVERED var validStatuses = new[] { "OK", "PENDING", "DELIVERED" }; var responseMessage = response.Messages.First(); var messageStatus = responseMessage.Status; if (validStatuses.All(x => string.Compare(x, messageStatus.GroupName, StringComparison.InvariantCultureIgnoreCase) != 0)) { _log.Error( null, "Could not send SMS", new { model.MessageId, InfobipResponse = new { responseMessage.MessageId, responseMessage.Status } }); result.Result = SmsResult.Error; result.ErrorMessage = $"{messageStatus.Name} - {messageStatus.Description}"; return(result); } result.Result = SmsResult.Ok; _log.Info("SMS sent", new { model.MessageId, InfobipResponse = new { responseMessage.MessageId, responseMessage.Status } }); } catch (Exception e) { _log.Error(e, "Could not send SMS", new { model.MessageId }); result.Result = SmsResult.Error; result.ErrorMessage = e.Message; } return(result); }
public async Task <SmsSenderResult> SendSmsAsync(SendSmsRequestModel model) { var isSuccessful = await _smsService.CreateAsync(model.MessageId, model.PhoneNumber, model.Message); var result = new SmsSenderResult { Result = SmsResult.Ok }; if (isSuccessful) { return(result); } result.Result = SmsResult.Error; result.ErrorMessage = "Error saving to database"; return(result); }
/// <summary> /// 普通单发短信接口,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名 /// </summary> /// <param name="type">短信类型,0 为普通短信,1 营销短信</param> /// <param name="nationCode">国家码,如 86 为中国</param> /// <param name="phoneNumber">不带国家码的手机号</param> /// <param name="msg">信息内容,必须与申请的模板格式一致,否则将返回错误</param> /// <param name="extend">扩展码,可填空</param> /// <param name="ext">服务端原样返回的参数,可填空</param> /// <returns></returns> public SmsSenderResult Send(int type, string nationCode, string phoneNumber, string msg, string extend, string ext) { if (0 != type && 1 != type) { throw new Exception("type " + type + " error"); } if (null == extend) { extend = ""; } if (null == ext) { ext = ""; } long random = util.GetRandom(); long curTime = util.GetCurTime(); // 按照协议组织 post 请求包体 JObject data = new JObject(); JObject tel = new JObject(); tel.Add("nationcode", nationCode); tel.Add("mobile", phoneNumber); data.Add("tel", tel); data.Add("msg", msg); data.Add("type", type); data.Add("sig", util.StrToHash(String.Format("appkey={0}&random={1}&time={2}&mobile={3}", AppKey, random, curTime, phoneNumber))); data.Add("time", curTime); data.Add("extend", extend); data.Add("ext", ext); string wholeUrl = smsurl + "?sdkappid=" + SDKAppID + "&random=" + random; HttpWebRequest request = util.GetPostHttpConn(wholeUrl); byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); request.ContentLength = requestData.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(requestData, 0, requestData.Length); requestStream.Close(); // 接收返回包 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); string responseStr = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); SmsSenderResult result; if (HttpStatusCode.OK == response.StatusCode) { result = util.ResponseStrToSenderResult(responseStr); } else { result = new SmsSenderResult(); result.result = -1; result.errmsg = "http error " + response.StatusCode + " " + responseStr; } return(result); }
/// <summary> /// 指定模板群发 /// 【注意】海外短信无群发功能 /// </summary> /// <param name="nationCode">国家码,如 86 为中国</param> /// <param name="phoneNumbers">不带国家码的手机号列表</param> /// <param name="templId">模板 id</param> /// <param name="templParams">模板参数列表</param> /// <param name="sign">签名,如果填空,系统会使用默认签名</param> /// <param name="extend">扩展码,可以填空</param> /// <param name="ext">服务端原样返回的参数,可以填空</param> /// <returns></returns> public SmsSenderResult SendWithParam(String nationCode, List <string> phoneNumbers, int templId, List <string> templParams, string sign, string extend, string ext) { if (null == sign) { sign = ""; } if (null == extend) { extend = ""; } if (null == ext) { ext = ""; } long random = util.GetRandom(); long curTime = util.GetCurTime(); // 按照协议组织 post 请求包体 JObject data = new JObject(); data.Add("tel", util.PhoneNumbersToJSONArray(nationCode, phoneNumbers)); data.Add("sig", util.CalculateSigForTempl(AppKey, random, curTime, phoneNumbers)); data.Add("tpl_id", templId); data.Add("params", util.SmsParamsToJSONArray(templParams)); data.Add("sign", sign); data.Add("time", curTime); data.Add("extend", extend); data.Add("ext", ext); string wholeUrl = multismsurl + "?sdkappid=" + SDKAppID + "&random=" + random; HttpWebRequest request = util.GetPostHttpConn(wholeUrl); byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); request.ContentLength = requestData.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(requestData, 0, requestData.Length); requestStream.Close(); // 接收返回包 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); string responseStr = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); SmsSenderResult result; if (HttpStatusCode.OK == response.StatusCode) { result = util.ResponseStrToSenderResult(responseStr); } else { result = new SmsSenderResult(); result.result = -1; result.errmsg = "http error " + response.StatusCode + " " + responseStr; } return(result); }