-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-PhoneCall.ps1
150 lines (120 loc) · 10.8 KB
/
Get-PhoneCall.ps1
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function Get-PhoneCall
{
<#
.Synopsis
Gets information about phone calls
.Description
Gets information about phone calls sent to or from any Twilio Number
.Example
Get-PhoneCall
.Link
Send-PhoneCall
.Link
Get-Web
.Link
http://twilio.com/
#>
[OutputType([PSObject])]
param(
# The call identifier
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('Sid')]
[string]
$CallSid,
# The Twilio credential
[Management.Automation.PSCredential]
$Credential,
# The status of the phone call
[ValidateSet("queued", "ringing", "in-progress", "completed", "failed", "busy", "no-answer")]
[string]$Status,
# The number the phone call was sent to
[string]$To,
# The number the phone call came from
[string]$From,
# Twilio connection settings
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string[]]
$Setting = @("TwilioAccountKey", "TwilioAccountSecret"),
# If set, will include the recording information with the call
[Switch]$IncludeRecording,
# If set, will include transcription information with the call
[Switch]$IncludeTranscript
)
process {
#region Resolve the Twilio Credential
if (-not $Credential -and $Setting) {
if ($setting.Count -eq 1) {
$userName = Get-WebConfigurationSetting -Setting "${Setting}_UserName"
$password = Get-WebConfigurationSetting -Setting "${Setting}_Password"
} elseif ($setting.Count -eq 2) {
$userName = Get-secureSetting -Name $Setting[0] -ValueOnly
$password= Get-secureSetting -Name $Setting[1] -ValueOnly
}
if ($userName -and $password) {
$password = ConvertTo-SecureString -AsPlainText -Force $password
$credential = New-Object Management.Automation.PSCredential $username, $password
} elseif ((Get-SecureSetting -Name "$Setting" -ValueOnly | Select-Object -First 1)) {
$credential = (Get-SecureSetting -Name "$Setting" -ValueOnly | Select-Object -First 1)
}
}
if (-not $Credential) {
Write-Error "No Twilio Credential provided. Use -Credential or Add-SecureSetting TwilioAccountDefault -Credential (Get-Credential) first"
return
}
#endregion Resolve the Twilio Credential
#region Define the default web parameters
$getWebParams = @{
WebCredential=$Credential
Url="https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/Calls/?"
AsXml =$true
UseWebRequest = $true
}
if ($psBoundParameters.Status) {
$getWebParams.Url += "&Status=$status"
}
if ($psBoundParameters.To) {
$getWebParams.Url += "&To=$to"
}
if ($psBoundParameters.From) {
$getWebParams.Url += "&From=$from"
}
if ($callSid) {
$getWebParams.Url =
"https://api.twilio.com/2010-04-01/Accounts/$($Credential.GetNetworkCredential().Username.Trim())/Calls/$CallSid"
}
#endregion Define the default web parameters
do {
# Get the response from Twilio
$twiResponse = Get-Web @getwebParams |
Select-Object -ExpandProperty TwilioResponse
#region Extract out call information
$twiResponse |
Select-Object -ExpandProperty Calls |
Select-Object -ExpandProperty Call |
ForEach-Object {
$item = $_
$_.pstypenames.clear()
$_.pstypenames.add('Twilio.PhoneCall')
if ($includeRecording) {
$recording =
Get-TwilioRecording -CallSid $_.CallSid -Credential $credential -Asmp3 -errorAction SilentlyContinue
$_ |
Add-Member NoteProperty Recording $recording -Force
}
if ($includeTranscript) {
$recording=
Get-TwilioRecording -CallSid $item.Sid -Credential $credential -errorAction SilentlyContinue
if ($recording) {
Get-TwilioTranscription -RecordingSid $recording.Sid -Credential $credential
}
}
$_
}
#endregion Extract out call information
if ($twiResponse.Calls.NextPageUri) {
# If there's a next page, slightly change the URL
$getWebParams.Url="https://api.twilio.com" + $twiResponse.Calls.NextPageUri
}
} while ($twiResponse.Calls.NextPageUri)
}
}