Skip to main content
Version: 0.15.17

Handler

A Bottender Handler includes many helpful methods and lets you easily handle different kinds of events from platforms. When an event comes in, the handler will choose the first method matched the condition to handle event.

API Reference

All methods return handler itself.

See more details about event and context.

onXXX(handler)

  • not includes onEvent, onUnhandled, onError

It will always trigger handler function if event.isXXX is true and just have one parameter.

ParamTypeDescription
handlerfunctionThis is a callback function receiving context as first parameter.

onXXX(predicate, handler)

  • not includes onEvent, onUnhandled, onError

It will trigger handler function if event.isXXX is true and predicate function return true.

ParamTypeDescription
predicatefunctionThis is a callback function receiving two parameters. Handler function will be triggered if it returns true.
function predicate(XXX, context) { /* ... */ }
handlerfunctionThis is a callback function receiving context as first parameter.
function handler(context) { /* ... */ }
  • Notices: onText, onPayload also support first parameter to be string type or RegExp. See more details from example below.

onEvent(handler)

It will trigger handler function from any event.

ParamTypeDescription
handlerfunctionThis is a callback function receiving context as first parameter.
function handler(context) { /* ... */ }

onUnhandled(handler)

It will trigger handler function if any onXXX function don't send any things back to user.

ParamTypeDescription
handlerfunctionThis is a callback function receiving context as first parameter.
function handler(context) { /* ... */ }

onError(handler)

It will trigger handler function if any Error is thrown.

ParamTypeDescription
handlerfunctionThis is a callback function receiving context as first parameter and error as second parameter.
function handler(context, error) { /* ... */ }

Methods table

NameMessengerLINESlackTelegramDescription
onit will trigger function from second parameter if function from first parameter return true.
onEventit will always trigger function from parameter.

Notice: This method will handle all event. Make sure this is the last method.
onMessageit will trigger function from parameter if context.event.isMessage is true and function from first parameter return true.
onTextit will trigger function from parameter if context.event.isText is true and function from first parameter return true.
onUnhandledit will trigger function from parameter if event is not handled.
onErrorit will trigger function from parameter if error happens.
onPostbackit will trigger function from parameter if context.event.isPostback is true and function from first parameter return true.
onPayloadit will trigger function from parameter if context.event.isPostback or context.event.isQuickReply is true and function from first parameter return true.
onLocationit will trigger function from parameter if context.event.isLocation is true and function from first parameter return true.
onAudioit will trigger function from parameter if context.event.isAudio is true and function from first parameter return true.
onVideoit will trigger function from parameter if context.event.isVideo is true and function from first parameter return true.
onPaymentit will trigger function from parameter if context.event.isPayment is true and function from first parameter return true.
onOptinit will trigger function from parameter if context.event.isOptin is true and function from first parameter return true.
onCheckoutUpdateit will trigger function from parameter if context.event.isCheckoutUpdate is true and function from first parameter return true.
onPreCheckoutit will trigger function from parameter if context.event.isPreCheckout is true and function from first parameter return true.
onQuickReplyit will trigger function from parameter if context.event.isQuickReply is true and function from first parameter return true.
onEchoit will trigger function from parameter if context.event.isEcho is true and function from first parameter return true.
onReadit will trigger function from parameter if context.event.isRead is true and function from first parameter return true.
onDeliveryit will trigger function from parameter if context.event.isDelivery is true and function from first parameter return true.
onImageit will trigger function from parameter if context.event.isImage is true and function from first parameter return true.
onFileit will trigger function from parameter if context.event.isFile is true and function from first parameter return true.
onFallbackit will trigger function from parameter if context.event.isFallback is true and function from first parameter return true.
onFollowit will trigger function from parameter if context.event.isFollow is true and function from first parameter return true.
onUnfollowit will trigger function from parameter if context.event.isUnfollow is true and function from first parameter return true.
onJoinit will trigger function from parameter if context.event.isJoin is true and function from first parameter return true.
onLeaveit will trigger function from parameter if context.event.isLeave is true and function from first parameter return true.
onBeaconit will trigger function from parameter if context.event.isBeacon is true and function from first parameter return true.
onCallbackQueryit will trigger function from parameter if context.event.isCallbackQuery is true and function from first parameter return true.
onPhotoit will trigger function from parameter if context.event.isPhoto is true and function from first parameter return true.
onDocumentit will trigger function from parameter if context.event.isDocument is true and function from first parameter return true.
onGameit will trigger function from parameter if context.event.isGame is true and function from first parameter return true.
onStickerit will trigger function from parameter if context.event.isSticker is true and function from first parameter return true.
onVoiceit will trigger function from parameter if context.event.isVoice is true and function from first parameter return true.
onVideoNoteit will trigger function from parameter if context.event.isVideoNote is true and function from first parameter return true.
onContactit will trigger function from parameter if context.event.isContact is true and function from first parameter return true.
onVenueit will trigger function from parameter if context.event.isVenue is true and function from first parameter return true.

Example

Let's use MessengerHandler as an example.

onText

User > yee
MessengerBot > yee.
User > yooooooo~
MessengerBot > Hi there!
User > I am going to sing a song for you.
MessengerBot > You talk too much!
User > yeeeeeee~
MessengerBot > I do not know what you said.
const handler = new MessengerHandler()
.onText('yee', async (context) => {
await context.sendText('yee.');
})
.onText(/yo/i, async (context) => {
await context.sendText('Hi there!');
})
.onText(
(text, context) => {
return text.length > 20;
},
async (context) => {
await context.sendText('You talk too much!');
}
)
.onText(async (context) => {
await context.sendText('I do not know what you said.');
});

onUnhandled

User > yooooooo~
MessengerBot > Oops. I do nothing.
const handler = new MessengerHandler()
.onText(async (context) => {
// event will come here first
// but you do nothing
})
.onUnhandled(async (context) => {
await context.sendText('Oops. I do nothing.');
});

onError

User > yooooooo~
MessengerBot > Oops. Error happens.
const handler = new MessengerHandler()
.onText((context) => {
throw new Error('Here comes error!');
})
.onError(async (context, err) => {
console.log(err.message); // Here comes error!
await context.sendText('Oops. Error happens.');
});