feat: Implement send sms action of twilio

This commit is contained in:
Faruk AYDIN
2022-10-26 18:24:12 +02:00
committed by Ali BARIN
parent 4553084503
commit 7d7c96274d
4 changed files with 87 additions and 7 deletions

View 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 });
},
});

View File

@@ -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,

View 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;

View File

@@ -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],
});