From 30d2740368eabcb1c9d54762dadf8c50c273a2a4 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Tue, 25 Oct 2022 20:03:08 +0200 Subject: [PATCH] feat: Implement twilio connection --- .../src/apps/twilio/assets/favicon.svg | 11 ++ .../backend/src/apps/twilio/auth/index.ts | 120 ++++++++++++++++++ .../src/apps/twilio/auth/is-still-verified.ts | 13 ++ .../apps/twilio/auth/verify-credentials.ts | 20 +++ packages/backend/src/apps/twilio/index.ts | 13 ++ 5 files changed, 177 insertions(+) create mode 100644 packages/backend/src/apps/twilio/assets/favicon.svg create mode 100644 packages/backend/src/apps/twilio/auth/index.ts create mode 100644 packages/backend/src/apps/twilio/auth/is-still-verified.ts create mode 100644 packages/backend/src/apps/twilio/auth/verify-credentials.ts create mode 100644 packages/backend/src/apps/twilio/index.ts diff --git a/packages/backend/src/apps/twilio/assets/favicon.svg b/packages/backend/src/apps/twilio/assets/favicon.svg new file mode 100644 index 00000000..7c20e190 --- /dev/null +++ b/packages/backend/src/apps/twilio/assets/favicon.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/packages/backend/src/apps/twilio/auth/index.ts b/packages/backend/src/apps/twilio/auth/index.ts new file mode 100644 index 00000000..8c35364b --- /dev/null +++ b/packages/backend/src/apps/twilio/auth/index.ts @@ -0,0 +1,120 @@ +import verifyCredentials from './verify-credentials'; +import isStillVerified from './is-still-verified'; + +export default { + fields: [ + { + key: 'accountSid', + label: 'Account SID', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: + 'Log into your Twilio account and find "API Credentials" on this page https://www.twilio.com/user/account/settings', + clickToCopy: false, + }, + { + key: 'authToken', + label: 'Auth Token', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'Found directly below your Account SID.', + clickToCopy: false, + }, + ], + authenticationSteps: [ + { + step: 1, + type: 'mutation', + name: 'createConnection', + arguments: [ + { + name: 'key', + value: '{key}', + }, + { + name: 'formattedData', + value: null, + properties: [ + { + name: 'accountSid', + value: '{fields.accountSid}', + }, + { + name: 'authToken', + value: '{fields.authToken}', + }, + ], + }, + ], + }, + { + step: 2, + type: 'mutation', + name: 'verifyConnection', + arguments: [ + { + name: 'id', + value: '{createConnection.id}', + }, + ], + }, + ], + reconnectionSteps: [ + { + step: 1, + type: 'mutation', + name: 'resetConnection', + arguments: [ + { + name: 'id', + value: '{connection.id}', + }, + ], + }, + { + step: 2, + type: 'mutation', + name: 'updateConnection', + arguments: [ + { + name: 'id', + value: '{connection.id}', + }, + { + name: 'formattedData', + value: null, + properties: [ + { + name: 'accountSid', + value: '{fields.accountSid}', + }, + { + name: 'authToken', + value: '{fields.authToken}', + }, + ], + }, + ], + }, + { + step: 3, + type: 'mutation', + name: 'verifyConnection', + arguments: [ + { + name: 'id', + value: '{connection.id}', + }, + ], + }, + ], + + verifyCredentials, + isStillVerified, +}; diff --git a/packages/backend/src/apps/twilio/auth/is-still-verified.ts b/packages/backend/src/apps/twilio/auth/is-still-verified.ts new file mode 100644 index 00000000..95a1ef29 --- /dev/null +++ b/packages/backend/src/apps/twilio/auth/is-still-verified.ts @@ -0,0 +1,13 @@ +import { IGlobalVariable } from '@automatisch/types'; +import verifyCredentials from './verify-credentials'; + +const isStillVerified = async ($: IGlobalVariable) => { + try { + await verifyCredentials($); + return true; + } catch (error) { + return false; + } +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/twilio/auth/verify-credentials.ts b/packages/backend/src/apps/twilio/auth/verify-credentials.ts new file mode 100644 index 00000000..3cbc44d7 --- /dev/null +++ b/packages/backend/src/apps/twilio/auth/verify-credentials.ts @@ -0,0 +1,20 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const verifyCredentials = async ($: IGlobalVariable) => { + try { + await $.http.get('/2010-04-01/Accounts.json?PageSize=1', { + auth: { + username: $.auth.data.accountSid as string, + password: $.auth.data.authToken as string, + }, + }); + + await $.auth.set({ + screenName: $.auth.data.accountSid, + }); + } catch (error) { + throw new Error(JSON.stringify(error.response.data)); + } +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/twilio/index.ts b/packages/backend/src/apps/twilio/index.ts new file mode 100644 index 00000000..dd312eaf --- /dev/null +++ b/packages/backend/src/apps/twilio/index.ts @@ -0,0 +1,13 @@ +import defineApp from '../../helpers/define-app'; + +export default defineApp({ + name: 'Twilio', + key: 'twilio', + iconUrl: '{BASE_URL}/apps/twilio/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/connections/twilio', + supportsConnections: true, + baseUrl: 'https://twilio.com', + apiBaseUrl: 'https://api.twilio.com', + primaryColor: 'e1000f', + beforeRequest: [], +});