Skip to content

Commit

Permalink
[Twilio Messaging] - new Twilio Messaging Destination (#2612)
Browse files Browse the repository at this point in the history
* Twilio Messaging init

* updates

* saving progress

* saving progress

* saving progress

* adding field UI dependencies

* minor refactor

* after some initial testing

* refactoring dynamic functions

* refactoring

* refactoring

* saving progress

* saving progress

* saving progress

* spelling correction

* renaming Action and removing tests for now

* removing console logs

* fixing typing issue
  • Loading branch information
joe-ayoub-segment authored Dec 3, 2024
1 parent eee15fa commit 0904d4e
Show file tree
Hide file tree
Showing 14 changed files with 1,123 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('Reddit Conversions Api', () => {
custom_event_name: 'Some Custom Event Name'
}
})
console.log(responses[0].options.json)

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ describe('POST events', () => {
mapping: track_mapping
})

console.log(responses[0].status)

expect(responses[0].status).toBe(200)
})

Expand All @@ -80,8 +78,6 @@ describe('POST events', () => {
mapping: identify_mapping
})

console.log(responses[0].status)

expect(responses[0].status).toBe(200)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,6 @@ function snakeCase(str: string) {
return result.split(' ').join('_').toLowerCase()
}

/*function handleEvent(str: EventType, object: EventBody, str: apiKey) {
const event: Event = {
api_key: apiKey,
body: eventBody,
type: eventType,
}
sendEvent(event);
}
function sendEvent(event: Event) {
const captureUrl = `https://c.schematichq.com/e`;
const payload = JSON.stringify(event);
fetch(captureUrl, {
method: "POST",
headers: {
"Content-Type": "application/json;charset=UTF-8",
},
body: payload,
})
.then((response) => {
if (!response.ok) {
throw new Error(
`Network response was not ok: ${response.statusText}`,
)
}
})
.catch((error) => {
console.error("There was a problem with the fetch operation:", error);
})
}*/

