Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dsTw5 authored Apr 23, 2024
1 parent 1952fd2 commit a04f3d2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 78 deletions.
158 changes: 90 additions & 68 deletions transfers/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,8 @@
rel="icon"
href="https://twilio-labs.github.io/function-templates/static/v1/favicon.ico"
/>
<link
rel="stylesheet"
href="https://twilio-labs.github.io/function-templates/static/v1/ce-paste-theme.css"
/>
<link rel="stylesheet" href="https://twilio-labs.github.io/function-templates/static/v1/ce-paste-theme.css" />
<link rel="stylesheet" href="styles.css" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/build/css/intlTelInput.min.css"
integrity="sha256-xpVuhxDPR39wFEQDha4W7kuMx+z9Av3dTS8MbH/RWEU="
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/build/js/intlTelInput.min.js"
integrity="sha256-uPbemOnf3P4eaeLHebLwPC71YRbu3WNBvO4ibYeBnGs="
crossorigin="anonymous"
></script>
<style>
.success {
border-color: #bce8f1;
Expand Down Expand Up @@ -95,7 +81,7 @@ <h1>
<p>
Before you start, make sure that the checkbox "Add my Twilio
Credentials (ACCOUNT_SID) and (AUTH_TOKEN) to ENV" is checked in the
"Environmental Variables" section of this Function. If it's not checked,
"Environmental Variables" of this Function. If it's not checked,
enable it and click "Deploy All" at the bottom.
</p>
<h2>
Expand All @@ -114,40 +100,32 @@ <h2>
<a
target="_blank"
href="https://console.twilio.com/us1/develop/functions/services"
>Function</a
>
>Function</a>
- open the Function and click on "Environment Variables" in the bottom
left corner, and change the value of Password. Click on "Deploy All"
button afterwards in the bottom left corner.
left corner, and change the value of Password. Click on "Deploy All" button afterwards in the bottom left corner. <br />The default
Password is <i><b>1</b></i>.
</p>
Password:<input id="password" type="password" name="password" />
<br />
<div><p>You are working with account:</p></div>
<div id="masking"></div>
<div><button onclick="unmask()">Show Account</button></div>
<div
id="status_acc"
style="
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
"
></div>
<div id="status_acc" style="color: #a94442; background-color: #f2dede; border-color: #ebccd1;"></div>
<div><h3>Fetch Number SID</h3></div>
<div>
<form id="fetchSID" action="javascript:number_fetch()">
<p>
In the below field, enter the number to get the SID that you need
to use to transfer it. The search is done on your main account and
its subaccounts automatically. <br /><b>Be carefull</b>, if you
insert an incomplete number, the system will search for a number
on your accounts (main account and subaccounts) that starts with
what you inserted. <br />
In the below field, enter the number to get the SID that you need to use to transfer
it. The search is done on your main account and its
subaccounts automatically. <br /><b>Be carefull</b>, if you insert an
incomplete number, the system will search for a number on your
accounts (main account and subaccounts) that starts with what you
inserted. <br />
For example, if you have a number +123456789, and you insert
+1234567, the details of the complete number will be returned.
Make sure you check what is the number in the result.
</p>
<input id="fphone" type="text" name="fphone" />
<input id="fphone" type="text" name="fphone" oninput="checkFormat()" />
<br />
<input id="tbutton" type="submit" value="Fetch" />
</form>
Expand All @@ -172,9 +150,7 @@ <h2>
href="https://support.twilio.com/hc/en-us/articles/223135327-Moving-Twilio-Phone-Numbers-to-another-Twilio-Account"
>FAQ page</a
>. <br />
<b>Hosted Numbers should not</b> be transferred with this tool or
the API. Contact Twilio Support if you want to transfer a Hosted
number.
<b>Hosted Numbers should not</b> be transferred with this tool or the API. Contact Twilio Support if you want to transfer a Hosted number.
</p>
<p>
Enter the SID of the phone number you would like to transfer
Expand Down Expand Up @@ -212,14 +188,19 @@ <h2>
Insert the Address SID (depends on regulatory requirements of the
number):
</p>
<input id="addressSID" type="text" name="addressSID" value="" />
<input
id="addressSID"
type="text"
name="addressSID"
value=""
/>
<input id="transfer" type="submit" value="Transfer" />
</form>
<div class="alert alert-info" style="display: none"></div>
<div class="alert alert-error" style="display: none"></div>
</div>
<div>
<!-- EDIT_CODE_V2 -->
<!-- EDIT_CODE -->
</div>
</div>
</main>
Expand All @@ -232,6 +213,16 @@ <h2>
const success = document.querySelector(".alert-success");
const warning = document.querySelector(".alert-warning");


async function checkFormat(){
if(!/^\+\d+$/.test(document.getElementById("fphone").value)){
document.getElementById("fphone").style.background = "Tomato";
}
else {
document.getElementById("fphone").style.background = "";
}
}

async function transfer() {
let alertConfirm =
"You requested to transfer a number. If there is a Whatsapp configuration on the number, it will be lost on transfer. If there are opt-outs and/or A2P registrations (US LongCodes only) on the number, those will also be lost. You will need to setup Opt-Outs and re-register the numbers on the new account before they will work. Please find more information in the linked FAQ page. \n\n Do you want to proceed with the transfer?";
Expand Down Expand Up @@ -310,46 +301,71 @@ <h2>
let aresponse = null;
let fresponse = null;
let json2 = null;
let ajson = null;
let ajson = [];
let sid = null;
let asid = null;
let phoneNumber = null;
let phoneNumber = document.getElementById("fphone").value;
let numStatus = null;
let addReq = null;
let insertedNumber = phoneNumber;
let subPage = 0;
let subPageToken = '';
let subPageAll = 0;
let subData = null;
let aPageJson = null;

const password = document.getElementById("password").value;
phoneNumber = document.getElementById("fphone").value;

try {
success.style.display = "none";
warning.style.display = "none";
status_acc.style.display = "none";

aresponse = await fetch("./list_accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": password,
while (subPageAll === 0) {

subData = {
pageSize: 20,
page: subPage,
pageToken: decodeURIComponent(subPageToken),
};
aresponse = await fetch("./list_accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: password,
},
body: JSON.stringify(subData),
});
console.log("return from Acc function");
if (aresponse.status === 200) {
aPageJson = await aresponse.json();
if (aPageJson.er === 0) {
warning.style.display = "";
status_acc.style.display="none";
success.style.display = "none";
warning.innerHTML = `Wrong password.`;
return;
}
if (aPageJson[aPageJson.length-1] !== "end") {
subPageToken = aPageJson[aPageJson.length-1];
console.log(subPageToken);
} else {
subPageAll = 1;
subPageToken = '';
}
subPage += 1;
aPageJson.pop();
console.log(aPageJson);
for(let z in aPageJson){
ajson.push(aPageJson[z]);
}
console.log(ajson);
} else {
warning.style.display = "";
warning.innerHTML = `Something went wrong with getting the list of accounts:<br /> Server error is ${aresponse.message}<br /> Server status is ${aresponse.status}`;
return;
}
}
});
console.log("return from Acc function");
console.log(aresponse);
if (aresponse.status === 200) {
ajson = await aresponse.json();
console.log(ajson);

if (ajson.er === 0) {
warning.style.display = "";
status_acc.style.display="none";
success.style.display = "none";
warning.innerHTML = `Wrong password.`;
return;
}
} else {
warning.style.display = "";
warning.innerHTML = `Something went wrong with getting the list of accounts:<br /> Server error is ${aresponse.message}<br /> Server status is ${aresponse.status}`;
return;
}

for (a = 0; a < ajson.length; a++) {
fdata = {
Expand Down Expand Up @@ -397,6 +413,12 @@ <h2>
warning.innerHTML = `<pre> ${JSON.stringify(json2, null, 2)}</pre>`;
} else {
if (fresponse.status === 200) {
if (insertedNumber !== phoneNumber.substring(0, insertedNumber.length,)){
warning.style.display = "";
success.style.display = "none";
warning.innerHTML = `Number not found.`;
return;
}
success.style.display = "";
success.innerHTML = `<pre>Number ${JSON.stringify(
phoneNumber,
Expand Down Expand Up @@ -473,4 +495,4 @@ <h2>
}
</script>
</body>
</html>
</html>
42 changes: 32 additions & 10 deletions transfers/functions/list_accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,38 @@ exports.handler = async function (context, event, callback) {
}

try {
sub = await client.api.v2010.accounts.list().then((accounts) =>
accounts.forEach((a) => {
allAccounts[i] = a.sid;
i += 1;
})
);

response.setStatusCode(200);
response.setBody(allAccounts);
return callback(null, response);
if (event.pageSize > 0) {
sub = await client.api.v2010.accounts
.page({
pageSize: event.pageSize,
Page: event.page,
pageToken: event.pageToken,
})
.then((accounts) => {
for (a = 0; a < accounts.instances.length; a++) {
allAccounts[i] = accounts.instances[a].sid;
i += 1;
}
if (accounts.nextPageUrl !== undefined) {

Check failure on line 31 in transfers/functions/list_accounts.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14)

Unexpected negated condition

Check failure on line 31 in transfers/functions/list_accounts.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16)

Unexpected negated condition
allAccounts[i] = accounts.nextPageUrl.split('PageToken=')[1];
} else {
allAccounts[i] = 'end';
}
});
response.setStatusCode(200);
response.setBody(allAccounts);
return callback(null, response);
} else {

Check failure on line 40 in transfers/functions/list_accounts.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14)

Unnecessary 'else' after 'return'

Check failure on line 40 in transfers/functions/list_accounts.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16)

Unnecessary 'else' after 'return'
sub = await client.api.v2010.accounts.list().then((accounts) =>
accounts.forEach((a) => {
allAccounts[i] = a.sid;
i += 1;
})
);
response.setStatusCode(200);
response.setBody(allAccounts);
return callback(null, response);
}
} catch (error) {
console.error(error.message);
response.setStatusCode(error.status || 400);
Expand Down

0 comments on commit a04f3d2

Please sign in to comment.