How to make slackbot reply if the message starts only with a tag

淺唱寂寞╮ 提交于 2019-12-11 23:22:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!