const action: ActionDefinition<Settings, Payload> = {
title: 'Track Event',
description: 'Send track events to Schematic',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('Twilio Messaging', () => {
describe('testAuthentication', () => {
it('should pass', () => {
expect(true).toBe(true)
})
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { DestinationDefinition } from '@segment/actions-core'
import type { Settings } from './generated-types'

import sendMessage from './sendMessage'

const destination: DestinationDefinition<Settings> = {
name: 'Twilio Messaging',
slug: 'actions-twilio-messaging',
mode: 'cloud',
description: 'Send SMS, MMS, Whatsapp and Messenger messages with Twilio',
authentication: {
scheme: 'basic',
fields: {
accountSID: {
label: 'Twilio Account SID',
description: 'Twilio Account SID',
type: 'string',
required: true
},
apiKeySID: {
label: 'Twilio API Key SID',
description: 'Twilio API Key SID',
type: 'string',
required: true
},
apiKeySecret: {
label: 'Twilio API Key Secret',
description: 'Twilio API Key Secret',
type: 'password',
required: true
},
region: {
label: 'Region',
description: 'The region where the message is originating from',
type: 'string',
choices: [
{ value: 'us-west-2', label: 'US West 2' },
{ value: 'eu-west-1', label: 'EU West 1' }
],
default: 'us-west-2',
required: true
}
},
testAuthentication: (request) => {
return request(`https://api.twilio.com/2010-04-01`)
}
},
extendRequest: ({ settings }) => {
return {
'Content-Type': 'application/x-www-form-urlencoded',
username: settings.apiKeySID,
password: settings.apiKeySecret
}
},
actions: {
sendMessage
}
}

export default destination
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


describe('TwilioMessaging.sendMessage', () => {
// TODO: Implement tests
it('should pass', () => {
expect(true).toBe(true)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { PredefinedContentTypes } from './types'

export const CONTENT_SID_TOKEN = '{accountSid}'

export const ACCOUNT_SID_TOKEN = '{accountSid}'

export const SEND_SMS_URL = `https://api.twilio.com/2010-04-01/Accounts/${ACCOUNT_SID_TOKEN}/Messages.json`

export const FIELD_REGEX = /\[(.*?)\]/

export const TOKEN_REGEX = /{{(.*?)}}/g

export const E164_REGEX = /^\+?[1-9]\d{1,14}$/

export const MESSAGING_SERVICE_SID_REGEX = /^MG[0-9a-fA-F]{32}$/

export const CONTENT_SID_REGEX = /^HX[0-9a-fA-F]{32}$/

export const GET_INCOMING_PHONE_NUMBERS_URL = `https://api.twilio.com/2010-04-01/Accounts/${ACCOUNT_SID_TOKEN}/IncomingPhoneNumbers.json?PageSize=1000`

export const GET_MESSAGING_SERVICE_SIDS_URL = 'https://messaging.twilio.com/v1/Services?PageSize=1000'

export const GET_ALL_CONTENTS_URL = 'https://content.twilio.com/v1/Content?PageSize=1000'

export const GET_CONTENT_URL = `https://content.twilio.com/v1/Content/${CONTENT_SID_TOKEN}`

export const GET_CONTENT_VARIABLES_URL = `https://content.twilio.com/v1/Content/${CONTENT_SID_TOKEN}`

export const CHANNELS = {
SMS: 'SMS',
MMS: 'MMS',
WHATSAPP: 'Whatsapp',
MESSENGER: 'Messenger'
} as const

export const PREDEFINED_CONTENT_TYPES: PredefinedContentTypes = {
TEXT: {
friendly_name: 'Text',
name: 'twilio/text',
supports_media: false,
supported_channels: [CHANNELS.SMS, CHANNELS.WHATSAPP, CHANNELS.MESSENGER]
},
MEDIA: {
friendly_name: 'Media',
name: 'twilio/media',
supports_media: true,
supported_channels: [CHANNELS.MMS, CHANNELS.WHATSAPP, CHANNELS.MESSENGER]
},
QUICK_REPLY: {
friendly_name: 'Quick Reply',
name: 'twilio/quick-reply',
supports_media: false,
supported_channels: [CHANNELS.WHATSAPP, CHANNELS.MESSENGER]
},
CALL_TO_ACTION: {
friendly_name: 'Call to Action',
name: 'twilio/call-to-action',
supports_media: false,
supported_channels: [CHANNELS.WHATSAPP, CHANNELS.MESSENGER]
},
LIST_PICKER: {
friendly_name: 'List Picker',
name: 'twilio/list-picker',
supports_media: false,
supported_channels: [CHANNELS.WHATSAPP]
},
CARD: {
friendly_name: 'Card',
name: 'twilio/card',
supports_media: true,
supported_channels: [CHANNELS.WHATSAPP, CHANNELS.MESSENGER]
},
WHATSAPP_CARD: {
friendly_name: 'WhatsApp Card',
name: 'whatsapp/card',
supports_media: true,
supported_channels: [CHANNELS.WHATSAPP]
},
WHATSAPP_AUTHENTICATION: {
friendly_name: 'WhatsApp Authentication',
name: 'whatsapp/authentication',
supports_media: false,
supported_channels: [CHANNELS.WHATSAPP]
},
CATALOG: {
friendly_name: 'Catalog',
name: 'twilio/catalog',
supports_media: false,
supported_channels: [CHANNELS.WHATSAPP]
}
}

export const INLINE_CONTENT_TYPES = {
INLINE: {
friendly_name: 'Inline',
name: undefined,
supports_media: true,
supported_channels: [CHANNELS.SMS, CHANNELS.MMS, CHANNELS.WHATSAPP, CHANNELS.MESSENGER]
}
}

export const ALL_CONTENT_TYPES = {
...PREDEFINED_CONTENT_TYPES,
...INLINE_CONTENT_TYPES
}

export const SENDER_TYPE = {
PHONE_NUMBER: 'Phone number',
MESSAGING_SERVICE: 'Messaging Service'
}
Loading

0 comments on commit 0904d4e

Please sign in to comment.