/// <summary>
        /// Batches the subscribe.
        /// </summary>
        /// <param name="recordList">The records</param>
        public virtual listBatchSubscribeOutput BatchSubscribe(IEnumerable<MailChimpEventQueueRecord> recordList)
        {
            if (String.IsNullOrEmpty(_mailChimpSettings.DefaultListId))
                throw new ArgumentException("MailChimp list is not specified");

            var input = new listBatchSubscribeInput();
            //any directive overrides
            input.api_Validate = true;
            input.api_AccessType = EnumValues.AccessType.Serial;
            input.api_OutputType = EnumValues.OutputType.JSON;
            //method parameters
            input.parms.apikey = _mailChimpSettings.ApiKey;
            input.parms.id = _mailChimpSettings.DefaultListId;
            input.parms.double_optin = false;
            input.parms.replace_interests = true;
            input.parms.update_existing = true;
            var batch = new List<Dictionary<string, object>>();

            foreach (var sub in recordList)
            {
                var entry = new Dictionary<string, object>();
                entry.Add("EMAIL", sub.Email);
                batch.Add(entry);
            }

            input.parms.batch = batch;

            //execution
            var cmd = new listBatchSubscribe(input);

            listBatchSubscribeOutput listSubscribeOutput = cmd.Execute();
            return listSubscribeOutput;
        }
