@aiteq/messenger-bot > BotServer
Represents a bot server listening for webhook requests.
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();
- addChatExtension(extension)
- hear(hooks, callback)
- on(event, callback)
- on(event, id, callback)
- start
- stop
Creates an instance of the BotServer.
Parameters:
Param | Type | Description |
---|---|---|
config | BotConfig | bot configuration object |
Returns: BotServer - an instance created
Install a Chat Extension.
Parameters:
Param | Type | Description |
---|---|---|
extension | ChatExtension | a Chat Extension to be installed |
Returns: this
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 | RegExp ⎮ string ⎮ Array <RegExp ⎮ string > |
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
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
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
Starts the bot server.
Returns: void
Stops the bot server.
Returns: void