public override async Task OnMessage(Channel channel, User user, string text, bool botIsMentioned) { if (!text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) return; await SendMessage(channel, text.Substring(prefix.Length)); }
internal override Task SendMessage(Channel channel, string text, Attachment[] attachments) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(text); if (attachments != null && attachments.Any()) { foreach (var attachment in attachments) { if (!string.IsNullOrEmpty(attachment.Pretext)) Console.WriteLine(attachment.Pretext); if (!string.IsNullOrEmpty(attachment.AuthorName)) Console.WriteLine(attachment.AuthorName); Console.WriteLine($"{attachment.Title} <{attachment.TitleLink}>"); if (!string.IsNullOrEmpty(attachment.Text)) Console.WriteLine(attachment.Text); foreach (var field in attachment.Fields) Console.WriteLine($"{field.Title}: {field.Value}"); Console.WriteLine(); } } Console.ResetColor(); // TODO: .CompletedTask (4.6). return Task.FromResult(true); }
/// <summary> /// Used privately to pass a message on to each handler. /// </summary> async Task SendMessageToHandlerAsync(Channel channel, User user, string text, bool botIsMentioned, Handler handler) { try { await handler.OnMessage(channel, user, text, botIsMentioned, cancellationSource.Token); } catch (Exception ex) { await SendMessage(channel, ex.ToString()); } }
/// <summary> /// Called by the bot implementation when a message is received, to be passed to handlers. /// </summary> protected void HandleRecievedMessage(Channel channel, User user, string text, bool botIsMentioned) { // If the text is cancellation, then send a cancellation message instead. if (cancellationTerms.Contains(text, StringComparer.OrdinalIgnoreCase)) { cancellationSource.Cancel(); cancellationSource = new CancellationTokenSource(); return; } foreach (var handler in handlers) handlerTasks.Add(SendMessageToHandlerAsync(channel, user, text, botIsMentioned, handler)); }
public override async Task OnMessage(Channel channel, User user, string text, bool botIsMentioned, CancellationToken cancellationToken) { if (!string.Equals(text, "countdown", StringComparison.OrdinalIgnoreCase)) return; for (var i = 10; i > 0 && !cancellationToken.IsCancellationRequested; i--) { await SendMessage(channel, $"{i}..."); await Task.Delay(1000); } if (!cancellationToken.IsCancellationRequested) await SendMessage(channel, "Thunderbirds are go!"); else await SendMessage(channel, "Countdown was aborted. Thunderbirds are cancelled, kids :("); }
public async override Task OnMessage(Channel channel, User user, string text, bool botIsMentioned) { // Check the message for a case reference. var match = Regex.Match(text, @"(?:fogbugz|fb|case|bug|feature|issue) (\d+)", RegexOptions.IgnoreCase); // Extract the case number. int caseNumber; if (!match.Success || !int.TryParse(match.Groups[1].Captures[0].Value, out caseNumber)) return; // Make it clear we're doing something. await SendTypingIndicator(channel); // Attempt to fetch the case from the FogBugz API. var c = GetCase(caseNumber); if (c == null) await SendMessage(channel, $"_(Unable to retrieve info from FogBugz for Case {caseNumber})_"); else { var att = new Attachment { Pretext = c.ParentBug != null ? $"Child case of {c.ParentBug}" : null, Fallback = $"{caseNumber}: {c.Title}", Title = $"{caseNumber}: {c.Title}", TitleLink = c.Url.AbsoluteUri, Text = c.LatestText, AuthorName = c.IsOpen ? c.AssignedTo : null, AuthorIcon = new Uri(url, $"default.asp?ixPerson={c.ixAssignedTo}&pg=pgAvatar&pxSize=16").AbsoluteUri, Colour = c.ixPriority <= 2 ? "danger" : "warning", Fields = new[] { new Field(c.Status, c.Category), new Field($"{c.Project} {c.Area}", "FixFor: " + c.Milestone) } }; await SendMessage(channel, null, new[] { att }); } }
protected async Task SayGoodbye(Channel channel) { await SendMessage(channel, RandomMessages.Goodbye()); }
protected async Task SayHello(Channel channel) { await SendMessage(channel, RandomMessages.Hello()); }
/// <summary> /// Used by the bot to send a typing notification. /// </summary> abstract internal Task SendTypingIndicator(Channel channel);
/// <summary> /// Used by the bot to send a message. /// </summary> abstract internal Task SendMessage(Channel channel, string text, Attachment[] attachments = null);
protected async Task SendTypingIndicator(Channel channel) { await bot.SendTypingIndicator(channel); }
protected async Task SendMessage(Channel channel, string text, Attachment[] attachments = null) { await bot.SendMessage(channel, text, attachments); }
public virtual Task OnMessage(Channel channel, User user, string text, bool botIsMentioned, CancellationToken cancellationToken) { return OnMessage(channel, user, text, botIsMentioned); }
public virtual Task OnMessage(Channel channel, User user, string text, bool botIsMentioned) { // TODO:.NET 4.6. //return Task.CompletedTask; return Task.FromResult(true); }
public override async Task OnMessage(Channel channel, User user, string text, bool botIsMentioned) { if (botIsMentioned) await SendMessage(channel, string.Format("<@{0}> Hello!", user.ID)); }
internal override Task SendTypingIndicator(Channel channel) { // TODO: .CompletedTask (4.6). return Task.FromResult(true); }