1 | import Contact from './Contact'
|
2 | import Attachment from './Attachment'
|
3 | import { Input } from './Input'
|
4 | import { InputSet } from './InputSet'
|
5 |
|
6 | class Message {
|
7 | private to: Contact[] = [];
|
8 | private content: string;
|
9 | private accessToken: string = '';
|
10 | private sentDate: number = 0;
|
11 | private messageId: string = '';
|
12 | private from: Contact;
|
13 | private teamToken: string = '';
|
14 | private accountAddress: string = '';
|
15 | private references: string[] = [];
|
16 | private subject: string = '';
|
17 | private type: string = 'message';
|
18 | private encoding: string = 'plain';
|
19 | private method: string = '';
|
20 | private replyTo: string = '';
|
21 | private inline: boolean = false;
|
22 | private cc: Contact[] = [];
|
23 | private bcc: Contact[] = [];
|
24 | private attachments: Attachment[] = [];
|
25 | private sentBy: string = '';
|
26 | private inputSet: InputSet;
|
27 |
|
28 | constructor (content: string) {
|
29 | this.content = content;
|
30 | }
|
31 |
|
32 | get SentDate(): number { return this.sentDate }
|
33 | get Content(): string { return this.content }
|
34 | get MessageId(): string { return this.messageId }
|
35 | get References(): string[] { return this.references }
|
36 | get From(): Contact { return this.from }
|
37 | get InputSet(): InputSet { return this.inputSet }
|
38 | get Subject(): string { return this.subject }
|
39 | get Inline(): boolean { return this.inline }
|
40 | get To() { return this.to }
|
41 | get AccessToken() { return this.accessToken }
|
42 | get TeamToken() { return this.teamToken }
|
43 | get AccountAddress() { return this.accountAddress }
|
44 | get Type() { return this.type }
|
45 | get Encoding() { return this.encoding }
|
46 | get Method() { return this.method }
|
47 | get ReplyTo() { return this.replyTo }
|
48 | get CC() { return this.cc }
|
49 | get BCC() { return this.bcc }
|
50 | get SentBy() { return this.sentBy }
|
51 |
|
52 | setSubject (subject: string) {
|
53 | this.subject = subject;
|
54 | return this;
|
55 | }
|
56 |
|
57 | setReceivers (receiver: Contact[]) {
|
58 | this.to = receiver;
|
59 | return this;
|
60 | }
|
61 |
|
62 | setAccessToken (accessToken: string): Message {
|
63 | this.accessToken = accessToken;
|
64 | return this;
|
65 | }
|
66 |
|
67 | setInline (inline: boolean): Message {
|
68 | this.inline = inline;
|
69 | return this;
|
70 | }
|
71 |
|
72 | setSentDate (sentDate: number): Message {
|
73 | this.sentDate = sentDate;
|
74 | return this;
|
75 | }
|
76 |
|
77 | setMessageId (messageId: string): Message {
|
78 | this.messageId = messageId;
|
79 | return this;
|
80 | }
|
81 |
|
82 | setTeamToken (teamToken: string): Message {
|
83 | this.teamToken = teamToken;
|
84 | return this;
|
85 | }
|
86 |
|
87 | setType (type: string): Message {
|
88 | this.type = type;
|
89 | return this;
|
90 | }
|
91 |
|
92 | setAccountAddress (accountAddress: string): Message {
|
93 | this.accountAddress = accountAddress;
|
94 | return this;
|
95 | }
|
96 |
|
97 | setEncoding (encoding: string): Message {
|
98 | this.encoding = encoding;
|
99 | return this;
|
100 | }
|
101 |
|
102 | setMethod (method: string): Message {
|
103 | this.method = method;
|
104 | return this;
|
105 | }
|
106 |
|
107 | setReplyTo (replyTo: string): Message {
|
108 | this.replyTo = replyTo;
|
109 | return this;
|
110 | }
|
111 |
|
112 | setSentBy (sentBy: string): Message {
|
113 | this.sentBy = sentBy;
|
114 | return this;
|
115 | }
|
116 |
|
117 | setFrom (from: Contact): Message {
|
118 | this.from = from;
|
119 | return this;
|
120 | }
|
121 |
|
122 | setCC (cc: Contact[]): Message {
|
123 | this.cc = cc;
|
124 | return this;
|
125 | }
|
126 |
|
127 | setBCC (bcc: Contact[]): Message {
|
128 | this.bcc = bcc;
|
129 | return this;
|
130 | }
|
131 |
|
132 | setReferences (references: string[]): Message {
|
133 | this.references = references;
|
134 | return this;
|
135 | }
|
136 |
|
137 | setAttachments (attachments: Attachment[]): Message {
|
138 | this.attachments = attachments;
|
139 | return this;
|
140 | }
|
141 |
|
142 | setInputSet (inputSet: InputSet) {
|
143 | this.inputSet = inputSet;
|
144 | return this;
|
145 | }
|
146 |
|
147 | getAttachments (): Attachment[] {
|
148 | return this.attachments;
|
149 | }
|
150 |
|
151 | buildCore (token: string, botAddress: string, botName:string): Message {
|
152 | let date = Date.now();
|
153 |
|
154 | this
|
155 | .setAccessToken(token)
|
156 | .setEncoding('plain')
|
157 | .setSentDate(date)
|
158 | .setMessageId(this.getMessageId(date, botAddress))
|
159 | .setFrom(new Contact(botAddress).setName(botAddress))
|
160 | .setAccountAddress(botAddress)
|
161 | .setSentBy(botAddress)
|
162 |
|
163 | return this;
|
164 | }
|
165 |
|
166 | getMessageId(date: number, botAddress: string): string {
|
167 | let id = '<' + date + '.'
|
168 | const chars = '0123456789'
|
169 | for (let i = 0; i < 7; i++) {
|
170 | id += chars.charAt(Math.floor(Math.random() * 10))
|
171 | }
|
172 | id += this.getHostName(botAddress);
|
173 | return id + '>'
|
174 | }
|
175 |
|
176 | private getHostName (botAddress: string): string {
|
177 | const index = botAddress.indexOf('@')
|
178 | return botAddress.substring(index, botAddress.length)
|
179 | }
|
180 |
|
181 | toJSON (): any {
|
182 | return {
|
183 | access_token: this.accessToken,
|
184 | account_address: this.accountAddress,
|
185 | subject: this.subject,
|
186 | sent_date: this.sentDate,
|
187 | message_id: this.messageId,
|
188 | team_token: this.teamToken,
|
189 | content: this.content,
|
190 | type: this.type,
|
191 | encoding: this.encoding,
|
192 | method: this.method,
|
193 | reply_to: this.replyTo,
|
194 | to: this.to.map((contact: Contact) => contact.toJSON()),
|
195 | from: this.from.toJSON(),
|
196 | cc: this.cc.map((contact: Contact) => contact.toJSON()),
|
197 | bcc: this.bcc.map((contact: Contact) => contact.toJSON()),
|
198 | references: this.references,
|
199 | inline: this.inline,
|
200 | attachments: this.attachments.map((attachment: Attachment) => attachment.toJSON()),
|
201 | sent_by: this.sentBy,
|
202 | input_set: this.inputSet ? this.inputSet.toJSON() : null
|
203 | }
|
204 | }
|
205 | }
|
206 |
|
207 | export default Message;
|