问题
so I'm finishing programming a cool slackbot with Nodejs. But i would like the bot to answer only if the command begins with a tag followed by the botname, for example:
@joe greet me
At the moment i have something like this:
if (msg.toLowerCase() == "greet me")
bot.postMessageToChannel('general',"wazzup");
The problem is that it only replies if the message is exactly "greet me". I'm not looking for include() function. I just want the bot to answer when tagged and followed by a specific command. Thanks
回答1:
A very elegant solution for this problem is to use the Events API and subscribe to the app_mention event. Then your bot will only receive message events when he is explicitly mentioned in a channel, e.g. @my_bot hi
.
In addition to you may also want to subscribe to the message.im event, so you can react to direct messages from users.
回答2:
Have you tried regular expressions? perhaps something like
var command = msg.toLowerCase().match(/^@joe (.*)/)[1]
to capture the message in the first group
来源:https://stackoverflow.com/questions/52878765/how-to-make-slackbot-reply-if-the-message-starts-only-with-a-tag