File size: 1,388 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ChatwootClient from '@figuro/chatwoot-sdk';
import type { conversation_message_create } from '@figuro/chatwoot-sdk/dist/models/conversation_message_create';
import { MessageType } from '@waha/apps/chatwoot/client/types';

export class Conversation {
  public onError: (e: any) => void;

  constructor(
    private accountAPI: ChatwootClient,
    private accountId: number,
    public conversationId: number,
  ) {}

  public async send(data: conversation_message_create) {
    try {
      const message = await this.accountAPI.messages.create({
        accountId: this.accountId,
        conversationId: this.conversationId,
        data: data,
      });
      return message;
    } catch (err) {
      if (this.onError) {
        this.onError(err);
      }
      throw err;
    }
  }

  public async incoming(text: string) {
    const data: conversation_message_create = {
      content: text,
      message_type: MessageType.INCOMING as any,
    };
    return this.send(data);
  }

  public async activity(text: string) {
    const data: conversation_message_create = {
      content: text,
      message_type: MessageType.ACTIVITY as any,
    };
    return this.send(data);
  }

  public async outgoing(text: string) {
    const data: conversation_message_create = {
      content: text,
      message_type: MessageType.OUTGOING as any,
    };
    return this.send(data);
  }
}