Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ClickSend Support #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ We've developed TextWise and have been using it internally for a while, and have
Care has been taken to ensure that TextWise works with on-prem or cloud, and has been styled to fit in with the rest of the ConnectWise Manage (PSA) user interface to provide a seamless experience.

# Features
- Supports SMSBroadcast and Twilio
- Supports SMSBroadcast, Twilio and ClickSend
- Send SMS messages or authentication codes directly to a contact
- Authenticate ticket contacts with randomly generated 4 digit codes
- Optional configuration for:
Expand All @@ -33,13 +33,16 @@ The application uses the following environment variables:
## SMS Provider Variables
| Variable | Required | Description | Expected Shape | Default |
|--|--|--|--|--|
|SMS_PROVIDER|**false**|The SMS provider to use. Currently only supports Twilio or SmsBroadcast|**twilio** or **smsbroadcast**|**smsbroadcast**
|SMS_PROVIDER|**false**|The SMS provider to use. Currently supports Twilio or SmsBroadcast or ClickSend|**twilio** or **smsbroadcast** or **clicksend**|**smsbroadcast**
|SMSBROADCAST_USERNAME|**true** if **SMS_PROVIDER** is set to **smsbroadcast**|Your SmsBroadcast username|string
|SMSBROADCAST_PASSWORD|**true** if **SMS_PROVIDER** is set to **smsbroadcast**|Your SmsBroadcast password|string
|SMSBROADCAST_FROM|**true** if **SMS_PROVIDER** is set to **smsbroadcast**|The from label for your messages|string
|TWILIO_AUTH_TOKEN|**true** if **SMS_PROVIDER** is set to **twilio**|Your Twilio auth token|string
|TWILIO_ACCOUNT_SID|**true** if **SMS_PROVIDER** is set to **twilio**|Your Twilio account SID|string
|TWILIO_PHONE_NUMBER|**true** if **SMS_PROVIDER** is set to **twilio**|The phone number to use for sending|string
|CLICKSEND_USERNAME|**true** if **SMS_PROVIDER** is set to **clicksend**|Your ClickSend API Username|string
|CLICKSEND_PASSWORD|**true** if **SMS_PROVIDER** is set to **clicksend**|Your ClickSend API Key|string
|CLICKSEND_FROM|**false** if **SMS_PROVIDER** is set to **clicksend**|The number/alpha tag you wish to send to. Number must be in internation format. Leave blank to use a shared number|string

## Optional Config Variables
| Variable | Required | Description | Expected Shape | Default |
Expand Down
6 changes: 5 additions & 1 deletion src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export const env = createEnv({
CW_COMPANY_URL: z.string(),
CW_CODE_BASE: z.string().default("v4_6_release"),
SMS_AUTH_MSG: z.string().default("Hi! Your auth code is {code}."),
SMS_PROVIDER: z.enum(["twilio", "smsbroadcast"]).default("smsbroadcast"),
SMS_PROVIDER: z.enum(["twilio", "smsbroadcast", "clicksend"]).default("smsbroadcast"),
TWILIO_ACCOUNT_SID: z.string().optional().nullish(),
TWILIO_AUTH_TOKEN: z.string().optional().nullish(),
TWILIO_PHONE_NUMBER: z.string().optional().nullish(),
SMSBROADCAST_USERNAME: z.string().optional().nullish(),
SMSBROADCAST_PASSWORD: z.string().optional().nullish(),
SMSBROADCAST_FROM: z.string().optional().nullish(),
CLICKSEND_USERNAME: z.string().optional().nullish(),
CLICKSEND_PASSWORD: z.string().optional().nullish(),
CLICKSEND_FROM: z.string().optional().nullish(),

},
client: {
NEXT_PUBLIC_DEBUG_LOGGING: z
Expand Down
37 changes: 37 additions & 0 deletions src/lib/sms/providers/clicksend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { env } from "~/env";
import type { SMSProvider } from "./../types";

export class ClicksendProvider implements SMSProvider {
async sendSms(contactNumber: string, message: string): Promise<void> {
const apiUsername = env.CLICKSEND_USERNAME;
const apiPassword = env.CLICKSEND_PASSWORD;
const fromNumber = env.CLICKSEND_FROM|| "";

const messagePayload: any = {
body: message,
to: contactNumber,
source: "TextWise"
};

if (fromNumber) {
messagePayload.from = fromNumber;
}

const body = JSON.stringify({
messages: [messagePayload]
});

const response = await fetch('https://rest.clicksend.com/v3/sms/send', {
method: 'POST',
headers: {
'Authorization': `Basic ${btoa(`${apiUsername}:${apiPassword}`)}`,
'Content-Type': 'application/json'
},
body: body
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
}
}
3 changes: 2 additions & 1 deletion src/lib/sms/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './sms-broadcast'
export * from './twilio'
export * from './twilio'
export * from './clicksend'
4 changes: 3 additions & 1 deletion src/lib/sms/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SMSBroadcastProvider, TwilioProvider } from "~/lib/sms/providers";
import { SMSBroadcastProvider, TwilioProvider, ClicksendProvider } from "~/lib/sms/providers";

import { env } from "~/env";
import type { SMSProvider } from "~/lib/sms/types";
Expand All @@ -9,6 +9,8 @@ export const getSMSProvider = (): SMSProvider => {
return new TwilioProvider();
case "smsbroadcast":
return new SMSBroadcastProvider();
case "clicksend":
return new ClicksendProvider();
default:
throw new Error("Invalid SMS provider in environment variables");
}
Expand Down