private CRMHeaders DoLookup(string url, string authToken, SIPFromHeader from, bool addCallNote, Action<CRMHeaders> callback)
{
try
{
string searchString = null;
string lookupType = null;
if (from.FromName != null && Regex.Match(from.FromName, @"\D").Success)
{
// The From display name has a non-digit character do a name lookup.
lookupType = "name";
searchString = from.FromName.Trim();
}
else if (from.FromName != null)
{
// The From display name is all digits do a phone number lookup.
lookupType = "phonenumber";
searchString = from.FromName.Trim();
}
else if (!Regex.Match(from.FromURI.User, @"\D").Success)
{
// The From URI user is all digits do a phone number lookup.
lookupType = "phonenumber";
searchString = from.FromURI.User.Trim();
}
else
{
// Last resort is to do a SIP URI lookup.
lookupType = "sipaddress";
searchString = from.FromURI.ToAOR();
}
LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact lookup type " + lookupType + " commencing for " + searchString + ".", m_context.Owner));
CRMHeaders result = null;
DateTime startLookup = DateTime.Now;
PersonRequest personRequest = new PersonRequest(url, authToken);
People people = null;
if (lookupType == "name")
{
people = personRequest.GetByName(searchString);
}
else if (lookupType == "phonenumber")
{
people = personRequest.GetByPhoneNumber(searchString);
}
else if (lookupType == "sipaddress")
{
people = personRequest.GetByCustomField("sip_address", searchString);
}
if (people != null && people.PersonList != null && people.PersonList.Count > 0)
{
Person person = people.PersonList[0];
string companyName = null;
if (person.CompanyID != null)
{
CompanyRequest companyRequest = new CompanyRequest(url, authToken);
Company company = companyRequest.GetByID(person.CompanyID.Value);
if (company != null)
{
companyName = company.Name;
}
}
double secondsDuration = DateTime.Now.Subtract(startLookup).TotalSeconds;
if (companyName != null)
{
LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact match " + person.FirstName + " " + person.LastName + " of " + companyName + ", time taken " + secondsDuration.ToString("0.##") + "s.", m_context.Owner));
}
else
{
LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact match " + person.FirstName + " " + person.LastName + ", time taken " + secondsDuration.ToString("0.##") + "s.", m_context.Owner));
}
//m_context.SetCallerDetails(new CRMHeaders(person.FirstName + " " + person.LastName, companyName, person.AvatarURL));
string personName = (!person.LastName.IsNullOrBlank()) ? person.FirstName + " " + person.LastName : person.FirstName;
result = new CRMHeaders(personName, companyName, person.AvatarURL);
if (addCallNote)
{
ThreadPool.QueueUserWorkItem(delegate { AddHighriseCallNote(url, authToken, from, person); });
}
}
else
{
double secondsDuration = DateTime.Now.Subtract(startLookup).TotalSeconds;
LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "No Highrise contact match, time taken " + secondsDuration.ToString("0.##") + "s.", m_context.Owner));
result = new CRMHeaders() { Pending = false, LookupError = "No Highrise contact match." };
}
if (callback != null)
{
callback(result);
}
return result;
}
catch (Exception excp)
{
logger.Error("Exception LookupHighriseContact. " + excp.Message);
LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Error looking up Highrise contact.", m_context.Owner));
var errorResult = new CRMHeaders() { Pending = false, LookupError = "Error looking up Highrise contact." };
if (callback != null)
{
callback(errorResult);
}
return errorResult;
}
}