PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.AppServer.DialPlan/DialPlanFacades/DialPlanCRMFacade.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 161 lines | 95 code | 16 blank | 50 comment | 12 complexity | 02400c6b887a38a76f2f14a0ded8b40d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: DialPlanCRMFacade.cs
  3. //
  4. // Description:
  5. // Facade class to allow easy integration with 3rd party CRM systems from dial plan
  6. // scripts.
  7. //
  8. // Author(s):
  9. // Aaron Clauson
  10. //
  11. // History:
  12. // 04 Feb 2011 Aaron Clauson Created.
  13. //
  14. // License:
  15. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  16. //
  17. // Copyright (c) 2011 Aaron Clauson (aaron@sipsorcery.com), SIP Sorcery Pty Ltd
  18. // All rights reserved.
  19. //
  20. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  21. // the following conditions are met:
  22. //
  23. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  24. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  25. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of SIP Sorcery Ltd.
  26. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  27. // prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  30. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  32. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  33. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. // POSSIBILITY OF SUCH DAMAGE.
  36. // ============================================================================
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Diagnostics;
  40. using System.Linq;
  41. using System.Text;
  42. using System.Threading;
  43. using SIPSorcery.CRM.ThirtySevenSignals;
  44. using SIPSorcery.SIP;
  45. using SIPSorcery.SIP.App;
  46. using SIPSorcery.Sys;
  47. using log4net;
  48. namespace SIPSorcery.AppServer.DialPlan
  49. {
  50. public class DialPlanCRMFacade
  51. {
  52. private static ILog logger = AppState.logger;
  53. private SIPMonitorLogDelegate LogToMonitor;
  54. private DialPlanContext m_context;
  55. public DialPlanCRMFacade(SIPMonitorLogDelegate logDelegate, DialPlanContext context)
  56. {
  57. LogToMonitor = logDelegate;
  58. m_context = context;
  59. }
  60. /// <summary>
  61. /// Looks up a person contact in the 37 Signals Highrise application.
  62. /// </summary>
  63. /// <param name="url">The URL of the Highrise account to attempt the lookup on.</param>
  64. /// <param name="authToken">The auth token for the Highrise account to attempt the lookup with.</param>
  65. /// <param name="from">The SIP from header of the incoming call to attempt to match on.</param>
  66. /// <param name="addCallNote">If true it indicates a Highrise note should be created if a matching contact is found.</param>
  67. public void LookupHighriseContact(string url, string authToken, SIPFromHeader from, bool addCallNote)
  68. {
  69. LookupHighriseContact(url, authToken, from.FromName, addCallNote);
  70. }
  71. /// <summary>
  72. /// Looks up a person contact in the 37 Signals Highrise application.
  73. /// </summary>
  74. /// <param name="url">The URL of the Highrise account to attempt the lookup on.</param>
  75. /// <param name="authToken">The auth token for the Highrise account to attempt the lookup with.</param>
  76. /// <param name="name">The name of the person to attempt a match on.</param>
  77. /// <param name="addCallNote">If true it indicates a Highrise note should be created if a matching contact is found.</param>
  78. public void LookupHighriseContact(string url, string authToken, string name, bool addCallNote)
  79. {
  80. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Looking up Highrise contact on " + url + " for " + name + ".", m_context.Owner));
  81. m_context.SetCallerDetails(new CRMHeaders() { Pending = true });
  82. ThreadPool.QueueUserWorkItem(delegate { DoLookup(url, authToken, name, addCallNote); });
  83. }
  84. private void DoLookup(string url, string authToken, string name, bool addCallNote)
  85. {
  86. try
  87. {
  88. PersonRequest personRequest = new PersonRequest(url, authToken);
  89. People people = personRequest.GetByName(name);
  90. if (people != null && people.PersonList != null && people.PersonList.Count > 0)
  91. {
  92. Person person = people.PersonList[0];
  93. string companyName = null;
  94. if (person.CompanyID != null)
  95. {
  96. CompanyRequest companyRequest = new CompanyRequest(url, authToken);
  97. Company company = companyRequest.GetByID(person.CompanyID.Value);
  98. if (company != null)
  99. {
  100. companyName = company.Name;
  101. }
  102. }
  103. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact match " + person.FirstName + " " + person.LastName + " of " + companyName + ".", m_context.Owner));
  104. m_context.SetCallerDetails(new CRMHeaders(person.FirstName + " " + person.LastName, companyName, person.AvatarURL));
  105. if (addCallNote)
  106. {
  107. ThreadPool.QueueUserWorkItem(delegate { AddHighriseCallNote(url, authToken, person); });
  108. }
  109. }
  110. else
  111. {
  112. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "No Highrise contact match.", m_context.Owner));
  113. m_context.SetCallerDetails(new CRMHeaders() { Pending = false, LookupError = "No Highrise contact match." });
  114. }
  115. }
  116. catch (Exception excp)
  117. {
  118. logger.Error("Exception LookupHighriseContact. " + excp.Message);
  119. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Error looking up Highrise contact.", m_context.Owner));
  120. m_context.SetCallerDetails(new CRMHeaders() { Pending = false, LookupError = "Error looking up Highrise contact." });
  121. }
  122. }
  123. private void AddHighriseCallNote(string url, string authToken, Person person)
  124. {
  125. try
  126. {
  127. NoteRequest request = new NoteRequest(url, authToken);
  128. string result = request.CreateNoteForPerson(person.ID, "Called at " + DateTime.UtcNow.ToString("dd MMM yyyy HH:mm:ss") + " UTC.");
  129. string personName = person.FirstName + " " + person.LastName;
  130. if(result != null)
  131. {
  132. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise call received note added for " + personName + ".", m_context.Owner));
  133. }
  134. else
  135. {
  136. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Failed to add Highrise call received note for " + personName + ".", m_context.Owner));
  137. }
  138. }
  139. catch (Exception excp)
  140. {
  141. LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Exception attempting to add Highrise call received note.", m_context.Owner));
  142. }
  143. }
  144. }
  145. }