|
| 1 | +import axios from "axios"; |
| 2 | +import { ITextingProvider, TextingProviderConfig, TextingSendResult, ProviderCapabilities, SubscriberResult, ListsResult } from "../../interfaces.js"; |
| 3 | + |
| 4 | +const DEFAULT_BASE_URL = "https://sms.nalosolutions.com/smsbackend"; |
| 5 | +const SUCCESS = "1701"; |
| 6 | + |
| 7 | +export class NaloProvider implements ITextingProvider { |
| 8 | + readonly name = "Nalo Solutions"; |
| 9 | + readonly capabilities: ProviderCapabilities = { addSubscriber: false, getLists: false }; |
| 10 | + |
| 11 | + // ponytail: local Ghana numbers (0XXXXXXXXX) become 233XXXXXXXXX; other formats pass through as digits. |
| 12 | + static normalizeNumber(phone: string) { |
| 13 | + const digits = phone.replace(/\D/g, ""); |
| 14 | + return /^0\d{9}$/.test(digits) ? "233" + digits.substring(1) : digits; |
| 15 | + } |
| 16 | + |
| 17 | + private async send(config: TextingProviderConfig, recipients: string[], message: string): Promise<TextingSendResult> { |
| 18 | + try { |
| 19 | + const response = await axios.post( |
| 20 | + `${config.baseUrl || DEFAULT_BASE_URL}/Resl_Nalo/send-message/`, |
| 21 | + { key: config.apiKey, msisdn: recipients.map(NaloProvider.normalizeNumber).join(","), message, sender_id: config.fromNumber || "" }, |
| 22 | + { headers: { "Content-Type": "application/json" } } |
| 23 | + ); |
| 24 | + if (String(response.data?.status) === SUCCESS) return { success: true, providerMessageId: response.data?.job_id }; |
| 25 | + return { success: false, error: this.errorText(response.data) }; |
| 26 | + } catch (error: any) { |
| 27 | + return { success: false, error: this.errorText(error.response?.data) || error.message }; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private errorText(data: any): string { |
| 32 | + if (!data) return ""; |
| 33 | + if (data.code) return `${data.code}: ${data.message || ""}`.trim(); |
| 34 | + return typeof data === "string" ? data : JSON.stringify(data); |
| 35 | + } |
| 36 | + |
| 37 | + sendMessage(config: TextingProviderConfig, to: string, message: string): Promise<TextingSendResult> { |
| 38 | + return this.send(config, [to], message); |
| 39 | + } |
| 40 | + |
| 41 | + async sendBulk(config: TextingProviderConfig, recipients: string[], message: string): Promise<TextingSendResult[]> { |
| 42 | + const result = await this.send(config, recipients, message); |
| 43 | + return recipients.map(() => ({ ...result })); |
| 44 | + } |
| 45 | + |
| 46 | + // Nalo validates fields before auth and has no account endpoint, so a key can't be checked without sending. |
| 47 | + async validateCredentials(config: TextingProviderConfig): Promise<boolean> { |
| 48 | + return !!config.apiKey && !!config.fromNumber; |
| 49 | + } |
| 50 | + |
| 51 | + async addSubscriber(): Promise<SubscriberResult> { |
| 52 | + return { success: false, error: "Nalo Solutions does not support subscriber management" }; |
| 53 | + } |
| 54 | + |
| 55 | + async getLists(): Promise<ListsResult> { |
| 56 | + return { success: false, error: "Nalo Solutions does not support lists" }; |
| 57 | + } |
| 58 | +} |
0 commit comments