public async Task <IActionResult> Index() { Config.ConStr = _configuration.GetConnectionString("Db"); try { DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); BotParser botParser = new BotParser(); var userAgent = Request.Headers["User-Agent"]; var result = DeviceDetector.GetInfoFromUserAgent(userAgent); if (string.IsNullOrEmpty(HttpContext.Session.GetString("_UserId"))) { var model = new LoginContent(); model.Alert = null; return(await Task.Run(() => RedirectToAction("SignIn", "Home", model))); } else { ViewBag.UserId = HttpContext.Session.GetString("_UserId"); ViewBag.Device = result.Match.DeviceType.ToString(); Console.WriteLine(ViewBag.Device); return(await Task.Run(() => View())); } } catch (Exception ex) { var Error = new ErrorViewModel(); Error.MessageContent = ex.ToString(); Error.MessageTitle = "Error "; Error.RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; return(await Task.Run(() => View())); } }
/// <summary> /// Called each new message from Discord /// </summary> private async Task OnMessageCreated(DiscordClient client, MessageCreateEventArgs e) { if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace)) { _logger.LogTrace(@"Message from {author}"); } if (e.Message.Author.IsBot || IsThisMessageForTheBot(e) == false) { return; // Ignore all botusers or messages not for the bot } // var members = await e.Guild.GetAllMembersAsync(); // Get the member info and roles // var member = members.Where(n => n.Id == e.Message.Author.Id).FirstOrDefault(); DiscordMember member = (DiscordMember)e.Message.Author; var roles = member.Roles.Select(n => n.Name).ToList(); var parser = new BotParser( e.Message.Content, e.Message.MentionedUsers.Where(n => n.IsBot == true).Count() > 0, roles, member.IsOwner, e.Author.Username ); var responseMessage = await _botRunner.HandleMessage(parser); var embed = new DiscordEmbedBuilder { Color = new DiscordColor("#550099"), Title = responseMessage.Title, Description = (responseMessage.Text.Length < 2048) ? responseMessage.Text : responseMessage.Text[..2048]
public async Task <string> ProcessStockCode() { try { var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, stooqApiUrl)); if (response.IsSuccessStatusCode) { StockModel stockModel = new BotParser(await response.Content.ReadAsStreamAsync()).Parse(); if (stockModel.Close == "N/D") { return("DATA NOT FOUND FOR THE STOCK CODE: " + stockCode.ToUpper() + "."); } return(stockCode.ToUpper() + " quote is $" + stockModel.Close + " per share."); } else { return("THE STOCK SERVER RETURNED AN ERROR."); } } catch { return("THERE WAS AN ERROR PROCESSING THE STOCK CODE: " + stockCode.ToUpper() + "."); } }
static void Main(string[] args) { Console.SetIn(new StreamReader(Console.OpenStandardInput(512))); BotParser parser = new BotParser(new AwesomeBot()); parser.Run(); }
public void TestParseNoBot() { var botParser = new BotParser(); botParser.SetUserAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; SV1; SE 2.x)"); var result = botParser.Parse(); result.Success.Should().BeFalse(); }
public void TestParseNoDetails() { var botParser = new BotParser(); botParser.SetUserAgent("Googlebot/2.1 (http://www.googlebot.com/bot.html)"); var result = botParser.Parse(); result.Success.Should().BeTrue(); }
public void GivenExpressionTestCorrectParsing( string expression, string?expectedCommand, string?expectedArgs, string?expectedQuery) { // ARRANGE & ACT var parser = new BotParser(expression, false, new string[] { "role" }, false, "user"); Assert.Equal(expectedCommand, parser.Command); Assert.Equal(expectedArgs, parser.CommandArgs); Assert.Equal(expectedQuery, parser.Query); }
/// <summary> /// Parses the UA for bot information using the Bot parser /// </summary> private void ParseBot() { if (skipBotDetection) { bot = new ParseResult <BotMatchResult>(); return; } var botParser = new BotParser(); botParser.SetUserAgent(userAgent); botParser.SetCache(cache); botParser.DiscardDetails = discardBotInformation; bot = botParser.Parse(); }
/// <summary> /// Called each new message from Discord /// </summary> private async Task OnMessageCreated(MessageCreateEventArgs e) { if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace)) { _logger.LogTrace(@"Message from {author}"); } if (e.Message.Author.IsBot || IsThisMessageForTheBot(e) == false) { return; // Ignore all botusers or messages not for the bot } var parser = new BotParser(e.Message.Content, e.Message.MentionedUsers.Where(n => n.IsBot == true).Count() > 0); var responseMessage = await _botRunner.HandleMessage(parser); await e.Message.RespondAsync(responseMessage); }
public async Task <IActionResult> TopupBerasAwal(int id) { DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); BotParser botParser = new BotParser(); var userAgent = Request.Headers["User-Agent"]; var result = DeviceDetector.GetInfoFromUserAgent(userAgent); ViewBag.Device = result.Match.DeviceType.ToString(); var model = new HomeModel(); if (ViewBag.Device == "smartphone") { return(await Task.Run(() => View(model))); } else { return(await Task.Run(() => View(model))); } }
public void TestGetInfoFromUaBot() { var expected = new Bot { Name = "Googlebot", Category = "Search bot", Url = "http://www.google.com/bot.html", Producer = new Producer { Name = "Google Inc.", Url = "http://www.google.com" } }; var botParser = new BotParser { DiscardDetails = false }; botParser.SetUserAgent("Googlebot/2.1 (http://www.googlebot.com/bot.html)"); var result = botParser.Parse(); result.Match.Should().BeEquivalentTo(expected, config => config.ExcludingMissingMembers()); }
static void Main(string[] args) { BotParser parser = new BotParser(new BotStarter()); parser.Run(); }
static void Main(string[] args) { BotParser parser = new BotParser(new AwesomeBot.Bot.AwesomeBot()); parser.Run(); }