feat: Implement send sms action of twilio
This commit is contained in:
64
packages/backend/src/apps/twilio/actions/send-sms/index.ts
Normal file
64
packages/backend/src/apps/twilio/actions/send-sms/index.ts
Normal file
@@ -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 });
|
||||
},
|
||||
});
|
@@ -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,
|
||||
|
20
packages/backend/src/apps/twilio/common/add-auth-header.ts
Normal file
20
packages/backend/src/apps/twilio/common/add-auth-header.ts
Normal file
@@ -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;
|
@@ -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],
|
||||
});
|
||||
|
Reference in New Issue
Block a user