Skip to content

WIP: Typescript definitions #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
export interface UserProfile {
id: string,
name: string,
avatar: string,
/** iso alpha 2 code */
country: string,
language: string,
apiVersion: number;
}

export interface ReceivedTextMessage {
text: string,
/** milliseconds */
timestamp: number,
token: string,
trackingData: any,
keyboard: any,
requiredArguments: string[],
minApiVersion: undefined;
}

type ConversationStarted = (userProfile: UserProfile,
isSubscribed: boolean,
context: any,
onFinish: any) => any;

export interface Bot {
onConversationStarted: (fn: ConversationStarted)=> void;
onTextMessage: (pattern: RegExp, callback: (message: ReceivedTextMessage, response: any) => any) => any;
middleware: () => any;
onError: (callback: (error: any) => any) => any;
setWebhook: (webhook: string) => Promise<any>;
onSubscribe: (callback: (response: Response) => void ) => void;
sendMessage: (minUserProfile: { id: string }, messages: any[]) => Promise<any>;
onUnsubscribe: (callback: (userId: string) => void) => void;
on: (eventName: string, handler: any)=>void;
}

export interface Keyboard {
Type: 'keyboard',
/** hex color */
BgColor: string;
Buttons: KeyboardButton[];
InputFieldState: 'hidden' | 'regular' | 'minimized',
}

export interface KeyboardButton {
Columns?: number,
Rows?: number,
ActionType?: "reply" | "open-url" | "none",
ActionBody: string,
/** hex color */
BgColor?: string;
Text: string;
TextVAlign?: string,
TextHAlign?: string,
TextSize?: 'regular'
Silent?: boolean;
OpenURLType?: string,
InternalBrowser?: {
ActionButton?: string;
ActionPredefinedURL?: string;
Mode?: string;
CustomTitle?: string,
}
}

export interface RichMedia {
ButtonsGroupColumns: number,
ButtonsGroupRows: number,
BgColor: string,
Buttons: RichMediaButton[]
}

export interface RichMediaButton {
Columns?: number;
Rows?: number;
ActionType?: "reply" | "open-url" | "none";
ActionBody?: string;
Image?: string;
OpenURLType?: string;
Text?: string;
TextSize?: string;
TextVAlign?: string;
TextHAlign?: string;
BgColor?: string;
InternalBrowser?: {
ActionButton: string;
ActionPredefinedURL?: string;
}
Silent?: boolean;
}

export interface Response {
userProfile: UserProfile,
send: (messages: any | any[]) => void;
}

export interface StickerMessage {
new (stickerId: number): StickerMessage;
}

export interface KeyboardMessage {
new (
keyboard: Keyboard,
optionalTrackingData?: string,
timestamp?: string,
token?: string,
minApiVersion?: number,
): KeyboardMessage;
}

export interface RichMediaMessage {
new (
richMedia: RichMedia,
optionalKeyboard?: Keyboard,
optionalTrackingData?: string,
timestamp?: string,
token?: string,
optionalAltText?: string,
minApiVersion?: number,
): RichMediaMessage;
}

export interface TextMessage {
new (
message: string,
keyboard?: Keyboard,
optionalTrackingData?: string,
timestamp?: string,
token?: string,
minApiVersion?: number,
): TextMessage;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A bot interface to work with Viber API",
"author": "Viber LTD",
"engine-strict": true,
"types": "index.d.ts",
"engines": {
"node": ">=5",
"npm": ">=3"
Expand Down Expand Up @@ -32,4 +33,4 @@
"express": "^4.14.0",
"nodeunit": "^0.11.3"
}
}
}