Skip to content

How to filter incoming messages#

Installation#

Before you begin, you need to install the library and initiate the bot; this process is described in detail here: How to import the library and initiate your bot.

How to filter incoming messages#

Filtering by webhook type occurs at the handler creation level, example - media-bot.js. If you need to filter a notification by its text, you can use the construct bot.hears('example', (ctx) => {})

Link to example: filter-bot.js.

const WhatsAppBot = require('@green-api/whatsapp-bot')

const bot = new WhatsAppBot({
     idInstance: "{{INSTANCE_ID}}",
     apiTokenInstance: "{{TOKEN}}",
})

bot.on('message', (ctx, next) => {
     ctx.reply('Send - test or cat or dog')
     next()
})
bot.hears('test', (ctx, next) => {
     ctx.reply('You write "test"')
     next()
})
bot.hears('cat', (ctx, next) => {
     ctx.reply('You write "cat"')
     next()
})
bot.hears('dog', (ctx, next) => {
     ctx.reply('You write "dog"')
     next()
})
bot.launch()

List of examples#

Description Link to example
How to initialize a handler hello-bot.js
Scene "Echo" echo-bot.js
How to filter by notification type media-bot.js
How to filter by message text filter-bot.js
How to work with bot state state-bot.js
Example of a ready-made chat bot demo-bot