@@ -2,88 +2,172 @@ import { BaseClient } from '../base/client';
22import { SirenConfig } from '../common/types' ;
33import { SirenValidationError } from '../common/errors' ;
44import {
5- RecipientType ,
6- ReplyData ,
5+ ProviderCode ,
6+ ProviderIntegration ,
7+ Recipient ,
78 SendMessageRequest ,
89 MessageData ,
9- StatusData
10+ StatusData ,
11+ ReplyData
1012} from './types' ;
1113
14+ /** Mapping between channel names and recipient field keys */
15+ const CHANNEL_RECIPIENT_KEY : Record < string , keyof Recipient > = {
16+ EMAIL : 'email' ,
17+ SMS : 'sms' ,
18+ WHATSAPP : 'whatsapp' ,
19+ SLACK : 'slack' ,
20+ TEAMS : 'teams' ,
21+ DISCORD : 'discord' ,
22+ LINE : 'line' ,
23+ IN_APP : 'inApp' ,
24+ PUSH : 'pushToken'
25+ } ;
26+
1227export class MessageClient extends BaseClient {
1328 constructor ( config : SirenConfig ) {
1429 super ( config ) ;
1530 }
1631
1732 /**
1833 * Send a message either using a template or directly.
19- * @param recipientType - The type of recipient ("user_id" or "direct")
20- * @param recipientValue - The identifier for the recipient (e.g., Slack user ID, email address)
21- * @param channel - The channel to send the message through (e.g., "SLACK", "EMAIL")
22- * @param body - Optional message body text (required if no template)
34+ * @param recipientValue - Identifier for the recipient (e.g., Slack user ID, email address)
35+ * @param channel - Channel to send the message through (e.g., "SLACK", "EMAIL")
36+ * @param body - Optional raw body text (required if no template)
2337 * @param templateName - Optional template name (required if no body)
24- * @param templateVariables - Optional template variables for template-based messages
25- * @returns The notification ID of the sent message
38+ * @param templateVariables - Optional variables for template-based messages
39+ * @param providerName - Optional provider integration name (must be provided with providerCode)
40+ * @param providerCode - Optional provider integration code (must be provided with providerName)
41+ * @returns Notification ID of the sent message
2642 */
2743 async send (
28- recipientType : RecipientType ,
2944 recipientValue : string ,
3045 channel : string ,
3146 body ?: string ,
3247 templateName ?: string ,
33- templateVariables ?: Record < string , any >
48+ templateVariables ?: Record < string , any > ,
49+ providerName ?: string ,
50+ providerCode ?: ProviderCode
3451 ) : Promise < string > {
3552 if ( ! body && ! templateName ) {
3653 throw new SirenValidationError ( 'Either body or templateName must be provided' ) ;
3754 }
3855
56+ if ( ( providerName !== undefined ) !== ( providerCode !== undefined ) ) {
57+ throw new SirenValidationError ( 'Both providerName and providerCode must be provided together' ) ;
58+ }
59+
60+ const recipient = this . createRecipient ( channel , recipientValue ) ;
61+
3962 const payload : SendMessageRequest = {
40- recipient : {
41- type : recipientType ,
42- value : recipientValue
43- } ,
63+ recipient,
4464 channel
4565 } ;
4666
4767 if ( body ) {
4868 payload . body = body ;
4969 } else if ( templateName ) {
5070 payload . template = { name : templateName } ;
71+ }
72+
5173 if ( templateVariables ) {
5274 payload . templateVariables = templateVariables ;
53- }
75+ }
76+
77+ if ( providerName && providerCode ) {
78+ payload . providerIntegration = {
79+ name : providerName ,
80+ code : providerCode
81+ } as ProviderIntegration ;
5482 }
5583
5684 const response = await this . makeRequest < SendMessageRequest , MessageData > (
5785 'POST' ,
5886 '/api/v1/public/send-messages' ,
5987 payload
6088 ) ;
89+
90+ return response . data ! . notificationId ;
91+ }
92+
93+ /**
94+ * Send a message using an awesome template path/identifier.
95+ */
96+ async sendAwesomeTemplate (
97+ recipientValue : string ,
98+ channel : string ,
99+ templateIdentifier : string ,
100+ templateVariables ?: Record < string , any > ,
101+ providerName ?: string ,
102+ providerCode ?: ProviderCode
103+ ) : Promise < string > {
104+ if ( ( providerName !== undefined ) !== ( providerCode !== undefined ) ) {
105+ throw new SirenValidationError ( 'Both providerName and providerCode must be provided together' ) ;
106+ }
107+
108+ const recipient = this . createRecipient ( channel , recipientValue ) ;
109+
110+ const payload : SendMessageRequest = {
111+ channel,
112+ templateIdentifier,
113+ recipient
114+ } ;
115+
116+ if ( templateVariables ) {
117+ payload . templateVariables = templateVariables ;
118+ }
119+
120+ if ( providerName && providerCode ) {
121+ payload . providerIntegration = {
122+ name : providerName ,
123+ code : providerCode
124+ } as ProviderIntegration ;
125+ }
126+
127+ const response = await this . makeRequest < SendMessageRequest , MessageData > (
128+ 'POST' ,
129+ '/api/v1/public/send-awesome-messages' ,
130+ payload
131+ ) ;
132+
61133 return response . data ! . notificationId ;
62134 }
63135
64136 /**
65137 * Retrieve the status of a specific message.
66- * @param messageId - The ID of the message for which to retrieve the status
67- * @returns The status of the message (e.g., "DELIVERED", "PENDING")
68138 */
69139 async getStatus ( messageId : string ) : Promise < string > {
70140 const response = await this . makeRequest < null , StatusData > (
71141 'GET' ,
72142 `/api/v1/public/message-status/${ messageId } `
73143 ) ;
144+
74145 return response . data ! . status ;
75146 }
76147
77148 /**
78149 * Retrieve replies for a specific message.
79- * @param messageId - The ID of the message for which to retrieve replies
80- * @returns A list of reply objects containing message details
81150 */
82151 async getReplies ( messageId : string ) : Promise < ReplyData [ ] > {
83152 const response = await this . makeRequest < null , ReplyData [ ] > (
84153 'GET' ,
85154 `/api/v1/public/get-reply/${ messageId } `
86155 ) ;
156+
87157 return response . data ! ;
88158 }
159+
160+ /**
161+ * Create the recipient object for the payload based on channel.
162+ */
163+ private createRecipient ( channel : string , recipientValue : string ) : Recipient {
164+ const key = CHANNEL_RECIPIENT_KEY [ channel . toUpperCase ( ) ] ;
165+ if ( ! key ) {
166+ throw new SirenValidationError ( `Unsupported channel: ${ channel } ` ) ;
167+ }
168+
169+ return {
170+ [ key ] : recipientValue
171+ } as Recipient ;
172+ }
89173}
0 commit comments