Beispiel #1
0
        public async Task <MessageSendResponse> SendMulticast(MessageSendRequest request)
        {
            var message = new MulticastMessage()
            {
                Notification = new Notification()
                {
                    Title = request.NotificationTitle,
                    Body  = request.NotificationBody,
                },
                Tokens = request.Tokens,
            };

            var response = await _firebaseService.SendMulticastAsync(message);

            var returnResponse = new MessageSendResponse()
            {
                Responses = response.Responses.Select(x => new ResponseItem
                {
                    IsSuccess = x.IsSuccess,
                    MessageId = x.MessageId,
                    Exception = x.IsSuccess ? null : new ExceptionItem
                    {
                        Message            = x.Exception.Message,
                        MessagingErrorCode = x.Exception.MessagingErrorCode.HasValue ? (int)x.Exception.MessagingErrorCode : 0
                    }
                }),
                FailureCount = response.FailureCount,
                SuccessCount = response.SuccessCount
            };

            return(returnResponse);
        }
Beispiel #2
0
        public void OnMessageSend(MessageSendResponse message)
        {
            Debug.Log($@"OnMessageSend Type:{message.Type} Reuslt:{message.Result}
Errormsg:{message.Errormsg}");
            if (message.Type == MessageType.Friend)
            {
                OnFriendMessageSend.Invoke(message);
            }
        }
        private PageResult SendMessage(MessageSendRequest request, Tbl_OTABusiness business)
        {
            MessageSendOrderInfo orderInfo = request.Body.OrderInfo;
            MessageSendResponse  result    = new MessageSendResponse
            {
                Head = HeadResult.V1
            };
            var validResult = _orderService.ValidDataForMessageSendRequest(request);

            if (!validResult.Status)
            {
                result.Head.Code     = validResult.Code;
                result.Head.Describe = validResult.Message;
                return(PageDataResult.Data(result, business.Saltcode.ToString()));
            }
            var tbl_Order = _orderService.Get(orderInfo.OrderId);

            if (tbl_Order == null)
            {
                result.Head.Code     = "117004";
                result.Head.Describe = "(重)发送入园凭证短信异常,订单不存在";
                return(PageDataResult.Data(result, business.Saltcode.ToString()));
            }
            var tbl_OrderDetails = _orderDetailService.GetList(tbl_Order.OrderNo);

            if (tbl_OrderDetails.FirstOrDefault(a => a.EticektSendQuantity >= 5) != null)
            {
                result.Head.Code     = "117006";
                result.Head.Describe = "(重)发送入园凭证短信异常,发送次数不能超过5次";
                return(PageDataResult.Data(result, business.Saltcode.ToString()));
            }
            try
            {
                var sendResult = _smsService.Send(tbl_OrderDetails, tbl_Order.Mobile);
                if (sendResult.Status)
                {
                    result.Body = new MessageSendResponseBody
                    {
                        Message = "OK"
                    };
                    result.Head.Code     = "000000";
                    result.Head.Describe = "成功";
                    return(PageDataResult.Data(result, business.Saltcode.ToString()));
                }
                result.Head.Code     = "117005";
                result.Head.Describe = "(重)发送入园凭证短信异常,发送失败";
                return(PageDataResult.Data(result, business.Saltcode.ToString()));
            }
            catch (Exception ex)
            {
                result.Head.Code     = "117005";
                result.Head.Describe = "(重)发送入园凭证短信异常,发送失败";
                return(PageDataResult.Data(result, business.Saltcode.ToString()));
            }
        }
        public async Task <ServiceProviderResponse> SendMessageByGroupAsync(
            [FromHeader(Constant.OperationTrackingIdHeader)] string requestId,
            [FromHeader] string account,
            [FromBody] MessageSendByGroupRequest request)
        {
            Account currentAccount = null;

            try
            {
                // TODO: limit on max target?
                Validator.ArgumentNotNull(request, nameof(request));
                Validator.ArgumentNotNull(request.MessageBody, nameof(request.MessageBody));
                Validator.ArgumentNotNullOrEmpty(request.MessageBody.TemplateName, request.MessageBody.TemplateName);
                Validator.IsTrue <ArgumentException>(request.Targets != null && request.Targets.Count > 0, nameof(request.Targets), "Targets is empty");
                Validator.IsTrue <ArgumentException>(request.Targets.Count <= 10, nameof(request.Targets), "Number of Targets could not be more than 10");

                currentAccount = await EnsureAccount(account, requestId);

                var tuples = await BuildInputMessageAsync(currentAccount, request.MessageBody, request.Targets, TargetType.Group, requestId);

                var message   = tuples.Item1;
                var extension = tuples.Item2;

                // Update report
                await this.reportManager.OnMessageSentAsync(currentAccount.EngagementAccount, message, extension);

                // Dispatch
                await DispatchMessageAsync(message, extension, requestId);

                metricManager.LogSendSuccess(1, currentAccount.EngagementAccount, currentAccount.SubscriptionId ?? string.Empty);

                var response = new MessageSendResponse
                {
                    MessageId = message.MessageInfo.MessageId,
                    SendTime  = message.MessageInfo.SendTime
                };

                return(ServiceProviderResponse.CreateJsonResponse(HttpStatusCode.OK, response));
            }
            catch
            {
                metricManager.LogSendFailed(1, currentAccount.EngagementAccount, currentAccount?.SubscriptionId ?? string.Empty);
                throw;
            }
        }
Beispiel #5
0
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <returns></returns>
        public string Send()
        {
            if (Signature != GetParam("sig").ToString())
            {
                ErrorCode = (int)ErrorType.API_EC_SIGNATURE;
                return("");
            }

            //如果是桌面程序则需要验证用户身份
            if (this.App.ApplicationType == (int)ApplicationType.DESKTOP)
            {
                if (Uid < 1)
                {
                    ErrorCode = (int)ErrorType.API_EC_SESSIONKEY;
                    return("");
                }
            }

            if (CallId <= LastCallId)
            {
                ErrorCode = (int)ErrorType.API_EC_CALLID;
                return("");
            }

            if (!CheckRequiredParams("subject,message,to_ids"))
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return("");
            }
            string ids = GetParam("to_ids").ToString();

            if (!CheckRequiredParams("to_ids") || !Utils.IsNumericArray(ids.Split(',')))
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return("");
            }

            if ((!CheckRequiredParams("from_id") || !Utils.IsNumeric(GetParam("from_id"))) && Uid < 1)
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return("");
            }
            int fromId = TypeConverter.ObjectToInt(GetParam("from_id"), Uid);

            string message = UBB.ParseUrl(Utils.EncodeHtml(GetParam("message").ToString()));

            string[] to_ids = ids.Split(',');

            string successfulIds = string.Empty;

            foreach (string id in to_ids)
            {
                if (TypeConverter.StrToInt(id) < 1)
                {
                    continue;
                }
                PrivateMessageInfo pm = new PrivateMessageInfo();
                pm.Folder       = 0;
                pm.Message      = message;
                pm.Msgfrom      = Discuz.Forum.Users.GetShortUserInfo(fromId).Username;
                pm.Msgfromid    = fromId;
                pm.Msgto        = "";//可能需要查询具体的收件人姓名
                pm.Msgtoid      = TypeConverter.StrToInt(id);
                pm.New          = 1;
                pm.Postdatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                pm.Subject      = GetParam("subject").ToString();

                successfulIds += (PrivateMessages.CreatePrivateMessage(pm, 0) > 0) ? (id + ",") : "";
            }
            successfulIds = successfulIds.Length > 0 ? successfulIds.Remove(successfulIds.Length - 1) : successfulIds;

            if (Format == FormatType.JSON)
            {
                return(string.Format("\"{0}\"", successfulIds));
            }

            MessageSendResponse nsr = new MessageSendResponse();

            nsr.Result = successfulIds;
            return(SerializationHelper.Serialize(nsr));
        }
