depoio / node-telegram-bot

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

Command not parsed if containing '@' #45

Closed longstone closed 8 years ago

longstone commented 8 years ago

Code:

    }).on('stats', function (message) {
    // do something
    }).on('message',function(msg){
        send(msg.chat.id,'unprocessable command: '+msg.text);
    }).start();

Request /stats@myBot Response: unprocessable command: /stats@myBot

Can someone confirm this is a bug? I'm running V0.1.2

shernshiou commented 8 years ago

@longstone will test and revert.

Luchanso commented 8 years ago

This request type is working for me

vacero commented 8 years ago

This is also not working for me. I think this needs simply to find and replace "@" + username after this line:

var command = msg.message.text.split(' ', 2)[0];
Luchanso commented 8 years ago

@viacgo, messages sent from public chat or private?

vacero commented 8 years ago

Both

JasperAlgra commented 8 years ago

Here the same, telegram now adds the @botname to the command in groups so I have to explain to chat users to 'not type the @ part' .. Anyhow, I now use something like this:

    // Handle all normal messages (without / prefix)
    .on('message', function (message) {

        console.log("score", message);
        // Check if message is a reply
        if (message.reply_to_message) {
            return handleReply(message);
        } else {
            // Add user of this message to database (ignore if known)
            db.addUser(message.from.id, message.from.first_name, message.from.last_name, message.from.username);
            // Handle the message
            handleMessage(message);
        }
    })

    //Command without argument
    .on('test', function (msg) {
        console.log("test", msg);
        bot.sendMessage({
            chat_id: msg.chat.id,
            text: 'You\'ve send command: ' + command
        });
    })

The command@botname always triggers the .on('message',..) instead of .on('command',...) function. So splitting the message on the @ into a command and a target as @viacgo suggests unfortunately only works for .on('message' and not for .on('command') triggers.

I'll try to look into the bot functions later to see if I can find out where it goes south.

JasperAlgra commented 8 years ago

allright, this should be it. I'll try to make a pull request but that's new for me with libaries and all :)

At the moment I'm not passing the target to the bot/self.emit function but I think that should be possible as well.

-- edit: this solution is not parsing the aguments well. Seems the message can be about anything: /command arg-1 ... arg-n /command@botname arg-1 .. arg-n

Bot.js line 221

if (msg.message.text && msg.message.text.charAt(0) === '/') {

                                    /**
                                     * Split the message on space and @
                                     * First part = command with leading /
                                     * Second part = target or arguments
                                     * Third part = arguments if target is available
                                     */

                                    var messageParts = msg.message.text.split(/[ @]/, 3);

                                    // Filter everything not alphanum out of the command
                                    var command = messageParts[0].replace(/[^a-zA-Z0-9 ]/g, "");

                                    if (messageParts.length == 3) {
                                        var arg = messageParts[2].split(' ');
                                        self.emit(command, msg.message, arg);
                                    } else if (messageParts.length == 2) {
                                        var arg = messageParts[1].split(' ');
                                        self.emit(command, msg.message, arg);
                                    } else {
                                        self.emit(command, msg.message);
                                    }
                                }
JasperAlgra commented 8 years ago

All right, rewrote the regexp with help of a colleague :)

See pull request #56

longstone commented 8 years ago

cool thanks!