-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.d.ts
More file actions
167 lines (149 loc) · 5.14 KB
/
index.d.ts
File metadata and controls
167 lines (149 loc) · 5.14 KB
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
166
167
/// <reference types="node" />
import WebSocket = require('ws');
// ---------------------------------------------------------------------------
// Message payload interfaces
// ---------------------------------------------------------------------------
/** Fired when a new post is sent in a channel (action: 'post/add') */
export interface PostAddMessage {
action: 'post/add';
/** The text content of the post */
message: string;
/** The channel the post was sent in */
channel_id: string;
/** ID of the user who sent the post */
user_id: number;
}
/** Fired when the bot is mentioned in a post (action: 'post/mention') */
export interface PostMentionMessage {
action: 'post/mention';
/** The text content of the post containing the mention */
message: string;
/** The channel the mention occurred in */
channel_id: string;
/** ID of the user who mentioned the bot */
user_id: number;
}
/** Fired when a user submits a bumote form on a post (action: 'post/bumote') */
export interface PostBumoteMessage {
action: 'post/bumote';
/** Contains the submitted form fields and submit button label */
message: {
/** Key/value pairs of form fields (e.g. `{ name: 'Hasan', burc: 'Koç' }`) */
form: Record<string, string>;
/** Label of the submit button */
submit: string;
};
/** ID of the post the bumote was submitted on */
post_id: number;
/** ID of the user who submitted the form */
user_id: number;
}
/** Fired when a user sends a direct message (action: 'message/send') */
export interface MessageSendMessage {
action: 'message/send';
/** The text content of the message */
message: string;
/** ID of the user who sent the message */
user_id: number;
}
/** Fired when a user joins the group (action: 'group/join') */
export interface GroupJoinMessage {
action: 'group/join';
/** ID of the group */
group_id: number;
/** ID of the user who joined */
user_id: number;
}
/** Fired when a user leaves the group (action: 'group/leave') */
export interface GroupLeaveMessage {
action: 'group/leave';
/** ID of the group */
group_id: number;
/** ID of the user who left */
user_id: number;
}
/** Fired when a user is kicked from the group (action: 'group/kick') */
export interface GroupKickMessage {
action: 'group/kick';
/** ID of the group */
group_id: number;
/** ID of the user who was kicked */
user_id: number;
}
/** Fired when a turbo transfer is made to the bot (action: 'turbo/transfer') */
export interface TurboTransferMessage {
action: 'turbo/transfer';
message: {
/** Optional note/message attached to the transfer */
message: string;
/** Amount of turbo transferred */
quantity: number;
};
/** ID of the transfer */
transfer_id: number;
/** ID of the user who sent the transfer */
user_id: number;
}
/** Union of all possible message payloads received via bot.on('message', ...) */
export type BotMessage =
| PostAddMessage
| PostMentionMessage
| PostBumoteMessage
| MessageSendMessage
| GroupJoinMessage
| GroupLeaveMessage
| GroupKickMessage
| TurboTransferMessage;
// ---------------------------------------------------------------------------
/**
* Represents the bot instance returned by TopluyoBOT()
*/
export interface BotInstance {
/** The underlying WebSocket connection */
ws: WebSocket;
/**
* Send a single API request to the Topluyo platform
* @param api API endpoint name
* @param data Request payload
*/
post(api: string, data?: Record<string, any>): Promise<any>;
/** Fired when the WebSocket connection is opened */
on(event: 'open', callback: (this: BotInstance) => void): void;
/** Fired when the bot is successfully authenticated */
on(event: 'connected', callback: (this: BotInstance) => void): void;
/** Fired when the WebSocket connection is closed */
on(event: 'close', callback: (this: BotInstance) => void): void;
/** Fired when authentication fails */
on(event: 'auth_problem', callback: (this: BotInstance) => void): void;
/**
* Fired when a message or event is received from the server.
* Use `data.action` to distinguish between event types:
* `'post/add'` | `'post/bumote'` | `'message/send'` | `'group/join'` | `'group/leave'` | `'group/kick'`
*/
on(event: 'message', callback: (this: BotInstance, data: BotMessage) => void): void;
/** Fired when a WebSocket error occurs */
on(event: 'error', callback: (this: BotInstance, err: Error) => void): void;
/**
* Wildcard listener — receives all events.
* The first argument is the event name, the second is the event data.
*/
on(event: '*', callback: (this: BotInstance, event: string, data: BotMessage | Error | undefined) => void): void;
/** Opens the WebSocket connection (called automatically on construction) */
connect(): void;
}
/**
* Creates and connects a new TopluyoBOT instance
* @param token Bot authentication token
* @returns Connected BotInstance
*
* @example
* // CommonJS
* const TopluyoBOT = require('topluyo-bot');
* const bot = TopluyoBOT('your-token');
*
* bot.on('message', function(data) {
* console.log(data);
* });
*/
declare function TopluyoBOT(token: string): BotInstance;
export = TopluyoBOT;