Beispiel #6
0
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <returns></returns>
        public string Send()
        {
            if (Signature != GetParam("sig").ToString())
            {
                ErrorCode = (int)ErrorType.API_EC_SIGNATURE;
                return "";
            }

            //如果是桌面程序则需要验证用户身份
            if (this.App.ApplicationType == (int)ApplicationType.DESKTOP)
            {
                if (Uid < 1)
                {
                    ErrorCode = (int)ErrorType.API_EC_SESSIONKEY;
                    return "";
                }
            }

            if (CallId <= LastCallId)
            {
                ErrorCode = (int)ErrorType.API_EC_CALLID;
                return "";
            }

            if (!CheckRequiredParams("subject,message,to_ids"))
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }
            string ids = GetParam("to_ids").ToString();

            if (!CheckRequiredParams("to_ids") || !Utils.IsNumericArray(ids.Split(',')))
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }

            if ((!CheckRequiredParams("from_id") || !Utils.IsNumeric(GetParam("from_id"))) && Uid < 1)
            {
                ErrorCode = (int)ErrorType.API_EC_PARAM;
                return "";
            }
            int fromId = TypeConverter.ObjectToInt(GetParam("from_id"), Uid);

            string message = UBB.ParseUrl(Utils.EncodeHtml(GetParam("message").ToString()));

            string[] to_ids = ids.Split(',');

            string successfulIds = string.Empty;
            foreach (string id in to_ids)
            {
                if (TypeConverter.StrToInt(id) < 1)
                    continue;
                PrivateMessageInfo pm = new PrivateMessageInfo();
                pm.Folder = 0;
                pm.Message = message;
                pm.Msgfrom = Discuz.Forum.Users.GetShortUserInfo(fromId).Username;
                pm.Msgfromid = fromId;
                pm.Msgto = "";//可能需要查询具体的收件人姓名
                pm.Msgtoid = TypeConverter.StrToInt(id);
                pm.New = 1;
                pm.Postdatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                pm.Subject = GetParam("subject").ToString();

                successfulIds += (PrivateMessages.CreatePrivateMessage(pm, 0) > 0) ? (id + ",") : "";
            }
            successfulIds = successfulIds.Length > 0 ? successfulIds.Remove(successfulIds.Length - 1) : successfulIds;

            if (Format == FormatType.JSON)
                return string.Format("\"{0}\"", successfulIds);

            MessageSendResponse nsr = new MessageSendResponse();
            nsr.Result = successfulIds;
            return SerializationHelper.Serialize(nsr);
        }
