public override async Task <bool> Handle(Message message) { var entries = await Task.WhenAll(_accounts.GetAll().AsParallel().Select(async a => { var user = await _psnService.GetUser(a.PSNName); if (user == null) { return(null); } return(new { TelegramName = a.TelegramName, PSNName = a.PSNName, Online = user.GetOnline(), StatusLine = user.GetStatusLine(), HasPlus = user.HasPlus() }); }).ToArray()); var table = entries.Where(t => t != null && t.Online) .OrderBy(t => t.TelegramName).ToList(); StringBuilder sb = new StringBuilder(); foreach (var entry in table) { var name = entry.TelegramName; if (!string.IsNullOrEmpty(name)) { name = name + " (" + entry.PSNName + ")"; } else { name = entry.PSNName; } if (entry.HasPlus) { sb.Append("PS+ "); } sb.Append(name); if (!string.IsNullOrEmpty(entry.StatusLine)) { sb.Append("\n" + entry.StatusLine); } sb.Append("\n\n"); } var response = await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = message.Chat.Id, ReplyToMessageId = message.MessageId, Text = sb.ToString(), ParseMode = "HTML", }); return(true); }
public override async Task <bool> Handle(Message message) { var match = _regex.Match(message.Text.Trim()); if (!match.Groups["param"].Success) { await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = message.Chat.Id, ReplyToMessageId = message.MessageId, Text = "Пожалуйста, укажи что ты хочешь найти. Например: /search@clankbot Dark Souls", ParseMode = "HTML", }); return(false); } var text = match.Groups["param"].Value; var filtered = _accounts.Search(text); var lines = filtered.AsParallel().Select(async account => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Telegram: <b>{0}</b>\nPSN: <b>{1}</b>", account.TelegramName, account.PSNName)); var userEntry = await _psnService.GetUser(account.PSNName); var status = userEntry.GetStatus(); if (!string.IsNullOrEmpty(status)) { builder.AppendLine(string.Format("{0}\n", status)); } if (!string.IsNullOrEmpty(account.Interests)) { builder.AppendLine(string.Format("Интересы:\n{0}", account.Interests)); } builder.AppendLine(); return(builder.ToString()); }).ToArray(); Task.WaitAll(lines); foreach (var line in lines) { await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = message.Chat.Id, ReplyToMessageId = message.MessageId, Text = line.Result, ParseMode = "HTML", }); Thread.Sleep(200); } return(true); }
public override async Task <bool> Handle(Message message) { var entries = await Task.WhenAll(_accounts.GetAll().AsParallel().Select(async a => { var user = await _psnService.GetUser(a.PSNName); if (user == null) { return(null); } return(new { Id = a.Id, TelegramName = a.TelegramName, PSNName = a.PSNName, Rating = user.GetRating(), ThrophyLine = user.GetTrophyLine() }); }).ToArray()); var table = entries.Where(t => t != null) .OrderByDescending(t => t.Rating).ToList(); StringBuilder sb = new StringBuilder(); int i = 1; if (table.Any()) { foreach (var t in table) { if (t.Id == message.From.Id) { sb.AppendLine(string.Format("<b>{0}. {1}</b>", i, !string.IsNullOrEmpty(t.TelegramName) ? t.TelegramName : t.PSNName)); } else { sb.AppendLine(string.Format("{0}. {1}", i, !string.IsNullOrEmpty(t.TelegramName) ? t.TelegramName : t.PSNName)); } i++; } var response = await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = message.Chat.Id, ReplyToMessageId = message.MessageId, Text = sb.ToString(), ParseMode = "HTML", }); } return(true); }
public async Task <bool> HandleCurrentStep(Account account, Message message) { try { var text = message.Text.Trim(); bool registration = false; if (account.Status < Status.Ok) { registration = true; } switch (account.Status) { case Status.AwaitingPSNName: var user = await _psnService.GetUser(message.Text); if (user == null) { await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, ReplyToMessageId = message.MessageId, Text = Messages.PSNNameFault, ParseMode = "HTML", }); } else { await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, ReplyToMessageId = message.MessageId, Text = Messages.PSNNameSuccess, ParseMode = "HTML", }); await _psnService.SendFriendRequest(text); account.PSNName = text; account.Status = Status.AwaitingFriendRequest; _accountService.Update(account); await SendCurrentStep(account); } break; case Status.AwaitingFriendRequest: await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, ReplyToMessageId = message.MessageId, Text = Messages.AwaitingFriendRequest, ParseMode = "HTML", }); break; case Status.AwaitingInterests: case Status.AwaitingNewInterests: account.Interests = text; account.Status = registration ? Status.AwaitingTrophies : Status.Ok; _accountService.Update(account); await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, Text = Messages.SavedInterests, ParseMode = "HTML", }); if (registration) { await SendCurrentStep(account); } break; case Status.AwaitingTrophies: case Status.AwaitingNewTrophies: var value = text.ToLower(); if (value == "да" || value == "нет") { account.Status = Status.Ok; account.ShowTrophies = value == "да"; _accountService.Update(account); await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, Text = value == "да" ? Messages.ShowTrophiesYes : Messages.ShowTrophiesNo, ParseMode = "HTML", }); if (registration) { await SendCurrentStep(account); } } else { await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, Text = Messages.YesOrNo, ParseMode = "HTML", }); } break; case Status.AwaitingDeleteConfirmation: if (string.Equals(text, account.PSNName)) { _accountService.Delete(account); await _psnService.RemoveFriend(account.PSNName); await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, Text = Messages.DeleteSuccess, ParseMode = "HTML", }); } else { await _telegramClient.SendMessage(new SendMessageQuery() { ChatId = account.Id, Text = Messages.DeleteFail, ParseMode = "HTML", }); account.Status = Status.Ok; _accountService.Update(account); } break; } return(true); } catch { return(false); } }