-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMessageApiClient.ts
165 lines (143 loc) · 5.66 KB
/
MessageApiClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as CMTypes from "../typescript-node-client/api";
import axios = require('axios');
export type Channel = "SMS" | "Viber" | "RCS" | "Apple Messages for Business" | "WhatsApp" | "Telegram Messenger" | "Twitter" | "MobilePush" | "Facebook Messenger" | "Google Business Messages" | "Instagram";
export type RichMessage = CMTypes.RichMessage;
export type Suggestion = CMTypes.Suggestion;
export type Template = CMTypes.Template;
export type MessagesResponse = CMTypes.MessagesResponse;
export type WhatsAppInteractive = CMTypes.WhatsAppInteractive;
/**
* Message client for the CM.com Platform
*/
export class MessageApiClient {
private readonly productToken: string;
/**
* Create a new client instance with a provided authentication key
* @param productToken Private autorization token for CM.com
*/
constructor(productToken: string) {
this.productToken = productToken;
}
/**
* Create a new message builder
*/
public createMessage() : Message {
return new Message(this.productToken);
}
/**
* Send an SMS message
* @param to array of recipients for the message, specify the numbers in international format with leading 00
* For Twitter: use the Twitter Snowflake ID
* @param from the sender of the message, specify valid Sender ID.
* For Twitter: use the Twitter Snowflake ID of the account you want to use as sender.
* @param message the body of the SMS message to be sent
* @param reference (optional) reference to the message to query it later in the CM.platform.
*/
public sendTextMessage(to: string[], from: string, message: string, reference: string = undefined) {
return this
.createMessage()
.setMessage(to, from, message, reference)
.send();
}
}
/**
* Message object to send via the CM.com platform
*/
export class Message extends CMTypes.MessageEnvelope {
private api: CMTypes.MessagesApi;
/**
* @deprecated Please use the MessageApiClient.createMessage instead.
*/
constructor(productToken: string) {
super();
this.api = new CMTypes.MessagesApi();
this.messages = new CMTypes.Messages();
this.messages.authentication = new CMTypes.Authentication();
this.messages.authentication.productToken = productToken;
}
/**
* Sets the essential message parameters.
* @param to array of recipients for the message, specify the numbers in international format with leading 00.
* For Twitter: use the Twitter Snowflake ID
* @param from the sender of the message, specify valid Sender ID.
* For Twitter: use the Twitter Snowflake ID of the account you want to use as sender.
* @param message the body of the SMS message to be sent
* @param reference (optional) reference to the message to query it later in the CM.platform.
*/
public setMessage(to: string[], from: string, message: string, reference: string = undefined): Message {
const msg = new CMTypes.Message();
msg.customGrouping3 = "text-sdk-javascript";
msg.from = from;
msg.body = new CMTypes.MessageBody();
msg.body.type = "AUTO";
msg.body.content = message;
msg.reference = reference;
msg.to = this.createRecipients(to);
this.messages.msg = new Array<CMTypes.Message>();
this.messages.msg.push(msg);
return this;
}
/**
* Sets the allowed channels to use. Default is to allow any channel configured for your account
* @param channels array of allowed channels.
* Any of "SMS", "Viber", "RCS", "Apple Business Chat", "WhatsApp", "Telegram Messenger" and "Twitter"
*/
public setAllowedChannels(channels: Channel[]): Message {
this.messages.msg[0].allowedChannels = channels || [];
return this;
}
/**
* Sets the rich message conversation
* @param conversation array of rich message conversation objects
*/
public setConversation(conversation: RichMessage[]): Message {
this.getRichContent().conversation = conversation;
return this;
}
/**
* Sets the rich message suggestions
* @param conversation array of rich message suggestion objects
*/
public setSuggestion(suggestions: Suggestion[]): Message {
this.getRichContent().suggestions = suggestions;
return this;
}
/**
* Sets the rich message template
* @param template template definition and usage object
*/
public setTemplate(template: Template): Message {
this.getRichContent().conversation = [{ template: template }];
return this;
}
/**
* Sets the WhatsAppInteractive Message
* @param template template definition and usage object
*/
public setInteractive(interactive: WhatsAppInteractive): Message {
this.getRichContent().conversation = [{ interactive: interactive }];
return this;
}
/**
* Sends the message to the CM.com Platform
*/
public send(): Promise<axios.AxiosResponse<any, any>> {
return this.api.messagesSendMessage(this);
}
private createRecipients(recipients: string[]): CMTypes.Recipient[] {
return recipients.map((number: string) => {
const recipient: CMTypes.Recipient = {
number: number
};
return recipient;
})
}
private getRichContent(): CMTypes.RichContent {
if (!this.messages.msg[0].richContent) {
this.messages.msg[0].richContent = new CMTypes.RichContent();
}
return this.messages.msg[0].richContent;
}
}
// exported for backwards compatibility
export { CMTypes };