2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
// any third-party rights.
|
|
|
|
//
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
namespace ASC.VoipService.Twilio;
|
|
|
|
|
|
|
|
public class TwilioResponseHelper
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
private readonly VoipSettings _settings;
|
|
|
|
private readonly string _baseUrl;
|
|
|
|
private readonly AuthContext _authContext;
|
|
|
|
private readonly TenantUtil _tenantUtil;
|
|
|
|
private readonly SecurityContext _securityContext;
|
|
|
|
|
|
|
|
public TwilioResponseHelper(
|
|
|
|
VoipSettings settings,
|
|
|
|
string baseUrl,
|
|
|
|
AuthContext authContext,
|
|
|
|
TenantUtil tenantUtil,
|
|
|
|
SecurityContext securityContext)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
_settings = settings;
|
|
|
|
_authContext = authContext;
|
|
|
|
_tenantUtil = tenantUtil;
|
|
|
|
_securityContext = securityContext;
|
|
|
|
_baseUrl = baseUrl.TrimEnd('/') + "/twilio/";
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Inbound(Tuple<Agent, bool> agentTuple)
|
|
|
|
{
|
|
|
|
var agent = agentTuple?.Item1;
|
|
|
|
var anyOnline = agentTuple != null && agentTuple.Item2;
|
|
|
|
var response = new VoiceResponse();
|
2019-08-15 12:04:42 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
if (_settings.WorkingHours != null && _settings.WorkingHours.Enabled)
|
|
|
|
{
|
|
|
|
var now = _tenantUtil.DateTimeFromUtc(DateTime.UtcNow);
|
|
|
|
if (!(_settings.WorkingHours.From <= now.TimeOfDay && _settings.WorkingHours.To >= now.TimeOfDay))
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
return AddVoiceMail(response);
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
2022-02-16 14:07:58 +00:00
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
if (anyOnline)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(_settings.GreetingAudio))
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
response.Play(Uri.EscapeDataString(_settings.GreetingAudio));
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
response.Enqueue(_settings.Queue.Name, GetEcho("Enqueue", agent != null), "POST",
|
|
|
|
GetEcho("Wait", agent != null), "POST");
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
return AddVoiceMail(response);
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Outbound()
|
|
|
|
{
|
|
|
|
return !_settings.Caller.AllowOutgoingCalls
|
|
|
|
? new VoiceResponse()
|
|
|
|
: AddToResponse(new VoiceResponse(), _settings.Caller);
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Dial()
|
|
|
|
{
|
|
|
|
return new VoiceResponse();
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Queue()
|
|
|
|
{
|
|
|
|
return new VoiceResponse();
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Enqueue(string queueResult)
|
|
|
|
{
|
|
|
|
return queueResult == "leave" ? AddVoiceMail(new VoiceResponse()) : new VoiceResponse();
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Dequeue()
|
|
|
|
{
|
|
|
|
return AddToResponse(new VoiceResponse(), _settings.Caller);
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Leave()
|
|
|
|
{
|
|
|
|
return AddVoiceMail(new VoiceResponse());
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Wait(string queueTime, string queueSize)
|
|
|
|
{
|
|
|
|
var response = new VoiceResponse();
|
|
|
|
var queue = _settings.Queue;
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-03-17 15:01:39 +00:00
|
|
|
if (Convert.ToInt32(queueTime) > queue.WaitTime || Convert.ToInt32(queueSize) > queue.Size)
|
|
|
|
{
|
|
|
|
return response.Leave();
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
if (!string.IsNullOrEmpty(queue.WaitUrl))
|
|
|
|
{
|
|
|
|
var gather = new Gather(method: "POST", action: GetEcho("gatherQueue"));
|
|
|
|
gather.Play(Uri.EscapeDataString(queue.WaitUrl));
|
|
|
|
response.Gather(gather);
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
2022-02-16 14:07:58 +00:00
|
|
|
else
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
response.Pause(queue.WaitTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse GatherQueue(string digits, List<Agent> availableOperators)
|
|
|
|
{
|
|
|
|
var response = new VoiceResponse();
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-03-17 15:01:39 +00:00
|
|
|
if (digits == "#")
|
|
|
|
{
|
|
|
|
return AddVoiceMail(response);
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
var oper = _settings.Operators.Find(r => r.PostFix == digits && availableOperators.Contains(r)) ??
|
|
|
|
_settings.Operators.FirstOrDefault(r => availableOperators.Contains(r));
|
|
|
|
|
|
|
|
return oper != null ? AddToResponse(response, oper) : response;
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse Redirect(string to)
|
|
|
|
{
|
|
|
|
if (to == "hold")
|
|
|
|
{
|
|
|
|
return new VoiceResponse().Play(Uri.EscapeDataString(_settings.HoldAudio), 0);
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
|
|
|
|
if (Guid.TryParse(to, out var newCallerId))
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
_securityContext.AuthenticateMeWithoutCookie(newCallerId);
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
return new VoiceResponse().Enqueue(_settings.Queue.Name, GetEcho("enqueue"), "POST",
|
|
|
|
GetEcho("wait") + "&RedirectTo=" + to, "POST");
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public VoiceResponse VoiceMail()
|
|
|
|
{
|
|
|
|
return new VoiceResponse();
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
private VoiceResponse AddToResponse(VoiceResponse response, Agent agent)
|
|
|
|
{
|
|
|
|
var dial = new Dial(method: "POST", action: GetEcho("dial"), timeout: agent.TimeOut, record: agent.Record ? "record-from-answer" : "do-not-record");
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
switch (agent.Answer)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
case AnswerType.Number:
|
|
|
|
response.Dial(dial.Number(agent.PhoneNumber, method: "POST", url: GetEcho("client")));
|
|
|
|
break;
|
|
|
|
case AnswerType.Client:
|
|
|
|
response.Dial(dial.Client(agent.ClientID, "POST", GetEcho("client")));
|
|
|
|
break;
|
|
|
|
case AnswerType.Sip:
|
|
|
|
response.Dial(dial.Sip(agent.ClientID, method: "POST", url: GetEcho("client")));
|
|
|
|
break;
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
return response;
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
private VoiceResponse AddVoiceMail(VoiceResponse response)
|
|
|
|
{
|
|
|
|
return string.IsNullOrEmpty(_settings.VoiceMail)
|
|
|
|
? response.Say("")
|
|
|
|
: response.Play(Uri.EscapeDataString(_settings.VoiceMail)).Record(method: "POST", action: GetEcho("voiceMail"), maxLength: 30);
|
|
|
|
}
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
public string GetEcho(string action, bool user = true)
|
|
|
|
{
|
|
|
|
var result = _baseUrl.TrimEnd('/');
|
2019-06-06 14:41:16 +00:00
|
|
|
|
2022-02-16 14:07:58 +00:00
|
|
|
if (!string.IsNullOrEmpty(action))
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
result += "/" + action.TrimStart('/');
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
2022-02-16 14:07:58 +00:00
|
|
|
if (user)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2022-02-16 14:07:58 +00:00
|
|
|
result += "?CallerId=" + _authContext.CurrentAccount.ID;
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
2022-02-16 14:07:58 +00:00
|
|
|
|
|
|
|
return result;
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
}
|