Beispiel #7
0
        /*
         * 每个用户UID 30秒内只能调用一次该接口
         */
        public override bool Run(CommandParameter commandParam, ref string result)
        {
            //如果是桌面程序则需要验证用户身份
            if (commandParam.AppInfo.ApplicationType == (int)ApplicationType.DESKTOP && commandParam.LocalUid < 1)
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_SESSIONKEY, commandParam.ParamList);
                return(false);
            }

            if (!commandParam.CheckRequiredParams("subject,message,to_ids"))
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PARAM, commandParam.ParamList);
                return(false);
            }

            string ids = commandParam.GetDNTParam("to_ids").ToString();

            if (!Utils.IsNumericList(ids))
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PARAM, commandParam.ParamList);
                return(false);
            }

            string[] idArray = ids.Split(',');
            if (idArray.Length > 10)
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PM_TOID_OVERFLOW, commandParam.ParamList);
                return(false);
            }
            //桌面应用程序用户强制使用session_info.uid
            int fromId = commandParam.AppInfo.ApplicationType == (int)ApplicationType.DESKTOP ?
                         commandParam.LocalUid : commandParam.GetIntParam("from_id", commandParam.LocalUid);
            ShortUserInfo fromUserInfo = Discuz.Forum.Users.GetShortUserInfo(fromId);

            if (fromUserInfo == null)
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PM_FROMID_NOT_EXIST, commandParam.ParamList);
                return(false);
            }

            //如果发送用户不是管理员,且在30秒内调用了该接口
            if (fromUserInfo.Adminid != 1 && !CommandCacheQueue <SendMessageItem> .EnQueue(new SendMessageItem(fromUserInfo.Uid, DateTime.Now.Ticks)))
            {
                result = Util.CreateErrorMessage(ErrorType.API_EC_PM_VISIT_TOOFAST, commandParam.ParamList);
                return(false);
            }

            string message       = UBB.ParseUrl(Utils.EncodeHtml(commandParam.GetDNTParam("message").ToString()));
            string successfulIds = string.Empty;

            foreach (string id in ids.Split(','))
            {
                int toUid = TypeConverter.StrToInt(id);
                if (toUid < 1 || toUid == fromId)
                {
                    continue;
                }
                ShortUserInfo toUserInfo = Discuz.Forum.Users.GetShortUserInfo(toUid);
                if (toUserInfo == null)
                {
                    continue;
                }

                PrivateMessageInfo pm = new PrivateMessageInfo();
                pm.Folder       = 0;
                pm.Message      = message;
                pm.Msgfrom      = fromUserInfo.Username;
                pm.Msgfromid    = fromId;
                pm.Msgto        = toUserInfo.Username;
                pm.Msgtoid      = TypeConverter.StrToInt(id);
                pm.New          = 1;
                pm.Postdatetime = Utils.GetDateTime();
                pm.Subject      = commandParam.GetDNTParam("subject").ToString();

                successfulIds += (PrivateMessages.CreatePrivateMessage(pm, 0) > 0) ? (id + ",") : "";
            }
            successfulIds = successfulIds.Length > 0 ? successfulIds.Remove(successfulIds.Length - 1) : successfulIds;

            if (commandParam.Format == FormatType.JSON)
            {
                result = string.Format("\"{0}\"", successfulIds);
            }
            else
            {
                MessageSendResponse nsr = new MessageSendResponse();
                nsr.Result = successfulIds;
                result     = SerializationHelper.Serialize(nsr);
            }
            return(true);
        }
        public async Task <ServiceProviderResponse> SendMessageAsync(
            [FromHeader(Constant.OperationTrackingIdHeader)] string requestId,
            [FromHeader] string account,
            [FromBody] MessageSendRequest request)
        {
            Account currentAccount = null;

            try
            {
                SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Received, $"Request is received");

                currentAccount = await this.ValidateAccount(account);

                Validator.ArgumentNotNull(request, nameof(request));
                Validator.ArgumentNotNull(request.MessageBody, nameof(request.MessageBody));
                Validator.ArgumentNotNullOrEmpty(request.MessageBody.TemplateName, request.MessageBody.TemplateName);

                request.Targets = this.ValidatePhoneNumbers(request.Targets);

                var pack = await this.BuildInputMessageAsync(currentAccount, request, requestId);

                var message = pack.InputMessage;
                SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Message is ready. messageId={message.MessageInfo.MessageId}");

                var signatureQuotaName = string.Format(Constant.SmsSignatureMAUNamingTemplate, pack.Signature.Value);
                SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Empty, $"If quota is enabled, request {request.Targets.Count} for account {account}. Otherwise, ignore");
                await QuotaCheckClient.AcquireQuotaAsync(account, SmsConstant.SmsQuotaName, request.Targets.Count);

                await QuotaCheckClient.AcquireQuotaAsync(account, signatureQuotaName, request.Targets.Count);

                try
                {
                    await this.reportManager.OnMessageSentAsync(account, message, pack.Extension);

                    SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Report is updated. messageId={message.MessageInfo.MessageId}");

                    await this.DispatchMessageAsync(message, pack.Extension, requestId);

                    SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Message is dispatched. messageId={message.MessageInfo.MessageId}");

                    this.metricManager.LogSendSuccess(1, account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory);
                    this.metricManager.LogSendTotal(BillingHelper.GetTotalSegments(message.MessageInfo.MessageBody), account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory);
                    var response = new MessageSendResponse
                    {
                        MessageId = message.MessageInfo.MessageId,
                        SendTime  = message.MessageInfo.SendTime
                    };

                    return(ServiceProviderResponse.CreateJsonResponse(HttpStatusCode.OK, response));
                }
                catch
                {
                    SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Empty, $"If quota is enabled, release {request.Targets.Count} for account {account}. Otherwise, ignore");
                    await QuotaCheckClient.ReleaseQuotaAsync(account, SmsConstant.SmsQuotaName, request.Targets.Count);

                    await QuotaCheckClient.ReleaseQuotaAsync(account, signatureQuotaName, request.Targets.Count);

                    this.metricManager.LogSendFailed(1, account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory);
                    throw;
                }
            }
            catch
            {
                this.metricManager.LogSendFailed(1, account, currentAccount?.SubscriptionId ?? string.Empty, null);
                throw;
            }
        }
Beispiel #9
0
 private void OnMessageSend(object sender, MessageSendResponse message)
 {
     MessageManager.Instance.OnMessageSend( message);
 }