-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.php
199 lines (181 loc) · 6.71 KB
/
app.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
use Telnyx\AvailablePhoneNumber;
use Telnyx\NumberOrder;
use Telnyx\PhoneNumber;
require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$TELNYX_API_KEY = $_ENV["TELNYX_API_KEY"];
Telnyx\Telnyx::setApiKey($TELNYX_API_KEY);
/**
* Prompts user for input from the console.
* @param string $prompt Sentence to display to user
* @return string Trimmed response in *lowercase* from user.
*/
function getUserInput(string $prompt) : string {
echo $prompt;
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$input = trim($line);
fclose($handle);
return strtolower($input);
}
/**
* Filter function to parse from an object containing a "phone_number" element
*
* @param object $elem Inner object from Telnyx Response
* @return string The e164 "+19198675309" style phone number
*/
function filterPhoneNumber(object $elem) : string {
$phoneNumber = (string) $elem["phone_number"];
return $phoneNumber;
}
/**
* Search the available numbers by area code. Will die on error.
* @param string $areaCode The area code to search Telnyx inventory
* @return array An array of e164 "+19198675309" formatted phone_number strings from inventory
*/
function searchAvailableNumbersByAreaCode(string $areaCode) : array {
try {
$telnyxResponse = AvailablePhoneNumber::All([
"filter[national_destination_code]" => $areaCode,
"filter[best_effort]" => true,
"filter[limit]" => 2
]);
$availableNumbers = array_map("filterPhoneNumber", $telnyxResponse["data"]);
if (empty($availableNumbers)) {
echo "No results found for {$areaCode}, quitting\n";
exit;
}
return $availableNumbers;
} catch (Exception $e){
echo "Caught exception: ", $e->getMessage(), "\n";
exit;
}
}
/**
* Prompt user via console to order a phone number and orders or passes
* @param string $phoneNumber e164 formatted phone number "+19198675309" to display and order
* @return PhoneNumberOrder object containing the order id and phone number
*/
function promptAndOrder(string $phoneNumber) : ?PhoneNumberOrder {
$shouldOrder = getUserInput("(y/n) Would you like to order {$phoneNumber}?\n");
if ($shouldOrder != "y"){
echo "Ok, not ordering \n";
return null;
}
else {
$orderInformation = orderNumber($phoneNumber);
return $orderInformation;
}
}
/**
* Class PhoneNumberOrder holds information about a Telnyx Order
*/
class PhoneNumberOrder {
public string $orderId;
public string $phoneNumber;
public string $phoneNumberStatus;
public string $phoneNumberId;
/**
* orderResponse constructor.
* @param string $orderId *orderId* from the Telnyx order
* @param string $phoneNumber the e164 formatted "+19198675309" phone number
*/
function __construct(string $orderId, string $phoneNumber){
$this->orderId = $orderId;
$this->phoneNumber = $phoneNumber;
}
}
/**
* Order a single phone number from Telynx.
* @param string $phoneNumber The desired phone number to order in e164 format "+19198675309"
* @return PhoneNumberOrder An object with "orderId" and "phoneNumber" in e164 format "+19198675309" from the order
*/
function orderNumber(string $phoneNumber) : ?PhoneNumberOrder {
try {
$requestBody = [
"phone_numbers" => [
["phone_number" => $phoneNumber]
]
];
$telnyxResponse = NumberOrder::Create($requestBody);
$orderInformation = new PhoneNumberOrder(
$telnyxResponse["id"],
$telnyxResponse["phone_numbers"][0]["phone_number"]);
echo "Order for phone number: {$orderInformation->phoneNumber} is {$telnyxResponse["status"]}\n";
return $orderInformation;
} catch (Exception $e){
if ($e->getTelnyxCode() === "10015") {
echo "Number {$phoneNumber} is not available for ordering\n";
return null;
}
else {
echo "Caught exception: ", $e->getMessage(), "\n";
exit;
}
}
}
/**
* Fetches the status and telnyx-id of the phone number by e164 string "+19198675309"
* @param PhoneNumberOrder $orderInformation Response from the orderNumber function
* @return PhoneNumberOrder updates the phone number status and the phone number id
*/
function waitForPhoneNumberToBeActive(PhoneNumberOrder $orderInformation): PhoneNumberOrder{
try {
$telnyxResponse = PhoneNumber::Retrieve($orderInformation->phoneNumber);
$orderStatus = (string) $telnyxResponse["status"];
$orderInformation->phoneNumberId = (string) $telnyxResponse["id"];
while ($orderStatus == 'purchase-pending') {
sleep(1);
$telnyxResponse = PhoneNumber::Retrieve($orderInformation->phoneNumber);
$orderStatus = (string) $telnyxResponse["status"];
}
$orderInformation->phoneNumberStatus = $orderStatus;
echo "Phone Number: {$orderInformation->phoneNumber} with id: {$orderInformation->phoneNumberId} status is: {$orderInformation->phoneNumberStatus}\n";
return $orderInformation;
} catch (Exception $e){
echo "Caught exception: ", $e->getMessage(), "\n";
exit;
}
}
/**
* Adds tags to phone numbers
* @param PhoneNumberOrder $orderInformation
* @param String $tag
* @return PhoneNumber
*/
function addTagToPhoneNumber(PhoneNumberOrder $orderInformation, String $tag): PhoneNumber {
try {
$phoneNumberId = $orderInformation->phoneNumberId;
PhoneNumber::Update($phoneNumberId, ["tags" => [$tag]]);
$telnyxResponse = PhoneNumber::Retrieve($orderInformation->phoneNumberId);
return $telnyxResponse;
} catch (Exception $e){
echo "Caught exception: ", $e->getMessage(), "\n";
exit;
}
}
/**
* Prompts the user for a tag for each phone number
* @param PhoneNumberOrder $orderInformation
* @return String tag for the new phone number
*/
function promptForTags(PhoneNumberOrder $orderInformation): String{
$phoneNumber = $orderInformation->phoneNumber;
$tag = getUserInput("Tag for {$phoneNumber}?\n");
return $tag;
}
/**
* Entry point for the program
*/
function Main(){
$areaCode = getUserInput("Which NPA (area code) would you like to search?: ");
$availableNumbers = searchAvailableNumbersByAreaCode($areaCode);
$ordersInformation = array_filter(array_map("promptAndOrder", $availableNumbers));
$updatedOrdersInformation = array_map("waitForPhoneNumberToBeActive", $ordersInformation);
$tags = array_map("promptForTags", $updatedOrdersInformation);
$telnyxResponses = array_map("addTagToPhoneNumber", $updatedOrdersInformation, $tags);
print_r($telnyxResponses);
}
Main();