-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchat.ts
71 lines (63 loc) · 2.08 KB
/
chat.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
import { JSONObject, DTOLike } from '~/dto'
import { AddMessagePayload, AddMessageResponse } from './message'
import { DeliveryStatusRequest } from './delivery-status'
import { TypingRequest } from './typing'
import { Channel } from './channel'
export type ChatOptions = {
channel: Channel,
scopeId: string,
title?: string,
mainDomain?: string
}
export type ChatDescription = {
scopeId: string
}
/**
* Main Chat API
*/
export class Chat {
channel: Channel
scopeId: string
title?: string
mainDomain: string
constructor(chatOptions: ChatOptions) {
const {
channel, scopeId, title,
mainDomain = 'amocrm.ru'
} = chatOptions
this.channel = channel
this.scopeId = scopeId
this.title = title
this.mainDomain = mainDomain
}
async addMessage(addMessagePayload: DTOLike<AddMessagePayload>): Promise<AddMessageResponse> {
const addMessagePayloadImported = AddMessagePayload.create(addMessagePayload)
const { channel, scopeId, mainDomain } = this
const { data } = await channel.post<JSONObject>(
{
url: `/v2/origin/custom/${scopeId}`,
mainDomain,
data: { event_type: 'new_message', payload: AddMessagePayload.export(addMessagePayloadImported) }
}
)
return AddMessageResponse.import(data)
}
async deliveryStatus(messageId: string, deliveryStatusRequest: DTOLike<DeliveryStatusRequest>): Promise<void> {
const { channel, scopeId, mainDomain } = this
await channel.post({
url: `/v2/origin/custom/${scopeId}/${messageId}/delivery_status`,
mainDomain,
data: deliveryStatusRequest,
dto: DeliveryStatusRequest
})
}
async typing(typingRequest: DTOLike<TypingRequest>) {
const { channel, scopeId, mainDomain } = this
await channel.post({
url: `/v2/origin/custom/${scopeId}/typing`,
mainDomain,
data: typingRequest,
dto: TypingRequest
})
}
}