Naltox / telegram-node-bot

Node module for creating Telegram bots.
MIT License
724 stars 143 forks source link

TextCommand multiple strings at once #171

Open Khalanar opened 7 years ago

Khalanar commented 7 years ago

Hi all,

Is there any way of checking for multiple strings in a single call? Something like when ( new TextCommand('hello' || 'Hello', 'greetingCommand'), GreetingController())

I've tried several things and have been utterly unsuccessful.

Thanks!

Khalanar commented 7 years ago

I am trying to pass an array too:

var hello = ["hello", "Hello", "HELLO"];

when ( new TextCommand(hello, 'greetingCommand'), GreetingController())

But it's not working either. Am i doing something wrong?

Thanks!

jesusgn90 commented 7 years ago

Maybe you should try with .otherwise and then filter the command or message inside!

Khalanar commented 7 years ago

Hi Jesús, thanks for your reply. I actually managed to fix this with regex. If someone else is interested, you can pass several strings to a regex like this: /string1|string2|string3/i Where "i" marks all strings as non case sensitive.

Snippet of my actual code: var hello = /hola|hello|hi|ey/i; tg.router .when(new Telegram.RegexpCommand ( hello, 'helloCommand'), todoCtrl)

I hope it helps any one else trying to achieve something similar.

magikcypress commented 7 years ago

A solution is :

class RegexController extends TelegramBaseController {

  helloregexHandler($) {
      $.sendMessage('Hi !')
  }

  get routes() {
      return {
        'helloregexCommand': 'helloregexHandler'
      }
  }
}

var hello = /hola|hello|hi|ey/i;

tg.router
  .when(
    new RegexpCommand(hello, 'helloregexCommand'),
    new RegexController()
  )

:+1: