UNPKG

7.94 kBTypeScriptView Raw
1/// <reference types="node" />
2import { EventEmitter } from "events";
3import { Express } from "express";
4import { Server } from "http";
5import { Options as HttpOptions, ScopedClient } from "scoped-http-client";
6
7export class Adapter extends EventEmitter {
8 robot: Robot;
9 constructor(robot: Robot);
10
11 send(envelope: Envelope, ...strings: string[]): void;
12 emote(envelope: Envelope, ...strings: string[]): void;
13 reply(envelope: Envelope, ...strings: string[]): void;
14 topic(envelope: Envelope, ...strings: string[]): void;
15 play(envelope: Envelope, ...strings: string[]): void;
16
17 run(): void;
18 close(): void;
19
20 receive(message: Message): void;
21 http(url: string): ScopedClient;
22
23 /**
24 * @deprecated
25 * @returns an object containing all the users with property names
26 * that are the user id and with the value set as the `User`.
27 * You can iterate over them all with `for (const id in users())`
28 */
29 users(): any;
30 userForId(id: string, options?: {}): User;
31 userForName(name: string): User | null;
32 usersForRawFuzzyName(fuzzyName: string): User[];
33 usersForFuzzyName(fuzzyName: string): User[];
34}
35
36export class DataStore {
37 constructor(robot: Robot);
38 set(key: string, value: any): Promise<void>;
39 setObject(key: string, objectKey: string, value: any): Promise<void>;
40 setArray(key: string, value: any): Promise<void>;
41 get(key: string): Promise<any>;
42 getObject(key: string, objectKey: string): Promise<any>;
43}
44
45export class DataStoreUnavailable extends Error {}
46
47export class Middleware<T extends Adapter = Adapter> {
48 stack: Array<MiddlewareHandler<T>>;
49 constructor(robot: Robot<T>);
50 execute(context: MiddlewareContext<T>, next: NextFunction, done: DoneFunction): void;
51 register(middleware: MiddlewareHandler<T>): void;
52}
53
54export class Brain<A extends Adapter> extends EventEmitter {
55 constructor(robot: Robot<A>);
56 set(key: string, value: any): this;
57 get(key: string): any;
58 remove(key: string): this;
59 save(): void;
60 close(): void;
61 setAutoSave(enabled: boolean): void;
62 resetSaveInterval(seconds: number): void;
63 mergeData(data: {}): void;
64 /**
65 * @returns an object containing all the users with property names
66 * that are the user id and with the value set as the `User`.
67 * You can iterate over them all with `for (const id in users())`
68 */
69 users(): any;
70 userForId(id: string, options?: {}): User;
71 userForName(name: string): User | null;
72 usersForRawFuzzyName(fuzzyName: string): User[];
73 usersForFuzzyName(fuzzyName: string): User[];
74}
75
76export class User {
77 constructor(id: string, options?: {});
78 id: string;
79 name: string;
80 set(key: string, value: any): this;
81 get(key: string): any;
82 [property: string]: unknown;
83}
84
85export class Message {
86 constructor(user: User, done?: boolean);
87 id: string;
88 user: User;
89 text: string | null;
90 room: string;
91 finish(): void;
92}
93
94export class TextMessage extends Message {
95 text: string;
96
97 constructor(user: User, text: string, id: string);
98
99 match(regex: RegExp): RegExpMatchArray;
100 toString(): string;
101}
102
103export class EnterMessage extends Message {
104 text: null;
105}
106
107export class LeaveMessage extends Message {
108 text: null;
109}
110
111export class TopicMessage extends TextMessage {
112 text: string;
113}
114
115export class CatchAllMessage extends Message {
116 message: Message;
117
118 constructor(message: Message);
119}
120
121export interface Envelope {
122 room: string;
123 user: User;
124 message: Message;
125}
126
127export class Response<
128 A extends Adapter = Adapter,
129 M extends Message = Message,
130 R extends RegExpMatchArray | { [key: string]: string } = RegExpMatchArray,
131> {
132 robot: Robot<A>;
133 match: R;
134 message: M;
135 envelope: Envelope;
136
137 constructor(robot: Robot<A>, message: M, match: R);
138 send(...strings: string[]): void;
139 emote(...strings: string[]): void;
140 reply(...strings: string[]): void;
141 topic(...strings: string[]): void;
142 play(...strings: string[]): void;
143 locked(...strings: string[]): void;
144 random<T>(items: T[]): T;
145 finish(): void;
146 http(url: string, options?: HttpOptions): ScopedClient;
147}
148
149export type ListenerCallback<A extends Adapter = Adapter, M extends Message = Message> = (
150 response: Response<A, M>,
151) => void;
152export type DoneFunction = () => void;
153export type NextFunction = (done: DoneFunction) => void;
154export interface MiddlewareContext<T extends Adapter = Adapter> {
155 response?: Response<T> | undefined;
156 [key: string]: unknown;
157}
158export type MiddlewareHandler<T extends Adapter = Adapter> = (
159 context: MiddlewareContext<T>,
160 next: NextFunction,
161 done: DoneFunction,
162) => void;
163
164export interface LogLevel {
165 (...messages: any[]): void;
166 disable(): void;
167 enable(): void;
168}
169
170export interface Log {
171 (...messages: any[]): void;
172 get(namespace: string): Log;
173 debug: LogLevel;
174 info: LogLevel;
175 notice: LogLevel;
176 warning: LogLevel;
177 error: LogLevel;
178}
179
180export class Robot<A extends Adapter = Adapter> {
181 readonly name: string;
182 readonly events: EventEmitter;
183 readonly brain: Brain<A>;
184 readonly alias: string;
185 readonly adapterPath: string;
186 readonly adapterName: string;
187 readonly adapter: A;
188 readonly errorHandlers: [];
189 readonly onUncaughtException: (err: Error) => void;
190 readonly datastore: null | DataStore;
191 readonly commands: [];
192 readonly middleware: {
193 listener: Middleware<A>;
194 response: Middleware<A>;
195 receive: Middleware<A>;
196 };
197 readonly logger: Log;
198 readonly pingIntervalId: null | NodeJS.Timeout;
199 readonly globalHttpOptions: HttpOptions;
200 readonly version: string;
201 readonly server?: Server | undefined;
202 readonly router: Express;
203
204 constructor(adapterPath: string, adapter: string, httpd: boolean, name: string, alias?: string);
205 catchAll(callback: ListenerCallback<A, CatchAllMessage>): void;
206 catchAll(options: {}, callback: ListenerCallback<A, CatchAllMessage>): void;
207 emit(event: string | symbol, ...args: unknown[]): void;
208 enter(callback: ListenerCallback<A, EnterMessage>): void;
209 enter(options: {}, callback: ListenerCallback<A, EnterMessage>): void;
210 error(cb: (error: Error) => void): void;
211 hear(regex: RegExp, callback: ListenerCallback<A, TextMessage>): void;
212 hear(regex: RegExp, options: {}, callback: ListenerCallback<A, TextMessage>): void;
213 helpCommands(): string[];
214 http(url: string, options?: HttpOptions): ScopedClient;
215 leave(callback: ListenerCallback<A, LeaveMessage>): void;
216 leave(options: {}, callback: ListenerCallback<A, LeaveMessage>): void;
217 listen(matcher: (message: Message) => boolean, callback: ListenerCallback<A>): void;
218 listen(matcher: (message: Message) => boolean, options: {}, callback: ListenerCallback<A>): void;
219 listenerMiddleware(middleware: MiddlewareHandler<A>): void;
220 loadExternalScripts(packages: string[]): void;
221 loadFile(directory: string, fileName: string): void;
222 loadHubotScripts(path: string, scripts: string[]): void;
223 messageRoom(room: string, ...strings: string[]): void;
224 on(event: string | symbol, listener: (...args: unknown[]) => void): this;
225 receive(message: Message, cb?: () => void): void;
226 receiveMiddleware(middleware: MiddlewareHandler<A>): void;
227 reply(envelope: Envelope, ...strings: string[]): void;
228 respond(regex: RegExp, callback: ListenerCallback<A, TextMessage>): void;
229 respond(regex: RegExp, options: {}, callback: ListenerCallback<A, TextMessage>): void;
230 respondPattern(regex: RegExp): RegExp;
231 responseMiddleware(middleware: MiddlewareHandler<A>): void;
232 run(): void;
233 send(envelope: Envelope, ...strings: string[]): void;
234 shutdown(): void;
235 topic(callback: ListenerCallback<A, TopicMessage>): void;
236 topic(options: {}, callback: ListenerCallback<A, TopicMessage>): void;
237}