private async Task ProcessMessage(BotEventArgs e) { using var scope = _serviceProvider.CreateScope(); var commandHandler = new CommandHandler(scope.ServiceProvider); CommandContainer commandContainer = _commandParser.ParseCommand(e); if (!commandContainer.StartsWithPrefix(_prefix)) { return; } commandContainer.RemovePrefix(_prefix); Result checkResult = commandHandler.CheckArgsCount(commandContainer); if (!checkResult.IsSuccess) { throw new CommandArgumentsException(checkResult.Message); } checkResult = commandHandler.CanCommandBeExecuted(commandContainer); if (!checkResult.IsSuccess) { throw new CommandCantBeExecutedException(checkResult.Message); } IBotMessage message = await commandHandler.ExecuteCommand(commandContainer); await message.SendAsync(_apiProvider, e.SenderInfo); }
private Task ClientOnMessage(SocketMessage arg) { var message = arg as SocketUserMessage; if (message is null) { return(Task.CompletedTask); } var context = new SocketCommandContext(_client, message); if (context.User.IsBot || context.Guild is null) { return(Task.CompletedTask); } LoggerHolder.Instance.Debug($"New message event: {context.Message}"); IBotMessage botMessage = ParseMessage(message, context); OnMessage?.Invoke(context.Client, new BotEventArgs( botMessage, new DiscordSenderInfo( (long)context.Channel.Id, (long)context.User.Id, context.User.Username, CheckIsAdmin(context.User), context.Guild.Id) ) ); return(Task.CompletedTask); }
static void PrintToConsole(IBotMessage message) { switch (message) { case BotMessageGroup bmg: foreach (var subMessage in ((BotMessageGroup)message).BotMessages) { PrintToConsole(subMessage); } break; case BotTextMessage btm: Console.WriteLine(btm.Text); break; case BotButtonMessage bbm: Console.WriteLine($"buttons {bbm.Text}"); bbm.Buttons.ToList().ForEach(x => Console.WriteLine($"{x.Value}: {x.DisplayText}")); break; case BotTransferMessage btm: Console.WriteLine("bot requested to transfer the chat"); break; } }
/*** * Stores a message if it is not already in the cache. */ public void AddMessage(IBotMessage message) { if (!Cache.Contains(message)) { Messages.Add(message); } Cache.Add(message); }
private IBotMessage ParseMessage(SocketUserMessage message, SocketCommandContext context) { List <IBotMediaFile> mediaFiles = context.Message.Attachments .Select(attachment => GetOnlineFile(attachment.Filename, attachment.Url)) .Where(onlineFile => onlineFile is not null) .Cast <IBotMediaFile>() .ToList(); IBotMessage parsedMessage = mediaFiles.Count switch { 0 => new BotTextMessage(context.Message.Content), 1 => new BotSingleMediaMessage(context.Message.Content, mediaFiles.Single()), _ => new BotMultipleMediaMessage(context.Message.Content, mediaFiles), }; return(parsedMessage); }
/*** * Determines if a message is desirable to the user. * @Returns: * true if the message should be posted because it matches a filter applied by the user. * false otherwise. */ public bool IsDesirable(IBotMessage message) { bool desirable = false; // No filters have been created by the user so we accept the message. if (UserPreferences.Count() == 0) { Console.WriteLine("[+] No filters applied so we take all messages!"); return(true); } else { string[] titleHints = message.Name.Split(' '); List <string> filterItems = titleHints.ToList <string>(); filterItems.AddRange(message.Categories); foreach (string filterItem in filterItems) { Console.WriteLine("[+] Considering category: " + filterItem.ToLower()); UserPreference preference; if (UserPreferences.FindUserPreferenceFromCache(filterItem.ToLower(), out preference)) { Console.WriteLine("[+] Category matches, considering prices!"); if (preference._minPrice != 0.0 && preference._maxPrice != 0.0) { double price = Convert.ToDouble(message.Price); desirable = (price >= preference._minPrice && price <= preference._maxPrice); } else { desirable = true; } break; } } } return(desirable); }
private void ProcessMessage(BotEventArgs e) { Result <CommandArgumentContainer> commandResult = _commandParser.ParseCommand(e); if (commandResult.IsFailed) { return; } if (!commandResult.Value.EnsureStartWithPrefix(_prefix)) { return; } commandResult = _commandHandler.IsCorrectArgumentCount(commandResult.Value.ApplySettings(_prefix)); if (commandResult.IsFailed) { HandlerError(commandResult, e); return; } commandResult = _commandHandler.IsCommandCanBeExecuted(commandResult.Value); if (commandResult.IsFailed) { HandlerError(commandResult, e); return; } Result <IBotMessage> executionResult = _commandHandler.ExecuteCommand(commandResult.Value); if (executionResult.IsFailed) { HandlerError(commandResult, e); return; } IBotMessage message = executionResult.Value; SenderInfo sender = commandResult.Value.Sender; //_apiProvider.WriteMessage(new BotEventArgs(executionResult.Value, commandResult.Value)); message.Send(_apiProvider, sender); }
private void ClientOnMessage(object sender, MessageEventArgs e) { LoggerHolder.Instance.Debug("New message event: {@e}", e); string text = e.Message.Text ?? e.Message.Caption; IBotMessage message = e.Message.Type switch { MessageType.Photo => new BotSingleMediaMessage(text, new BotOnlinePhotoFile(GetFileLink(e.Message.Photo.Last().FileId), e.Message.Photo.Last().FileId)), MessageType.Video => new BotSingleMediaMessage(text, new BotOnlineVideoFile(GetFileLink(e.Message.Video.FileId), e.Message.Video.FileId)), _ => new BotTextMessage(text), }; OnMessage?.Invoke(sender, new BotEventArgs( message, new TelegramSenderInfo( e.Message.Chat.Id, e.Message.From.Id, e.Message.From.FirstName, CheckIsAdmin(e.Message.From.Id, e.Message.Chat.Id) ) )); }
public override void Scrape() { HtmlWeb web = new HtmlWeb(); var baseHtml = web.Load(BaseUrl); HashSet <string> linksToFollow = ExtractNextLinks(baseHtml); linksToFollow.Add(BaseUrl); int currentDepth = 1; foreach (string link in linksToFollow) { var htmlDoc = web.Load(link); var bargainNodes = htmlDoc.DocumentNode.SelectNodes(BargainsXpath); if (bargainNodes != null) { foreach (var productNode in bargainNodes) { IBotMessage message = ExtractProductInfo(productNode); if (message != null) { AddMessage(message); } } } if (currentDepth >= Depth) { break; } currentDepth++; } }
public Task ProcessInternalMessage(IBotMessage botMessage, bool isQueue) { if (this.IsStarting) { return(Task.CompletedTask); } this.Logger.Info("'{Message}' патипу {Type}", botMessage.Message, botMessage.GetType()); var currentTime = DateTime.Now.TimeOfDay; var msgTasks = new List <Task>(); foreach (var user in this.AllUsers.Where(x => x.TgId != null)) { var isNeedSend = false; var messageText = botMessage.Message; if (botMessage is BotMessageRedmine redmineMessage) { (isNeedSend, messageText) = this.ProcessInternalMessageRedmine(redmineMessage, user, isNeedSend, messageText); } else if (botMessage is BotMessageJob jobMessage) { (isNeedSend, messageText) = this.ProcessInternalMessageJenkins(jobMessage, user, isNeedSend, messageText); } if (!isNeedSend) { continue; } var maySend = false; if (user.StartTime < user.EndTime) { maySend = user.StartTime < currentTime && user.EndTime > currentTime; } else { maySend = user.StartTime > currentTime || user.EndTime < currentTime; } if (user.TemporaryDisable) { maySend = false; } if (maySend) { // Рассылаем сообщения this.Logger.Info(botMessage.Message); msgTasks.Add(this.TgService.Value.SendMessage(user.TgId.Value, messageText)); } else if (isQueue) { // Запихиваем сообщения в очередь } } return(Task.WhenAll(msgTasks)); }
public void Talk(IBotMessage message) { BotStories.Add(message); message.ExcuteAsync(); }
public BotEventArgs(IBotMessage message, SenderInfo senderInfo) { Message = message; SenderInfo = senderInfo; }
public BotEventArgs(IBotMessage message, CommandArgumentContainer commandWithArgs) : this(message, commandWithArgs.Sender) { }