depoio / node-telegram-bot

Client wrapper for Telegram Bot API (Under heavy development)
MIT License
136 stars 30 forks source link

Fix for command parsing (#45) #56

Closed JasperAlgra closed 8 years ago

JasperAlgra commented 8 years ago

I think this is a way to fix #45

Possible command messages for as far as I can see: /command /command arg [..] arg /command@botname /command@botname arg [..] arg

Instead of splitting the 'command' on "@" or space you can use a regexp to split in one time in three possible parts:

After that nullify the parts that are matched as empty string and pass everything as self.emit. Note: I've added a fourth argument 'target' to the emit call, passing the @botname to the listening application. The application should handle the target if required (match to botname, or do something else like aim a command to a user, etc).

Hope you like the fix :)

vacero commented 8 years ago

I'm not sure about the target part of your code. The bot object from the library represents a single bot, with a unique username, so I don't see the reason it should respond to any command that has arbitrary targets. I think the bot should only respond to those commands that have its own username as target, and ignore the rest.

JasperAlgra commented 8 years ago

I agree completely. Only according to the telegram docs the target can also be a user, I think e.g. "/message@user hello". That would be better handed in the app code I think. We could also ignore it (I don't need/use it) but other users of your library might? At the moment there's no differtation between mentioned users/targets, resulting in the @part to be parsed as command.

JasperAlgra commented 8 years ago

I've just tested my bot on the "@part", whenever I'm in private/normal or groupchat with my bot it receives/reacts to all /commands regardsless of an @ . E.g. /image@otherbotname also triggers a response.

At first my bot had privacy mode disabled, but enabling it just now and results seem the same.

So now I'm not completely sure what's the point of the @ part as the bot seem to get all the messages anyhow. Do you have experience with multiple bots in one channel/group?

shernshiou commented 8 years ago

Thanks. I shall merge first for testing.