Update Webhooks in Bulk
Overview
If you have a lot of phone numbers, it could be tedious to change the webhook for message or call handling for all of them one by one. You can use this script (or one like it in another SDK) in order to change the webhook for all of the numbers at one time!
This code will list all of the numbers on your account and compare them to the list of numbers whose webhooks you want to update. For all the numbers that also exist in your CSV, the webhook will be updated sequentially.
Full code example: Update Webhooks in Bulk
- Python
- Node
from signalwire.rest import Client as signalwire_client
import csv
import requests
from requests.auth import HTTPBasicAuth
SpaceURL = 'YourSpace.signalwire.com'
ProjectID = "Some-Alphanumeric-String"
AuthToken = 'Some-Alphanumeric-String'
WebhookPath = 'https://example.com/message_handler'
PathToCSV = 'Path-To-CSV-On-Your-Computer'
client = signalwire_client(ProjectID, AuthToken, signalwire_space_url=SpaceURL)
incoming_phone_numbers = client.incoming_phone_numbers.list()
print("Total Numbers -- " + str(len(incoming_phone_numbers)))
results = []
with open(PathToCSV, 'r', encoding='utf-8-sig') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if "+" not in row[0]:
results.append("+" + row[0])
else:
results.append(row[0])
print(results)
for record in incoming_phone_numbers:
if record.phone_number in results:
print("update number -- " + record.phone_number)
response = requests.put('https://' + SpaceURL + '/api/relay/rest/phone_numbers/' + record.sid,
params={'call_handler': 'laml_webhooks',
'message_request_url': WebhookPath}
, auth=HTTPBasicAuth(ProjectID, AuthToken))
print(response)
const { RestClient } = require('@signalwire/compatibility-api');
const fs = require('fs');
const csv = require('@fast-csv/parse');
const fetch = require('node-fetch');
// TODO: Update with your own credentials
const spaceURL = 'YOURSPACENAME.signalwire.com'
const projectID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX"
const authToken = 'PTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
const authenticationString = projectID + ":" + authToken;
const webhookPath = 'https://example.com/message_handler'
const pathToCSV = 'webhooks.csv'
const client = RestClient(
projectID,
authToken,
{ signalwireSpaceUrl: spaceURL });
client.incomingPhoneNumbers
.list()
.then(numbersList => {
console.log("Total Numbers: " + numbersList.length)
numbersList.forEach((number) => {
csv.parseFile(pathToCSV)
.on('error', error => console.error(error))
.on('data', row => {
let csvNumber = row[0];
// Correct number format from CSV if necessary
if (csvNumber.indexOf('+') == -1) {
csvNumber = "+" + csvNumber;
}
if (number.phoneNumber == csvNumber) {
console.log("Update Number: " + csvNumber)
const url = "https://" + spaceURL + "/api/relay/rest/phone_numbers/" + number.sid;
const options = {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Basic ' + new Buffer.from(authenticationString).toString('base64')
},
body: JSON.stringify({
message_handler: 'laml_webhooks',
message_request_url: webhookPath
})
};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
}
})
});
})
.catch(err => console.error('error:' + err))