Telegram bot .hears() - 具有多个触发器的方法? [英] Telegram bot .hears()-method with multiple triggers?

查看:34
本文介绍了Telegram bot .hears() - 具有多个触发器的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node.js 和 Telegraf 开发一个电报聊天机器人.

有没有办法将多个触发器与 bot.hears 方法一起用于电报机器人,例如使用 || 时的 if 语句?我希望如果用户输入菜单,就会出现一个菜单,当用户输入时,也会出现相同的菜单.

现在我必须写两次相同的方法,就像这样:

bot.hears('Menu', ctx =>{ctx.telegram.sendMessage(ctx.chat.id, '这些是我们的产品.',{//显示菜单})})bot.hears('返回', ctx =>{ctx.telegram.sendMessage(ctx.chat.id, '这些是我们的产品.',{//显示菜单})})

解决方案

您可以使用 RegEx 作为触发;

例如,像 /Menu|Back/ 将同时触发 /Menu 作为 /Back.

<小时>

examples 使用之一这种方法.查看来源实现的总体思路.

I am developing a telegram chat bot with node.js and telegraf.

Is there a way to use multiple triggers with the bot.hears method for telegram bots, like for if statements when you use ||? I want that if the user types menu, a menu appears and when the user types back, the same menu also appears.

Right now I have to write the same method twice, like this:

bot.hears('Menu', ctx =>
    {
        ctx.telegram.sendMessage(ctx.chat.id, 'These are our products.',
                {
                     //show menu
                })

    })

bot.hears('Back', ctx =>
    {
        ctx.telegram.sendMessage(ctx.chat.id, 'These are our products.',
                {
                     //show menu
                })

    })

解决方案

Looking at the .hears() documentation;

You can use a RegEx as a Trigger;

For example, a regex like /Menu|Back/ will trigger for both /Menu as /Back.


One of the examples uses this method. Take a look at the source to get a general idea on the implementation.

这篇关于Telegram bot .hears() - 具有多个触发器的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