Twilio
使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。
发送短信
/** * 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}; }
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis script enables sending SMS messages directly from Google Ads scripts using the Twilio API.\u003c/p\u003e\n"],["\u003cp\u003eIt requires essential Twilio account details like Account SID, Auth Token, and the designated Twilio phone number for sending messages.\u003c/p\u003e\n"],["\u003cp\u003eThe script includes error handling, notifying a specified email address in case of sending failures.\u003c/p\u003e\n"],["\u003cp\u003eIt limits SMS messages to 1600 characters, adhering to Twilio's message length constraints.\u003c/p\u003e\n"],["\u003cp\u003eThe script facilitates building and sending an SMS request and has retry mechanisms for handling temporary errors.\u003c/p\u003e\n"]]],[],null,["# Twilio\n\nSend a SMS message\n------------------\n\n```gdscript\n/**\n * An example of sending SMS messages from Google Ads Scripts using Twilio.\n * See: https://developers.google.com/google-ads/scripts/docs/features/third-party-apis#basic_authentication_samples\n * for full details on configuration.\n */\n\n// Supply an email address: If for some reason your Twilio account\n// details become invalid or change, this will be used to make sure\n// you are notified of failure.\nconst EMAIL_ADDRESS = 'INSERT_EMAIL_ADDRESS';\n\n// The Twilio phone number or short code, as per the Phone Numbers Dashboard\n// https://www.twilio.com/console/phone-numbers/incoming\nconst TWILIO_SRC_PHONE_NUMBER = 'INSERT_TWILIO_SRC_PHONE_NUMBER';\n\n// Your Twilio Account SID, see: https://www.twilio.com/console\nconst TWILIO_ACCOUNT_SID = 'INSERT_TWILIO_ACCOUNT_SID';\n\n// Your Twilio API Auth Token, see: https://www.twilio.com/console\nconst TWILIO_ACCOUNT_AUTHTOKEN = 'INSERT_TWILIO_ACCOUNT_AUTHTOKEN';\n\n/**\n * Builds an SMS message for sending with Twilio and sends the message.\n * @param {string} dstPhoneNumber The destination number. This is a string as\n * telephone numbers may contain '+'s or be prefixed with '00' etc.\n * @param {string} message The text message to send.\n */\nfunction sendTwilioSms(dstPhoneNumber, message) {\n const request =\n buildTwilioMessageRequest(dstPhoneNumber, message);\n sendSms(request);\n}\n\n/**\n * Send an SMS message\n * @param {!SmsRequest} request The request object to send\n */\nfunction sendSms(request) {\n const retriableErrors = [429, 500, 503];\n\n for (let attempts = 0; attempts \u003c 3; attempts++) {\n const response = UrlFetchApp.fetch(request.url, request.options);\n const responseCode = response.getResponseCode();\n\n if (responseCode \u003c 400 || retriableErrors.indexOf(responseCode) === -1) {\n break;\n }\n Utilities.sleep(2000 * Math.pow(2, attempts));\n }\n\n if (responseCode \u003e= 400 && EMAIL_ADDRESS) {\n MailApp.sendEmail(\n EMAIL_ADDRESS, 'Error sending SMS Message from Google Ads Scripts',\n response.getContentText());\n }\n}\n\n/**\n * Builds a SMS request object specific for the Twilio service.\n * @param {string} recipientPhoneNumber Destination number including country\n * code.\n * @param {string} textMessage The message to send.\n * @return {SmsRequest}\n */\nfunction buildTwilioMessageRequest(recipientPhoneNumber, textMessage) {\n if (!recipientPhoneNumber) {\n throw Error('No \"recipientPhoneNumber\" specified in call to ' +\n 'buildTwilioMessageRequest. \"recipientPhoneNumber\" cannot be empty');\n }\n if (!textMessage) {\n throw Error('No \"textMessage\" specified in call to ' +\n 'buildTwilioMessageRequest. \"textMessage\" cannot be empty');\n }\n const twilioUri = `https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages`;\n const authHeader = 'Basic ' +\n Utilities.base64Encode(\n TWILIO_ACCOUNT_SID + ':' + TWILIO_ACCOUNT_AUTHTOKEN);\n const options = {\n muteHttpExceptions: true,\n method: 'POST',\n headers: {Authorization: authHeader},\n payload: {\n From: TWILIO_SRC_PHONE_NUMBER,\n To: recipientPhoneNumber,\n // Twilio only accepts up to 1600 characters\n Body: textMessage.substr(0, 1600)\n }\n };\n return {url: twilioUri, options: options};\n}\n```"]]