示例#1
0
        private void ProcessFax()
        {
            // Check Fax
            var faxApi = new FaxApi();
            var faxes  = faxApi.GetDocuments();

            foreach (FaxDocument fax in faxes.Entities.Where(x => !(x.Read ?? false)))
            {
                DateTime faxDate    = fax.DateCreated != null ? ((DateTime)fax.DateCreated).ToLocalTime() : DateTime.Now;
                string   fromNumber = fax.CallerAddress.Replace("tel:+", "");
                string   toNumber   = fax.ReceiverAddress.Replace("tel:+", "");

                Log("FAX from " + fax.CallerAddress + " at " + faxDate);

                // Try to look up the email for this fax # in Settings. If it's not found, just move on with the default
                string groupEmail = DefaultRecipientEmail;
                try
                {
                    groupEmail = Properties.Settings.Default["n" + toNumber].ToString();
                }
                catch { }

                // Download the fax file
                string           fileName;
                DownloadResponse faxFile = faxApi.GetDocumentsDocumentIdContent(fax.Id);
                using (var client = new WebClient())
                {
                    // Replace any characters not allowed in a file name
                    string fromNumberSafeString = fromNumber
                                                  .Replace(@"\", "")
                                                  .Replace(@"/", "")
                                                  .Replace(@":", "")
                                                  .Replace(@"*", "")
                                                  .Replace(@"?", "")
                                                  .Replace(@"""", "")
                                                  .Replace(@"<", "")
                                                  .Replace(@">", "")
                                                  .Replace(@"|", "")
                                                  .Replace(@" ", "");

                    fileName = Path.GetTempPath() +
                               "PureCloud_FAX_" + fromNumberSafeString + "_" +
                               faxDate.ToString("yyyyMMdd-HHmmss") + ".pdf";

                    client.DownloadFile(faxFile.ContentLocationUri, fileName);
                }

                // Email to the proper group
                string subject = "Fax from " + fromNumber;
                string body    = ComposeFaxEmail(faxDate, fromNumber, toNumber);
                SendEmail(groupEmail, EmailFromAddress, body, subject, fileName);
                Log("    Sent to " + toNumber + ": " + groupEmail);

                // Mark the fax as read so it won't get sent again next time
                fax.Read = true;
                faxApi.PutDocumentsDocumentId(fax.Id, fax);
            }
        }
 public void Init()
 {
     instance = new FaxApi();
 }