2019-06-06 14:41:16 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-06-06 14:41:16 +00:00
|
|
|
using ASC.Core;
|
|
|
|
using ASC.Core.Tenants;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-06-06 14:41:16 +00:00
|
|
|
using Twilio.TwiML;
|
|
|
|
|
|
|
|
namespace ASC.VoipService.Twilio
|
|
|
|
{
|
|
|
|
public class TwilioResponseHelper
|
|
|
|
{
|
|
|
|
private readonly VoipSettings settings;
|
|
|
|
private readonly string baseUrl;
|
|
|
|
|
2020-08-12 09:58:08 +00:00
|
|
|
private AuthContext AuthContext { get; }
|
|
|
|
private TenantUtil TenantUtil { get; }
|
|
|
|
private SecurityContext SecurityContext { get; }
|
2019-09-17 12:42:32 +00:00
|
|
|
|
|
|
|
public TwilioResponseHelper(
|
2020-08-27 14:01:37 +00:00
|
|
|
VoipSettings settings,
|
|
|
|
string baseUrl,
|
|
|
|
AuthContext authContext,
|
|
|
|
TenantUtil tenantUtil,
|
2019-09-20 15:53:27 +00:00
|
|
|
SecurityContext securityContext)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
|
|
|
this.settings = settings;
|
2019-09-09 12:56:33 +00:00
|
|
|
AuthContext = authContext;
|
2019-09-17 12:42:32 +00:00
|
|
|
TenantUtil = tenantUtil;
|
|
|
|
SecurityContext = securityContext;
|
2019-06-06 14:41:16 +00:00
|
|
|
this.baseUrl = baseUrl.TrimEnd('/') + "/twilio/";
|
|
|
|
}
|
|
|
|
|
2019-08-15 12:04:42 +00:00
|
|
|
public VoiceResponse Inbound(Tuple<Agent, bool> agentTuple)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2019-08-15 13:28:29 +00:00
|
|
|
var agent = agentTuple?.Item1;
|
2020-09-29 11:53:02 +00:00
|
|
|
var anyOnline = agentTuple != null && agentTuple.Item2;
|
2019-06-06 14:41:16 +00:00
|
|
|
var response = new VoiceResponse();
|
2019-08-15 12:04:42 +00:00
|
|
|
|
2019-06-06 14:41:16 +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))
|
|
|
|
{
|
|
|
|
return AddVoiceMail(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anyOnline)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(settings.GreetingAudio))
|
|
|
|
{
|
2020-10-12 19:39:23 +00:00
|
|
|
response.Play(Uri.EscapeUriString(settings.GreetingAudio));
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 19:39:23 +00:00
|
|
|
response.Enqueue(settings.Queue.Name, GetEcho("Enqueue", agent != null), "POST",
|
2019-06-06 14:41:16 +00:00
|
|
|
GetEcho("Wait", agent != null), "POST");
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddVoiceMail(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse Outbound()
|
|
|
|
{
|
|
|
|
return !settings.Caller.AllowOutgoingCalls
|
|
|
|
? new VoiceResponse()
|
|
|
|
: AddToResponse(new VoiceResponse(), settings.Caller);
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse Dial()
|
|
|
|
{
|
|
|
|
return new VoiceResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse Queue()
|
|
|
|
{
|
|
|
|
return new VoiceResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse Enqueue(string queueResult)
|
|
|
|
{
|
|
|
|
return queueResult == "leave" ? AddVoiceMail(new VoiceResponse()) : new VoiceResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse Dequeue()
|
|
|
|
{
|
|
|
|
return AddToResponse(new VoiceResponse(), settings.Caller);
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse Leave()
|
|
|
|
{
|
|
|
|
return AddVoiceMail(new VoiceResponse());
|
|
|
|
}
|
|
|
|
|
2020-10-12 13:52:31 +00:00
|
|
|
public VoiceResponse Wait(string queueTime, string queueSize)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
|
|
|
var response = new VoiceResponse();
|
|
|
|
var queue = settings.Queue;
|
|
|
|
|
|
|
|
if (Convert.ToInt32(queueTime) > queue.WaitTime || Convert.ToInt32(queueSize) > queue.Size) return response.Leave();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(queue.WaitUrl))
|
|
|
|
{
|
|
|
|
var gather = new Gather(method: "POST", action: GetEcho("gatherQueue"));
|
2020-10-12 19:39:23 +00:00
|
|
|
gather.Play(Uri.EscapeUriString(queue.WaitUrl));
|
|
|
|
response.Gather(gather);
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-12 19:39:23 +00:00
|
|
|
response.Pause(queue.WaitTime);
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-10-12 13:52:31 +00:00
|
|
|
public VoiceResponse GatherQueue(string digits, List<Agent> availableOperators)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
|
|
|
var response = new VoiceResponse();
|
|
|
|
|
|
|
|
if (digits == "#") return AddVoiceMail(response);
|
|
|
|
|
|
|
|
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-09-17 12:42:32 +00:00
|
|
|
public VoiceResponse Redirect(string to)
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
|
|
|
if (to == "hold")
|
|
|
|
{
|
|
|
|
return new VoiceResponse().Play(Uri.EscapeUriString(settings.HoldAudio), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-15 13:05:50 +00:00
|
|
|
if (Guid.TryParse(to, out var newCallerId))
|
2019-06-06 14:41:16 +00:00
|
|
|
{
|
2021-08-18 14:04:16 +00:00
|
|
|
SecurityContext.AuthenticateMeWithoutCookie(newCallerId);
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new VoiceResponse().Enqueue(settings.Queue.Name, GetEcho("enqueue"), "POST",
|
|
|
|
GetEcho("wait") + "&RedirectTo=" + to, "POST");
|
|
|
|
}
|
|
|
|
|
|
|
|
public VoiceResponse VoiceMail()
|
|
|
|
{
|
|
|
|
return new VoiceResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
switch (agent.Answer)
|
|
|
|
{
|
|
|
|
case AnswerType.Number:
|
2020-10-12 19:39:23 +00:00
|
|
|
response.Dial(dial.Number(agent.PhoneNumber, method: "POST", url: GetEcho("client")));
|
2019-06-06 14:41:16 +00:00
|
|
|
break;
|
|
|
|
case AnswerType.Client:
|
2020-10-12 19:39:23 +00:00
|
|
|
response.Dial(dial.Client(agent.ClientID, "POST", GetEcho("client")));
|
2019-06-06 14:41:16 +00:00
|
|
|
break;
|
|
|
|
case AnswerType.Sip:
|
2020-10-12 19:39:23 +00:00
|
|
|
response.Dial(dial.Sip(agent.ClientID, method: "POST", url: GetEcho("client")));
|
2019-06-06 14:41:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private VoiceResponse AddVoiceMail(VoiceResponse response)
|
|
|
|
{
|
|
|
|
return string.IsNullOrEmpty(settings.VoiceMail)
|
|
|
|
? response.Say("")
|
|
|
|
: response.Play(Uri.EscapeUriString(settings.VoiceMail)).Record(method: "POST", action: GetEcho("voiceMail"), maxLength: 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetEcho(string action, bool user = true)
|
|
|
|
{
|
|
|
|
var result = baseUrl.TrimEnd('/');
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(action))
|
|
|
|
{
|
|
|
|
result += "/" + action.TrimStart('/');
|
|
|
|
}
|
|
|
|
if (user)
|
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
result += "?CallerId=" + AuthContext.CurrentAccount.ID;
|
2019-06-06 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|