forked from Montesuma80/3cx-web-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer.cs
105 lines (101 loc) · 4.52 KB
/
answer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Text;
using TCX.Configuration;
using TCX.PBXAPI;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Net;
namespace WebAPI
{
public class answer
{
public static string call(string args1)
{
string connectionAsString(ActiveConnection ac)
{
return $"ID={ac.ID}:CCID={ac.CallConnectionID}:S={ac.Status}:DN={ac.DN.Number}:EP={ac.DN.GetRegistrarContactsEx()}";
}
string mod2 = "";
using (var dn = PhoneSystem.Root.GetDNByNumber(args1))
{
using (var connections = dn.GetActiveConnections().GetDisposer())
{
var alltakenconnections = connections.ToDictionary(x => x, y => y.OtherCallParties);
foreach (var kv in alltakenconnections)
{
var owner = kv.Key;
string agent = "";
foreach (var r in owner.DN.GetRegistrarContactsEx())
{
if (r.UserAgent.Contains("3CXPhone for Windows"))
{
agent = r.Contact;
}
}
string result = connectionAsString(owner);
Console.WriteLine(result);
string CCIDdata = kv.Key.AttachedData.GetValueOrDefault("devcontact");
Console.WriteLine(CCIDdata);
if (result.Contains("S=Ringing") && CCIDdata == agent)
{
int cut = result.IndexOf(':');
string mod = result.Substring(0, cut);
mod2 = mod.Substring(3);
int i = 0;
try
{
i = System.Convert.ToInt32(mod2);
}
catch (FormatException)
{
// the FormatException is thrown when the string text does
// not represent a valid integer.
}
catch (OverflowException)
{
// the OverflowException is thrown when the string is a valid integer,
// but is too large for a 32 bit integer. Use Convert.ToInt64 in
// this case.
}
PhoneSystem.Root.GetByID<ActiveConnection>(i).Answer();
return ("true");
}
else if (result.Contains("S=Ringing") && CCIDdata.Contains("127.0.0.1"))
{
int cut = result.IndexOf(':');
string mod = result.Substring(0, cut);
mod2 = mod.Substring(3);
int i = 0;
try
{
i = System.Convert.ToInt32(mod2);
}
catch (FormatException)
{
// the FormatException is thrown when the string text does
// not represent a valid integer.
}
catch (OverflowException)
{
// the OverflowException is thrown when the string is a valid integer,
// but is too large for a 32 bit integer. Use Convert.ToInt64 in
// this case.
}
PhoneSystem.Root.GetByID<ActiveConnection>(i).Answer();
Logger.WriteLine("Active Connection Type is Mobilephone or WebRTC");
return ("true");
}
else
{
Logger.WriteLine("next");
}
}
}
return ("null");
}
}
}
}