/// <summary> /// Sets up a listener on the defined channelId types (see <paramref name="hubs"/>) that the bot is a member of. The <paramref name="handler"/> /// is executed when an incoming message matches the given <paramref name="match"/>. /// Messages in <see cref="HubType.Channel"/> and <see cref="HubType.Group"/> are expected to /// contain the name of the bot before the handler will fire. /// </summary> /// <param name="match">Defines when to execute this handler</param> /// <param name="hubs">The Slack channels to listen on</param> /// <param name="modes">Configuration options</param> /// <param name="handler">Function to be run when a message matches</param> /// <returns></returns> public IWhenHandler When(MessageMatcher match, HubType hubs, Modes modes, Func <IConversation, Task> handler) { bool MessageAddressesBot(Message msg) => (modes & Modes.ObserveAllMessages) == Modes.ObserveAllMessages || msg.Text.Contains(this.state.BotUserId, StringComparison.OrdinalIgnoreCase) || msg.Text.Contains(this.state.BotUsername, StringComparison.OrdinalIgnoreCase); var whenHandler = new WhenHandler(this, msg => { if (msg.User != null && msg.User.Equals(this.state.BotUserId)) { return(MessageMatcher.NoMatch); } var messageHubType = this.state.GetHubById(msg.Channel).HubType; if ((hubs & messageHubType) != messageHubType) { return(MessageMatcher.NoMatch); } if (messageHubType != HubType.DirectMessage) { if (!MessageAddressesBot(msg)) { return(MessageMatcher.NoMatch); } } match.SetupLogger(this.config.LoggerFactory); return(match.GetMatches(msg)); }, async(msg, matches) => { var modesCopy = modes; // Conversation being initiated from another thread? force threaded mode if (msg.RawThreadTimestamp != null) { modesCopy |= Modes.StartThread; } using (var conversation = new Conversation(this, modesCopy, msg, matches)) { try { await handler(conversation); return(true, null); } catch (Exception ex) { return(false, ex); } } }); this.whenHandlers.Enqueue(whenHandler); return(whenHandler); }
private async Task <(bool success, Match[] matches)> MessageMatches(Message msg, MessageMatcher matcher) { if (matcher == null) { return(true, null); } var matches = await matcher.GetMatches(msg); bool success = matches != null && matches.Sum(m => m.Score) >= 0; if (!success) { return(false, null); } return(true, matches); }