/// <summary> /// 微信消息模板推送 /// </summary> /// <param name="userids"></param> /// <param name="batchid"></param> /// <param name="log"></param> /// <returns></returns> public static async Task <bool> PushByUserIDAndBatchIDAsync(IEnumerable <string> userids, int batchid, PushTemplateLog log) { try { using (var client = new TemplatePushClient()) { var pushResult = await client.PushByUserIDAndBatchIDAsync(userids, batchid, log); if (pushResult.Success && pushResult.Result) { return(true); } else { Logger.Warn($"PushByUserIDAndBatchIDAsync 推送失败{pushResult.ErrorCode + pushResult.ErrorMessage}"); } } } catch (Exception ex) { Logger.Error($"PushByUserIDAndBatchIDAsync 异常,{ex.Message + ex.StackTrace}", ex); } return(false); }
public static bool PushTemplate(PushTemplate data, PushTemplateLog template) { var result = false; try { using (var client = new WeiXinPushClient()) { var getResult = client.PushByTemplate(data, template); getResult.ThrowIfException(true); result = getResult.Result; } } catch (Exception ex) { logger.Error(ex); } return(result); }
public static bool PushWechatInfoByBatchId(int batchId, PushTemplateLog template) { var result = false; try { var templateResult = SelectTemplateByBatchID(batchId); if (templateResult != null && templateResult.Any()) { var wxTemplate = templateResult.FirstOrDefault(x => x.DeviceType == DeviceType.WeChat); if (wxTemplate != null) { result = PushTemplate(wxTemplate, template); } } } catch (Exception ex) { logger.Error(ex); } return(result); }
private void PushMessage(Guid packetGroupNo, string nickName, int count, Guid userId) { var templateLog = new PushTemplateLog { Replacement = JsonConvert.SerializeObject(new Dictionary <string, string> { { "{{leftnumber}}", count.ToString() }, { "{{packetgroupno}}", packetGroupNo.ToString("D") }, { "{{nickname}}", nickName } }) }; var client = new TemplatePushClient(); int batchId = 1642; var target = userId.ToString("D"); try { var result = client.PushByUserIDAndBatchID(new List <string> { target.ToLower() }, batchId, templateLog); result.ThrowIfException(true); if (!(result.Success && result.Result)) { Logger.Warn( $"向用户{JsonConvert.SerializeObject(target)}推送信息{JsonConvert.SerializeObject(templateLog)},推送失败", result.Exception); } } catch (Exception ex) { Logger.Warn($"向用户{JsonConvert.SerializeObject(target)}推送信息{batchId}出现异常", ex); } finally { client.Dispose(); } }
/// <summary> /// 消息推送 /// </summary> /// <param name="userids"></param> /// <param name="batchid"></param> /// <param name="log"></param> /// <returns></returns> public async static Task <OperationResult <bool> > PushByUserIDAndBatchIDAsync(IEnumerable <string> userids, int batchid, PushTemplateLog log) { if (userids != null && userids.Any()) { using (var client = new TemplatePushClient()) { var result = await client.PushByUserIDAndBatchIDAsync(userids, batchid, log); if (!result.Success) { Logger.Error($" PushByUserIDAndBatchIDAsync fail => ErrorCode ={result.ErrorCode} & ErrorMessage ={result.ErrorMessage} "); } return(result); } } else { return(OperationResult.FromResult(true)); } }
/// <summary> /// 按照batchid和userid推送消息 /// </summary> /// <param name="userids"></param> /// <param name="batchid"></param> /// <param name="log"></param> /// <returns></returns> public async Task <OperationResult <bool> > PushByUserIDAndBatchIDAsync(IEnumerable <string> userids, int batchid, PushTemplateLog log) { var result = await _Client.PushByUserIDAndBatchIDAsync(userids, batchid, log).ConfigureAwait(false); result.ThrowIfException(true); if (!result.Success) { _logger.Error($"PushService PushByUserIDAndBatchIDAsync fail => ErrorCode ={result.ErrorCode} & ErrorMessage ={result.ErrorMessage}"); } return(result); }
/// <summary> /// 发送短信 /// </summary> /// <param name="couponRuleList"></param> /// <param name="user"></param> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async ValueTask <bool> SendNoyificationAsync(List <PromotionTaskPromotionListModel> couponRuleList, User user, SendCouponRequest request, CancellationToken cancellationToken) { try { List <string> smsParam = JsonConvert.DeserializeObject <List <string> >(request.SmsParam ?? ""); string msgUrl = string.Empty; //追踪短信链接 string msgBatchs = string.Empty; //追踪短信批次 string msgSubject = string.Empty; //追踪短信主题 if (request.SmsId > 0 && smsParam.Any()) //判断是否发送短信 { if (smsParam.FirstOrDefault().ToLower().Contains("tuhu.cn")) //发送追踪短信 { msgUrl = smsParam[0]; smsParam.RemoveAt(0); msgBatchs = $"CouponTask_{request.PromotionTaskId}_{DateTime.Now.ToString("yyyyMMdd")}"; msgSubject = $"优惠券任务:{request.PromotionTaskId}"; var biSmsRequest = new BiSmsRequest() { PhoneNumber = user.MobileNumber, MsgBatchs = msgBatchs, MsgSubject = msgSubject, MsgBody = smsParam.ToArray(), MsgUrl = msgUrl, RelatedUser = request.Creater, TemplateId = request.SmsId }; await smsService.SendBiSmsAsync(biSmsRequest, cancellationToken).ConfigureAwait(false); } else //发送普通短信 { var smsRequest = new SendTemplateSmsRequest() { Cellphone = user.MobileNumber, TemplateId = request.SmsId, TemplateArguments = smsParam.ToArray() }; await smsService.SendSmsAsync(smsRequest).ConfigureAwait(false); } } foreach (var couponRule in couponRuleList.Where(p => p.IsRemind == "1")) { PushTemplateLog pushTemplateLog = new PushTemplateLog() { Replacement = JsonConvert.SerializeObject( new Dictionary <string, string> { ["{{currenttime}}"] = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm"), ["{{couponamount}}"] = couponRule.DiscountMoney.ToString(CultureInfo.InvariantCulture), ["{{couponname}}"] = couponRule.PromotionDescription }) }; await pushService.PushByUserIDAndBatchIDAsync(new List <string> { user.MobileNumber }, 1509, pushTemplateLog).ConfigureAwait(false); } return(true); } catch (Exception ex) { logger.LogWarning("SendNoyificationAsync Exception", ex); } return(false); }