2023-04-01 22:16:56 +08:00
|
|
|
import { assert } from '@silverhand/essentials';
|
|
|
|
import { got, HTTPError } from 'got';
|
|
|
|
|
2023-04-01 15:53:09 +08:00
|
|
|
import type {
|
|
|
|
GetConnectorConfig,
|
|
|
|
SendMessageFunction,
|
|
|
|
CreateConnector,
|
|
|
|
SmsConnector,
|
|
|
|
} from '@logto/connector-kit';
|
|
|
|
import {
|
|
|
|
ConnectorError,
|
|
|
|
ConnectorErrorCodes,
|
|
|
|
validateConfig,
|
|
|
|
ConnectorType,
|
2024-01-25 19:44:20 +08:00
|
|
|
replaceSendMessageHandlebars,
|
2023-04-01 15:53:09 +08:00
|
|
|
} from '@logto/connector-kit';
|
|
|
|
|
|
|
|
import { defaultMetadata, endpoint } from './constant.js';
|
2023-08-03 13:28:23 +08:00
|
|
|
import type { PublicParameters } from './types.js';
|
2023-04-01 15:53:09 +08:00
|
|
|
import { twilioSmsConfigGuard } from './types.js';
|
|
|
|
|
|
|
|
const sendMessage =
|
|
|
|
(getConfig: GetConnectorConfig): SendMessageFunction =>
|
|
|
|
async (data, inputConfig) => {
|
|
|
|
const { to, type, payload } = data;
|
|
|
|
const config = inputConfig ?? (await getConfig(defaultMetadata.id));
|
2023-08-03 13:28:23 +08:00
|
|
|
validateConfig(config, twilioSmsConfigGuard);
|
2023-04-01 15:53:09 +08:00
|
|
|
const { accountSID, authToken, fromMessagingServiceSID, templates } = config;
|
|
|
|
const template = templates.find((template) => template.usageType === type);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
template,
|
|
|
|
new ConnectorError(
|
|
|
|
ConnectorErrorCodes.TemplateNotFound,
|
|
|
|
`Cannot find template for type: ${type}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const parameters: PublicParameters = {
|
|
|
|
To: to,
|
|
|
|
MessagingServiceSid: fromMessagingServiceSID,
|
2024-01-25 19:44:20 +08:00
|
|
|
Body: replaceSendMessageHandlebars(template.content, payload),
|
2023-04-01 15:53:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2023-07-08 01:17:21 +08:00
|
|
|
return await got.post(endpoint.replaceAll('{{accountSID}}', accountSID), {
|
2023-04-01 15:53:09 +08:00
|
|
|
headers: {
|
|
|
|
Authorization:
|
|
|
|
'Basic ' + Buffer.from([accountSID, authToken].join(':')).toString('base64'),
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
body: new URLSearchParams(parameters).toString(),
|
|
|
|
});
|
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof HTTPError) {
|
|
|
|
const {
|
|
|
|
response: { body: rawBody },
|
|
|
|
} = error;
|
|
|
|
assert(
|
|
|
|
typeof rawBody === 'string',
|
2023-04-19 11:07:45 +08:00
|
|
|
new ConnectorError(
|
|
|
|
ConnectorErrorCodes.InvalidResponse,
|
|
|
|
`Invalid response raw body type: ${typeof rawBody}`
|
|
|
|
)
|
2023-04-01 15:53:09 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
throw new ConnectorError(ConnectorErrorCodes.General, rawBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createTwilioSmsConnector: CreateConnector<SmsConnector> = async ({ getConfig }) => {
|
|
|
|
return {
|
|
|
|
metadata: defaultMetadata,
|
|
|
|
type: ConnectorType.Sms,
|
|
|
|
configGuard: twilioSmsConfigGuard,
|
|
|
|
sendMessage: sendMessage(getConfig),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default createTwilioSmsConnector;
|