Skip to content

Latest commit

 

History

History
160 lines (107 loc) · 4.61 KB

botserver.md

File metadata and controls

160 lines (107 loc) · 4.61 KB

@aiteq/messenger-bot > BotServer

Class: BotServer

Represents a bot server listening for webhook requests.

Example:

new BotServer({
    name: "MyBot",
    verifyToken: "hasta-la-vista-baby",
    accessToken: "<your access token>",
    appSecret: "<your app secret>"
})
.hear("hello", (chat: Chat) => {
    chat.say("Hello! I'm Emil, the Bot. How are you?");
})
.start();

Index

Constructors

Methods


Constructors

new BotServer(config)

Creates an instance of the BotServer.

Parameters:

Param Type Description
config BotConfig bot configuration object

Returns: BotServer - an instance created


Methods

addChatExtension(extension)

Install a Chat Extension.

Parameters:

Param Type Description
extension ChatExtension a Chat Extension to be installed

Returns: this


hear(hooks, hearHandler)

A convenient method to subscribe to specific hooks that can be found within a received text message. The hooks can be specified as a command or using a regular expression. When the server receives a text message, it test the content for specified hooks. If a match is found the server executes the subscribed callback. Commands are considered as case-insensitive.

The callback is executed with three parameters:

# Type Description
1. Chat an instance of Chat to be used for replaying
2. string original message
3. string[] an array of captured matches if the regular expression contains capturing groups

Note: The callbacks installed using hear() are executed BEFORE callbacks installed using on().

Note: the hear() method listens only for TEXT messages.

Note: The callback is not executed when a received text message matches the hook but the message is part of an active conversation.

Parameters:

Param Type Description
hooks RegExpstringArray<RegExpstring> a string, a regexp or a mixed array of both strings and regexps
callback (chat: Chat, text: string, matches: string[]) => void a callback to be executed if a message matches one of the hooks

Returns: this - for chaining


on(event, callback)

Subscribe to an event emitted when a webhook request is received.

The callback is executed with two parameters:

# Type Description
1. Chat an instance of Chat to be used for replaying
2. any event specific data (e.g. original text message)

Parameters:

Param Type Description
event Event an event for which the callback will be executed
callback (chat: Chat, ...args: any[]) => void a callback function

Returns: this - for chaining

on(event, id, callback)

Subscribe to an identified event. An identified event is specified, in addition to its type, with an ID. This feature is available for events capable of carrying data such as POSTBACK or PERSISTENT_MENU_ITEM.

The callback is executed with two parameters:

# Type Description
1. Chat an instance of Chat to be used for replaying
2. any event specific data (e.g. original text message)

Parameters:

Param Type Description
event Event an event for which the callback will be executed
id string an identification of the event
callback (chat: Chat, ...args: any[]) => void a callback function

Returns: this - for chaining


start()

Starts the bot server.

Returns: void


stop()

Stops the bot server.

Returns: void