Module

x/grammy/mod.ts>Composer#hears

The Telegram Bot Framework.
Very Popular
Latest
method Composer.prototype.hears
import { Composer } from "https://dotland.deno.dev/x/grammy@v1.31.0/mod.ts";

Registers some middleware that will only be executed when the message contains some text. Is it possible to pass a regular expression to match:

// Match some text (exact match)
bot.hears('I love grammY', ctx => ctx.reply('And grammY loves you! <3'))
// Match a regular expression
bot.hears(/\/echo (.+)/, ctx => ctx.reply(ctx.match[1]))

Note how ctx.match will contain the result of the regular expression. Here it is a RegExpMatchArray object, so ctx.match[1] refers to the part of the regex that was matched by (.+), i.e. the text that comes after “/echo”.

You can pass an array of triggers. Your middleware will be executed if at least one of them matches.

Both text and captions of the received messages will be scanned. For example, when a photo is sent to the chat and its caption matches the trigger, your middleware will be executed.

If you only want to match text messages and not captions, you can do this:

// Only matches text messages (and channel posts) for the regex
bot.on(':text').hears(/\/echo (.+)/, ctx => { ... })

Parameters

trigger: MaybeArray<string | RegExp>

The text to look for

...middleware: Array<HearsMiddleware<C>>

The middleware to register