Twilio
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Wyślij SMS-a
/**
* An example of sending SMS messages from Google Ads Scripts using Twilio.
* See: https://developers.google.com/google-ads/scripts/docs/features/third-party-apis#basic_authentication_samples
* for full details on configuration.
*/
// Supply an email address: If for some reason your Twilio account
// details become invalid or change, this will be used to make sure
// you are notified of failure.
const EMAIL_ADDRESS = 'INSERT_EMAIL_ADDRESS';
// The Twilio phone number or short code, as per the Phone Numbers Dashboard
// https://www.twilio.com/console/phone-numbers/incoming
const TWILIO_SRC_PHONE_NUMBER = 'INSERT_TWILIO_SRC_PHONE_NUMBER';
// Your Twilio Account SID, see: https://www.twilio.com/console
const TWILIO_ACCOUNT_SID = 'INSERT_TWILIO_ACCOUNT_SID';
// Your Twilio API Auth Token, see: https://www.twilio.com/console
const TWILIO_ACCOUNT_AUTHTOKEN = 'INSERT_TWILIO_ACCOUNT_AUTHTOKEN';
/**
* Builds an SMS message for sending with Twilio and sends the message.
* @param {string} dstPhoneNumber The destination number. This is a string as
* telephone numbers may contain '+'s or be prefixed with '00' etc.
* @param {string} message The text message to send.
*/
function sendTwilioSms(dstPhoneNumber, message) {
const request =
buildTwilioMessageRequest(dstPhoneNumber, message);
sendSms(request);
}
/**
* Send an SMS message
* @param {!SmsRequest} request The request object to send
*/
function sendSms(request) {
const retriableErrors = [429, 500, 503];
for (let attempts = 0; attempts < 3; attempts++) {
const response = UrlFetchApp.fetch(request.url, request.options);
const responseCode = response.getResponseCode();
if (responseCode < 400 || retriableErrors.indexOf(responseCode) === -1) {
break;
}
Utilities.sleep(2000 * Math.pow(2, attempts));
}
if (responseCode >= 400 && EMAIL_ADDRESS) {
MailApp.sendEmail(
EMAIL_ADDRESS, 'Error sending SMS Message from Google Ads Scripts',
response.getContentText());
}
}
/**
* Builds a SMS request object specific for the Twilio service.
* @param {string} recipientPhoneNumber Destination number including country
* code.
* @param {string} textMessage The message to send.
* @return {SmsRequest}
*/
function buildTwilioMessageRequest(recipientPhoneNumber, textMessage) {
if (!recipientPhoneNumber) {
throw Error('No "recipientPhoneNumber" specified in call to ' +
'buildTwilioMessageRequest. "recipientPhoneNumber" cannot be empty');
}
if (!textMessage) {
throw Error('No "textMessage" specified in call to ' +
'buildTwilioMessageRequest. "textMessage" cannot be empty');
}
const twilioUri = `https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages`;
const authHeader = 'Basic ' +
Utilities.base64Encode(
TWILIO_ACCOUNT_SID + ':' + TWILIO_ACCOUNT_AUTHTOKEN);
const options = {
muteHttpExceptions: true,
method: 'POST',
headers: {Authorization: authHeader},
payload: {
From: TWILIO_SRC_PHONE_NUMBER,
To: recipientPhoneNumber,
// Twilio only accepts up to 1600 characters
Body: textMessage.substr(0, 1600)
}
};
return {url: twilioUri, options: options};
}
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2024-08-21 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Brak potrzebnych mi informacji"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Zbyt skomplikowane / zbyt wiele czynności do wykonania"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Nieaktualne treści"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"Problem z tłumaczeniem"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Problem z przykładami/kodem"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Inne"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Łatwo zrozumieć"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Rozwiązało to mój problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Inne"
}]
{"lastModified": "Ostatnia aktualizacja: 2024-08-21 UTC."}
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2024-08-21 UTC."]]