Beispiel #2
0
        /// <summary>
        /// Lists the batch subscribe.
        /// </summary>
        /// <param name="apikey">The apikey.</param>
        /// <param name="mailChimpListId">The mail chimp list id.</param>
        /// <param name="subscriberList">The subscriber list.</param>
        /// <returns></returns>
        public MailChimp_Response ListBatchSubscribe(string apikey, string mailChimpListId, List <Table.MailChimpContact> subscriberList)
        {
            //todo:pass
            MailChimp_Response response = new MailChimp_Response();

            response.hdr = new RespHdr();
            string StatusInfo = "";

            var input = GetListBatchSubscribeInput(apikey, mailChimpListId);
            // method parameters
            var batch = new List <Dictionary <string, object> >();

            foreach (Table.MailChimpContact sub_rec in subscriberList)
            {
                Dictionary <string, object> entry = new Dictionary <string, object>();
                entry.Add("EMAIL", sub_rec.Email);
                entry.Add("EMAIL_TYPE", EnumValues.emailType.NotSpecified);
                entry.Add("FNAME", sub_rec.FirstName);
                entry.Add("LNAME", sub_rec.LastName);
                batch.Add(entry);
            }
            input.parms.batch = batch;

            // execution
            var cmd    = new listBatchSubscribe(input);
            var output = cmd.Execute();

            // output, format with user control
            if (output.api_ErrorMessages.Count > 0)
            {
                StringBuilder sbErrDetail = new StringBuilder();
                foreach (var apiErrorMessage in output.api_ErrorMessages)
                {
                    sbErrDetail.AppendLine(string.Format("error code:{0},error msg:{1}", apiErrorMessage.code,
                                                         apiErrorMessage.error));
                    StatusInfo = apiErrorMessage.error;
                }
                string errMsg = string.Format("apikey:{4}mailChimpListId:{5}\r\n{0}{1}{2}{3}", output.api_Request, output.api_Response,     // raw data
                                              sbErrDetail, output.api_ValidatorMessages, apikey, mailChimpListId);
                int Event_id = 5016;
                EventLog.WriteEntry(InfoHubEventLog.LogSource, errMsg, EventLogEntryType.Warning, Event_id, Category);
                response.hdr.Successful = false;
                response.hdr.StatusInfo = "MailChimpMgr: " + StatusInfo;
                return(response);
            }
            else
            {
                //Console.Write(output);
                response.hdr.Successful = true;
                response.hdr.StatusInfo = "";
                return(response);
            }
        }
        /// <summary>
        /// Batches the subscribe.
        /// </summary>
        /// <param name="recordList">The records</param>
        public virtual listBatchSubscribeOutput BatchSubscribe(IEnumerable <MailChimpEventQueueRecord> recordList)
        {
            if (String.IsNullOrEmpty(_mailChimpSettings.DefaultListId))
            {
                throw new ArgumentException("MailChimp list is not specified");
            }

            var input = new listBatchSubscribeInput();

            //any directive overrides
            input.api_Validate   = true;
            input.api_AccessType = EnumValues.AccessType.Serial;
            input.api_OutputType = EnumValues.OutputType.JSON;
            //method parameters
            input.parms.apikey            = _mailChimpSettings.ApiKey;
            input.parms.id                = _mailChimpSettings.DefaultListId;
            input.parms.double_optin      = false;
            input.parms.replace_interests = true;
            input.parms.update_existing   = true;
            var batch = new List <Dictionary <string, object> >();

            foreach (var sub in recordList)
            {
                var entry = new Dictionary <string, object>();
                entry.Add("EMAIL", sub.Email);
                batch.Add(entry);
            }

            input.parms.batch = batch;

            //execution
            var cmd = new listBatchSubscribe(input);

            listBatchSubscribeOutput listSubscribeOutput = cmd.Execute();

            return(listSubscribeOutput);
        }
    /// <summary>
    /// MaqilChimp Integartion
    /// </summary>
    /// <param name="UserType"></param>
    /// <returns></returns>
    private bool AddMailChimpSubscriber(int UserType)
    {
        bool returnVal = false;
        try
        {
            if (chkAgreeReceiveNewsletters.Checked == true)
            {
                listBatchSubscribeInput input = new listBatchSubscribeInput();
                input.api_AccessType = EnumValues.AccessType.Serial; // access
                input.api_OutputType = EnumValues.OutputType.XML; // output
                input.api_MethodType = PerceptiveMCAPI.EnumValues.MethodType.POST;// method
                input.api_Validate = false;// validate
                input.parms.double_optin = false;
                input.parms.replace_interests = true;
                input.parms.update_existing = true;

                input.parms.apikey = WebConfig.MailChimpApiKeyNew;
                input.parms.id = WebConfig.UserNewsLetterListID;

                // ------------------------------ address
                List<Dictionary<string, object>> batch = new List<Dictionary<string, object>>();
                Dictionary<string, object> entry = new Dictionary<string, object>();
                List<interestGroupings> groupings = new List<interestGroupings>();

                entry.Add("EMAIL", txtEmail.Text);
                batch.Add(entry);
                interestGroupings ig = new interestGroupings { name = "Account Status", groups = new List<string> { "New User Contributor" } };
                //if (UserType == 2)
                //{
                //    ig = new interestGroupings { name = "Account Status", groups = new List<string> { "New User Business" } };
                //}
                groupings.Add(ig);

                entry.Add("groupings", groupings);
                batch.Add(entry);
                input.parms.batch = batch;
                // execution
                listBatchSubscribe cmd = new listBatchSubscribe(input);
                listBatchSubscribeOutput output = cmd.Execute();
                //phase-1 enhancement

                if ((output != null) && (output.api_ErrorMessages.Count > 0))
                {
                    string ErrorCode = output.api_ErrorMessages.FirstOrDefault().code;
                    string Error = "Error occured. " + output.api_ErrorMessages.FirstOrDefault().error;

                    messages.InnerHtml = ShowMessage(headertext, ErrorCode + "</br>" + Error, 1);
                    messages.Visible = true;
                    returnVal = false;
                }
                else
                {
                    if (output.result.success_count > 0)
                    {
                        returnVal = true;
                    }
                }
            }

        }
        catch (Exception ex)
        {
            messages.InnerHtml = ShowMessage(headertext, ex.Message.ToString(), 1);
            messages.Visible = true;
            returnVal = false;
        }
        return returnVal;
    }
        public void listBatchSubscribe_method(dynamic o)
        {
            string TagID = o.TagID;
            string TagName = o.TagName;
            string Category = o.Category;

            if (!dicMailChimpLists.ContainsKey(TagName))
                return;

            string MailChimp_List_ID = dicMailChimpLists[TagName].ListId;
            List<string> EMails = this.GetListSubscriberEMails(this.MailChimp_API_Key, MailChimp_List_ID).Distinct().ToList();
            Dictionary<string, string> dicEMails = new Dictionary<string, string>();
            if (EMails.Count > 0)
                dicEMails = EMails.ToDictionary(x => x);

            listBatchSubscribeInput input = new listBatchSubscribeInput();
            // any directive overrides
            input.api_Validate = true;
            input.api_AccessType = EnumValues.AccessType.Serial;
            input.api_OutputType = EnumValues.OutputType.XML;
            // method parameters
            input.parms.apikey = this.MailChimp_API_Key;
            input.parms.id = MailChimp_List_ID;
            input.parms.double_optin = false;
            input.parms.replace_interests = true;
            input.parms.update_existing = true;

            List<Dictionary<string, object>> batch = new List<Dictionary<string, object>>();
            if (Category == "Student")
            {
                List<string> StudentIDs = new List<string>();
                if (this.StudentTags.ContainsKey(TagID))
                    StudentIDs = this.StudentTags[TagID];
                List<dynamic> Students = new List<dynamic>();
                StudentIDs.ForEach(x=>
                {
                    if (dicStudents.ContainsKey(x))
                        Students.Add(dicStudents[x]);
                });
                SyncListMergeVars(this.MailChimp_API_Key, MailChimp_List_ID, new Dictionary<string, string>() { { "ID", "學生系統編號" }, { "NAME", "姓名" }, { "GENDER", "性別" }, { "BIRTHDAY", "生日" }, { "YEAR", "生日之年" }, { "MONTH", "生日之月" }, { "DAY", "生日之日" }, { "ENROLLYEAR", "入學年度" }, { "DEPT", "系所組別" }, { "EMAIL", "電子郵件" }, { "STATUS", "學生狀態" } });
                foreach (dynamic oo in Students)
                {
                    if (oo.Status == "退學" || oo.Status == "刪除")
                        continue;

                    foreach (string email in oo.EMails)
                    {
                        Dictionary<string, object> entry = new Dictionary<string, object>();

                        entry.Add("ID", oo.ID);
                        entry.Add("NAME", oo.Name);
                        entry.Add("GENDER", oo.Gender);
                        entry.Add("BIRTHDAY", oo.Birthday);
                        entry.Add("YEAR", oo.Birthday_Year);
                        entry.Add("MONTH", oo.Birthday_Month);
                        entry.Add("DAY", oo.Birthday_Day);
                        entry.Add("ENROLLYEAR", oo.EnrollYear);
                        entry.Add("DEPT", oo.Dept);
                        entry.Add("EMAIL", email);
                        entry.Add("STATUS", oo.Status);

                        batch.Add(entry);
                        if (dicEMails.ContainsKey(email))
                            dicEMails.Remove(email);
                    }
                }
            }
            else if (Category == "Teacher")
            {
                List<string> TeacherIDs = new List<string>();
                if (this.TeacherTags.ContainsKey(TagID))
                    TeacherIDs = this.TeacherTags[TagID];
                List<dynamic> Teachers = new List<dynamic>();
                TeacherIDs.ForEach(x =>
                {
                    if (this.dicTeachers.ContainsKey(x))
                        Teachers.Add(dicTeachers[x]);
                });
                SyncListMergeVars(this.MailChimp_API_Key, MailChimp_List_ID, new Dictionary<string, string>() { { "ID", "教師系統編號" }, { "NAME", "姓名" }, { "GENDER", "性別" }, { "BIRTHDAY", "生日" }, { "YEAR", "生日之年" }, { "MONTH", "生日之月" }, { "DAY", "生日之日" }, { "EMAIL", "電子郵件" } });
                foreach (dynamic oo in Teachers)
                {
                    if (oo.Status == "256")
                        continue;

                    foreach (string email in oo.EMails)
                    {
                        Dictionary<string, object> entry = new Dictionary<string, object>();

                        entry.Add("ID", oo.ID);
                        entry.Add("NAME", oo.Name);
                        entry.Add("GENDER", oo.Gender);
                        entry.Add("BIRTHDAY", oo.Birthday);
                        entry.Add("YEAR", oo.Birthday_Year);
                        entry.Add("MONTH", oo.Birthday_Month);
                        entry.Add("DAY", oo.Birthday_Day);
                        entry.Add("EMAIL", email);

                        batch.Add(entry);
                        if (dicEMails.ContainsKey(email))
                            dicEMails.Remove(email);
                    }
                }
            }
            input.parms.batch = batch;

            //  取消訂閱不在電子報名單中的訂閱者
            List<string> UnSubscribeEMails = new List<string>();
            foreach (string email in dicEMails.Keys)
                UnSubscribeEMails.Add(email);

            listBatchUnsubscribeOutput output_unsubscribe = this.listBatchUnSubscribe_method(this.MailChimp_API_Key, MailChimp_List_ID, UnSubscribeEMails);

            // 訂閱電子報
            listBatchSubscribe cmd = new listBatchSubscribe(input);
            listBatchSubscribeOutput output = cmd.Execute();
            // output, format with user control
            //if (output.api_ErrorMessages.Count > 0)
            //{
            //    showResults(output.api_Request, output.api_Response, // raw data
            //    output.api_ErrorMessages, output.api_ValidatorMessages); // & errors
            //}
            //else
            //{
            //    show_listBatch1.Display(output);
            //}
        }
 public MailChimpResult Subscribe(List<Dictionary<string, object>> batch, string listId)
 {
     Check.Argument.IsNotNull(batch, "batch");
     Check.Argument.IsNotNullOrEmpty(listId, "listId");
     var input = new listBatchSubscribeInput
                 {
                     api_Validate = true,
                     api_AccessType = EnumValues.AccessType.Serial,
                     api_OutputType = EnumValues.OutputType.JSON,
                     parms =
                         {
                             apikey = MailChimpConfiguration.ApiKey,
                             id = listId,
                             double_optin = DoubleOptin,
                             replace_interests = ReplaceInterests,
                             update_existing = UpdateExisting,
                             batch = batch
                         },
                 };
     var cmd = new listBatchSubscribe(input);
     listBatchSubscribeOutput output = cmd.Execute();
     return new MailChimpResult(output.api_ErrorMessages.Count <= 0, output.api_ErrorMessages);
 }