{"id":27191,"date":"2021-05-14T12:41:58","date_gmt":"2021-05-14T10:41:58","guid":{"rendered":"https:\/\/www.sms77.io\/docs\/third-party-solutions\/google-apps\/"},"modified":"2021-12-10T11:00:32","modified_gmt":"2021-12-10T10:00:32","slug":"google-apps-script","status":"publish","type":"docs","link":"https:\/\/www.seven.io\/en\/docs\/third-party-solutions\/google-apps-script\/","title":{"rendered":"Google Apps Script"},"content":{"rendered":"

\nThis is a collection of code snippets which can be run inside Google Apps Script<\/a>. The snippets are highly customizable and can be very handy in cases where no own server infrastructure is given.\n<\/p>\n

\n

Google Calendar<\/h2>\n

For every function the time span can be changed by altering the variable hours<\/code>. The default value is 96.<\/p>\n

SMS<\/h3>\n

The function Sms77CalendarSMS<\/code> sends a SMS for every event occurring in the given timeframe.<\/p>\n

\r\nfunction Sms77CalendarSMS() {\r\n    const apiKey = 'INSERT_YOUR_SMS77_API_KEY';\r\n    const to = 'INSERT_YOUR_RECIPIENT_OF_CHOICE';\r\n\r\n    const toPlaceholder = key => `:${key}:`;\r\n    const placeholder_id = toPlaceholder('ID');\r\n    const placeholder_time = toPlaceholder('TIME');\r\n    const placeholder_title = toPlaceholder('TITLE');\r\n    const now = new Date();\r\n    const statusTagKey = 'sms77_status_sms';\r\n    const statusTagValue = 'ok';\r\n\r\n    const textTemplate = `Google Calendar: Upcoming event: ${placeholder_title} with id ${placeholder_id} starting ${placeholder_time}.`;\r\n\r\n    const calendarName = '';\r\n    const debug = false;\r\n    const delay = '';\r\n    const flash = false;\r\n    const foreign_id = '';\r\n    const from = 'GoogleApps';\r\n    const hours = 96; \/\/ hours from now until end date\r\n    const label = '';\r\n    const performance_tracking = false;\r\n\r\n    for (const event of ('' === calendarName ? CalendarApp.getDefaultCalendar()\r\n        : CalendarApp.getCalendarsByName(calendarName)).getEvents(\r\n        now, new Date(now.getTime() + (hours * 60 * 60 * 1000)))) {\r\n        if (statusTagValue === event.getTag(statusTagKey)) {\r\n            continue;\r\n        }\r\n\r\n        let text = textTemplate;\r\n        Object.entries({\r\n            [placeholder_id]: () => event.getId(),\r\n            [placeholder_time]: () => event.getStartTime(),\r\n            [placeholder_title]: () => event.getTitle(),\r\n        }).forEach(([k, v]) => text = text.replace(k, v()));\r\n\r\n        const payload = {\r\n            from,\r\n            text,\r\n            to,\r\n        };\r\n        Object.entries({\r\n            debug,\r\n            delay,\r\n            flash,\r\n            foreign_id,\r\n            from,\r\n            label,\r\n            performance_tracking\r\n        }).forEach(([k, v]) => {\r\n            if (false !== v) {\r\n                if (true === v) {\r\n                    payload[k] = '1';\r\n                } else if ('' !== v) {\r\n                    payload[k] = v;\r\n                }\r\n            }\r\n        });\r\n\r\n        const response = UrlFetchApp.fetch('https:\/\/gateway.sms77.io\/api\/sms', {\r\n            headers: {\r\n                SentWith: 'Google-Apps',\r\n                'X-Api-Key': apiKey,\r\n            },\r\n            method: 'post',\r\n            payload,\r\n        }).getContentText();\r\n\r\n        if (100 !== Number.parseInt(response)) {\r\n            throw new Error(`Unexpected status code \"${response}\".`);\r\n        }\r\n\r\n        Logger.log(response);\r\n\r\n        event.setTag(statusTagKey, statusTagValue);\r\n    }\r\n}\r\n<\/pre>\n

Voice<\/h3>\n

The function Sms77CalendarVoice<\/code> issues a text-to-speech call for every event occurring in the given timeframe.<\/p>\n

\r\nfunction Sms77CalendarVoice() {\r\n    const apiKey = 'INSERT_YOUR_SMS77_API_KEY';\r\n    const to = 'INSERT_YOUR_RECIPIENT_OF_CHOICE';\r\n\r\n    const toPlaceholder = key => `:${key}:`;\r\n    const placeholder_id = toPlaceholder('ID');\r\n    const placeholder_time = toPlaceholder('TIME');\r\n    const placeholder_title = toPlaceholder('TITLE');\r\n    const now = new Date();\r\n    const statusTagKey = 'sms77_status_voice';\r\n    const statusTagValue = 'ok';\r\n\r\n    const textTemplate = `Google Calendar informs you about the upcoming event ${placeholder_title} starting at ${placeholder_time}.`;\r\n\r\n    const calendarName = '';\r\n    const from = '+491771783130';\r\n    const hours = 96; \/\/ hours from now until end date\r\n    const xml = false;\r\n\r\n    for (const event of ('' === calendarName ? CalendarApp.getDefaultCalendar()\r\n        : CalendarApp.getCalendarsByName(calendarName)).getEvents(\r\n        now, new Date(now.getTime() + (hours * 60 * 60 * 1000)))) {\r\n        if (statusTagValue === event.getTag(statusTagKey)) {\r\n            continue;\r\n        }\r\n\r\n        let text = textTemplate;\r\n        Object.entries({\r\n            [placeholder_id]: () => event.getId(),\r\n            [placeholder_time]: () => event.getStartTime(),\r\n            [placeholder_title]: () => event.getTitle(),\r\n        }).forEach(([k, v]) => text = text.replace(k, v()));\r\n\r\n        const payload = {\r\n            from,\r\n            text,\r\n            to,\r\n        };\r\n\r\n        if (xml) {\r\n            payload.xml = '1';\r\n        }\r\n\r\n        const response = UrlFetchApp.fetch('https:\/\/gateway.sms77.io\/api\/voice', {\r\n            headers: {\r\n                SentWith: 'Google-Apps',\r\n                'X-Api-Key': apiKey,\r\n            },\r\n            method: 'post',\r\n            payload,\r\n        }).getContentText();\r\n\r\n        Logger.log(response);\r\n\r\n        const code = response.split('\\n')[0];\r\n        if ('100' !== code) {\r\n            throw new Error(`Unexpected status code \"${code}\".`);\r\n        }\r\n\r\n        event.setTag(statusTagKey, statusTagValue);\r\n    }\r\n}\r\n<\/pre>\n<\/section>\n
\n
\n

Google Contacts<\/h2>\n

SMS<\/h3>\n

The function Sms77ContactsSMS<\/code> sends a SMS to all of your contacts.<\/p>\n

\r\nfunction Sms77ContactsSMS() {\r\n    const apiKey = 'INSERT_YOUR_SMS77_API_KEY';\r\n\r\n    const toPlaceholder = key => `:${key}:`;\r\n    const placeholder_id = toPlaceholder('ID');\r\n    const placeholder_full_name = toPlaceholder('FULL_NAME');\r\n    const placeholder_notes = toPlaceholder('NOTES');\r\n\r\n    const textTemplate = `Dear ${placeholder_full_name} our new phone number is +4901234567890. Kind regards Phil from Sms77!`;\r\n\r\n    const debug = false;\r\n    const delay = '';\r\n    const flash = false;\r\n    const foreign_id = '';\r\n    const from = 'GoogleApps';\r\n    const label = '';\r\n    const performance_tracking = false;\r\n\r\n    for (const contact of ContactsApp.getContacts()) {\r\n        const phones = contact.getPhones();\r\n        if (!phones.length) {\r\n            continue;\r\n        }\r\n\r\n        let text = textTemplate;\r\n        Object.entries({\r\n            [placeholder_id]: () => contact.getId(),\r\n            [placeholder_full_name]: () => contact.getFullName(),\r\n            [placeholder_notes]: () => contact.getNotes(),\r\n        }).forEach(([k, v]) => text = text.replace(k, v()));\r\n\r\n        const payload = {\r\n            from,\r\n            text,\r\n            to: (phones.find(p => p.isPrimary()) || phones.shift()).getPhoneNumber(),\r\n        };\r\n        Object.entries({\r\n            debug,\r\n            delay,\r\n            flash,\r\n            foreign_id,\r\n            from,\r\n            label,\r\n            performance_tracking\r\n        }).forEach(([k, v]) => {\r\n            if (false !== v) {\r\n                if (true === v) {\r\n                    payload[k] = '1';\r\n                } else if ('' !== v) {\r\n                    payload[k] = v;\r\n                }\r\n            }\r\n        });\r\n\r\n        const response = UrlFetchApp.fetch('https:\/\/gateway.sms77.io\/api\/sms', {\r\n            headers: {\r\n                SentWith: 'Google-Apps',\r\n                'X-Api-Key': apiKey,\r\n            },\r\n            method: 'post',\r\n            payload,\r\n        }).getContentText();\r\n\r\n        if (100 !== Number.parseInt(response)) {\r\n            throw new Error(`Unexpected status code \"${response}\".`);\r\n        }\r\n\r\n        Logger.log(response);\r\n    }\r\n}\r\n<\/pre>\n

Voice<\/h3>\n

The function Sms77ContactsVoice<\/code> issues a text-to-speech call to all of your contacts.<\/p>\n

\r\nfunction Sms77ContactsVoice() {\r\n    const apiKey = 'INSERT_YOUR_SMS77_API_KEY';\r\n\r\n    const toPlaceholder = key => `:${key}:`;\r\n    const placeholder_id = toPlaceholder('ID');\r\n    const placeholder_full_name = toPlaceholder('FULL_NAME');\r\n    const placeholder_notes = toPlaceholder('NOTES');\r\n\r\n    const textTemplate = `Dear ${placeholder_full_name} our new phone number is +4901234567890. Kind regards Phil from Sms77!`;\r\n\r\n    const from = '+491771783130';\r\n    const xml = false;\r\n\r\n    for (const contact of ContactsApp.getContacts()) {\r\n        const phones = contact.getPhones();\r\n        if (!phones.length) {\r\n            continue;\r\n        }\r\n\r\n        let text = textTemplate;\r\n        Object.entries({\r\n            [placeholder_id]: () => contact.getId(),\r\n            [placeholder_full_name]: () => contact.getFullName(),\r\n            [placeholder_notes]: () => contact.getNotes(),\r\n        }).forEach(([k, v]) => text = text.replace(k, v()));\r\n\r\n        const payload = {\r\n            from,\r\n            text,\r\n            to: (phones.find(p => p.isPrimary()) || phones.shift()).getPhoneNumber(),\r\n        };\r\n\r\n        if (xml) {\r\n            payload.xml = '1';\r\n        }\r\n\r\n        const response = UrlFetchApp.fetch('https:\/\/gateway.sms77.io\/api\/voice', {\r\n            headers: {\r\n                SentWith: 'Google-Apps',\r\n                'X-Api-Key': apiKey,\r\n            },\r\n            method: 'post',\r\n            payload,\r\n        }).getContentText();\r\n\r\n        Logger.log(response);\r\n\r\n        const code = response.split('\\n')[0];\r\n        if ('100' !== code) {\r\n            throw new Error(`Unexpected status code \"${code}\".`);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<\/section>\n","protected":false},"featured_media":0,"parent":17826,"menu_order":18,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-27191","docs","type-docs","status-publish","hentry"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs\/27191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/comments?post=27191"}],"version-history":[{"count":6,"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs\/27191\/revisions"}],"predecessor-version":[{"id":27197,"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs\/27191\/revisions\/27197"}],"up":[{"embeddable":true,"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs\/17826"}],"next":[{"title":"GitHub Actions","link":"https:\/\/www.seven.io\/en\/docs\/third-party-solutions\/github-actions\/","href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs\/26924"}],"prev":[{"title":"Firefox Add-On","link":"https:\/\/www.seven.io\/en\/docs\/third-party-solutions\/firefox\/","href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/docs\/17859"}],"wp:attachment":[{"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/media?parent=27191"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.seven.io\/en\/wp-json\/wp\/v2\/doc_tag?post=27191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}