diff --git a/packages/backend/src/apps/twilio/actions/send-sms/index.ts b/packages/backend/src/apps/twilio/actions/send-sms/index.ts new file mode 100644 index 00000000..6fcb55a7 --- /dev/null +++ b/packages/backend/src/apps/twilio/actions/send-sms/index.ts @@ -0,0 +1,64 @@ +import defineAction from '../../../../helpers/define-action'; + +export default defineAction({ + name: 'Send SMS', + key: 'sendSms', + description: 'Send an SMS', + substeps: [ + { + key: 'chooseConnection', + name: 'Choose connection', + }, + { + key: 'chooseAction', + name: 'Set up action', + arguments: [ + { + label: 'From Number', + key: 'fromNumber', + type: 'string', + required: true, + description: + 'The number to send the SMS from. Include country code. Example: 15551234567', + variables: true, + }, + { + label: 'To Number', + key: 'toNumber', + type: 'string', + required: true, + description: + 'The number to send the SMS to. Include country code. Example: 15551234567', + variables: true, + }, + { + label: 'Message', + key: 'message', + type: 'string', + required: true, + description: 'The message to send.', + variables: true, + }, + ], + }, + { + key: 'testStep', + name: 'Test action', + }, + ], + + async run($) { + const requestPath = `/2010-04-01/Accounts/${$.auth.data.accountSid}/Messages.json`; + const messageBody = $.step.parameters.message; + + const fromNumber = '+' + ($.step.parameters.fromNumber as string).trim(); + const toNumber = '+' + ($.step.parameters.toNumber as string).trim(); + + const response = await $.http.post( + requestPath, + `Body=${messageBody}&From=${fromNumber}&To=${toNumber}` + ); + + $.setActionItem({ raw: response.data }); + }, +}); diff --git a/packages/backend/src/apps/twilio/auth/verify-credentials.ts b/packages/backend/src/apps/twilio/auth/verify-credentials.ts index 3cbc44d7..8d7b8d53 100644 --- a/packages/backend/src/apps/twilio/auth/verify-credentials.ts +++ b/packages/backend/src/apps/twilio/auth/verify-credentials.ts @@ -2,12 +2,7 @@ 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 $.http.get('/2010-04-01/Accounts.json?PageSize=1'); await $.auth.set({ screenName: $.auth.data.accountSid, diff --git a/packages/backend/src/apps/twilio/common/add-auth-header.ts b/packages/backend/src/apps/twilio/common/add-auth-header.ts new file mode 100644 index 00000000..f5f47f85 --- /dev/null +++ b/packages/backend/src/apps/twilio/common/add-auth-header.ts @@ -0,0 +1,20 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if ( + requestConfig.headers && + $.auth.data?.accountSid && + $.auth.data?.authToken + ) { + requestConfig.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + + requestConfig.auth = { + username: $.auth.data.accountSid as string, + password: $.auth.data.authToken as string, + }; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/twilio/index.ts b/packages/backend/src/apps/twilio/index.ts index dd312eaf..46c3f248 100644 --- a/packages/backend/src/apps/twilio/index.ts +++ b/packages/backend/src/apps/twilio/index.ts @@ -1,4 +1,5 @@ import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; export default defineApp({ name: 'Twilio', @@ -9,5 +10,5 @@ export default defineApp({ baseUrl: 'https://twilio.com', apiBaseUrl: 'https://api.twilio.com', primaryColor: 'e1000f', - beforeRequest: [], + beforeRequest: [addAuthHeader], });