public ActionResult Index() { const string defaultWelcomeMessage = "Welcome to your support!\nWe never leave you alone😊"; var settingRepo = new SettingRepo(BotInstanceId); var supporters = new SupporterRepo(BotInstanceId).GetAll(); var subscribers = Subscribers .GroupJoin(supporters, subscriber => subscriber.ChatId, supporter => supporter.ChatId, (subscriber, thisSupporters) => new SubscriberViewModel { ChatId = subscriber.ChatId, Username = subscriber.Username, FirstName = subscriber.FirstName, LastName = subscriber.LastName, IsSupporter = thisSupporters.Any() } ) .ToList(); return(View(new HomeViewModel { Subscribers = subscribers, WelcomeMessage = settingRepo.GetWelcomeMessage() ?? defaultWelcomeMessage })); }
public ActionResult RemoveSupporter(long chatId) { var supporterRepo = new SupporterRepo(BotInstanceId); supporterRepo.Remove(chatId); return(Redirect(Request.Headers["Referer"].ToString())); }
private SubscriberRecord FairSelectSupporter() { var supporters = SupporterRepo.GetAll(); var connections = ConnectionRepo.GetAll(); //TODO: Fair select return(supporters.FirstOrDefault(x => connections.All(c => c.SupporterChatId != x.ChatId))); }
public ActionResult MakeSupporter(long chatId) { var supporterRepo = new SupporterRepo(BotInstanceId); supporterRepo.Add(chatId); //TODO: What's the correct way? //ConnectionNotifier.CustomerDisconnected(); return(Redirect(Request.Headers["Referer"].ToString())); }
public ConnectionManager( SupporterRepo supporterRepo, ConnectionRepo connectionRepo, StateManager stateManager, ITelegramBotClient telegramClient, ConnectionNotifier connectionNotifier) { SupporterRepo = supporterRepo; ConnectionRepo = connectionRepo; StateManager = stateManager; TelegramClient = telegramClient; ConnectionNotifier = connectionNotifier; }
public SupportBotInstance( WaitingManager waitingManager, SupporterRepo supporterRepo, SettingRepo settingRepo, ConnectionManager connectionManager, StateManager stateManager, ITelegramBotClient telegramClient) { WaitingManager = waitingManager; SupporterRepo = supporterRepo; SettingRepo = settingRepo; ConnectionManager = connectionManager; StateManager = stateManager; TelegramClient = telegramClient; }
public void Update(Update update, SubscriberRecord subscriberRecord) { if (update.Type != UpdateType.Message) { return; } if (SupporterRepo.IsSupporter(subscriberRecord)) { HandleSupporterMessage(update, subscriberRecord); } else { HandleCustomerMessage(update, subscriberRecord); } }
public bool TryConnect(SubscriberRecord customer) { var supporters = SupporterRepo.GetAll(); var connections = ConnectionRepo.GetAll(); if (supporters.Count() > connections.Count()) { var supporter = FairSelectSupporter(); ConnectionRepo.Add(supporter, customer); TelegramClient.SendTextMessageAsync(supporter.ChatId, $"You're Connected to {customer.FirstName} {customer.LastName}", replyMarkup: StateManager.GetSupporterReplyKeyboardMarkup(supporter)); TelegramClient.SendTextMessageAsync(customer.ChatId, "You're Connected.", replyMarkup: StateManager.GetCustomerReplyKeyboardMarkup(customer)); return(true); } return(false); }
public void Direct(SubscriberRecord subscriberRecord, Update update) { var partyChatId = ConnectionRepo.FindPartyChatId(subscriberRecord); var replyKeyboardMarkup = SupporterRepo.IsSupporter(subscriberRecord) ? StateManager.GetCustomerReplyKeyboardMarkup(subscriberRecord) : StateManager.GetSupporterReplyKeyboardMarkup(subscriberRecord); if (partyChatId != default(long)) { var message = update.Message; string fileId; switch (message.Type) { case MessageType.Text: if (message.Text != null) { TelegramClient.SendTextMessageAsync(partyChatId, message.Text, replyMarkup: replyKeyboardMarkup); } break; case MessageType.Photo: fileId = message.Photo.Last().FileId; TelegramClient.SendPhotoAsync(partyChatId, new InputOnlineFile(fileId), message.Caption ?? "", replyMarkup: replyKeyboardMarkup); break; case MessageType.Audio: fileId = message.Audio.FileId; TelegramClient.SendAudioAsync(partyChatId, new InputOnlineFile(fileId), message.Caption ?? "", ParseMode.Default, message.Audio.Duration, message.Audio.Performer ?? "", message.Audio.Title ?? "", replyMarkup: replyKeyboardMarkup); break; case MessageType.Video: fileId = message.Video.FileId; TelegramClient.SendVideoAsync(partyChatId, new InputOnlineFile(fileId), caption: message.Caption ?? "", replyMarkup: replyKeyboardMarkup); break; case MessageType.Voice: fileId = message.Voice.FileId; TelegramClient.SendVoiceAsync(partyChatId, new InputOnlineFile(fileId), message.Caption ?? "", replyMarkup: replyKeyboardMarkup); break; case MessageType.Document: fileId = message.Document.FileId; TelegramClient.SendDocumentAsync(partyChatId, new InputOnlineFile(fileId), message.Caption ?? "", replyMarkup: replyKeyboardMarkup); break; case MessageType.Sticker: fileId = message.Sticker.FileId; TelegramClient.SendStickerAsync(partyChatId, new InputOnlineFile(fileId), replyMarkup: replyKeyboardMarkup); break; case MessageType.Location: TelegramClient.SendLocationAsync(partyChatId, message.Location.Latitude, message.Location.Longitude, replyMarkup: replyKeyboardMarkup); break; case MessageType.VideoNote: fileId = message.VideoNote.FileId; TelegramClient.SendVideoNoteAsync(partyChatId, new InputOnlineFile(fileId), message.VideoNote.Duration, message.VideoNote.Length, replyMarkup: replyKeyboardMarkup); break; case MessageType.Contact: case MessageType.Venue: case MessageType.Game: case MessageType.Invoice: case MessageType.SuccessfulPayment: case MessageType.Unknown: TelegramClient.SendTextMessageAsync(subscriberRecord.ChatId, $"Message type {message.Type} is not supported.", replyMarkup: replyKeyboardMarkup); break; case MessageType.WebsiteConnected: case MessageType.ChatMembersAdded: case MessageType.ChatMemberLeft: case MessageType.ChatTitleChanged: case MessageType.ChatPhotoChanged: case MessageType.MessagePinned: case MessageType.ChatPhotoDeleted: case MessageType.GroupCreated: case MessageType.SupergroupCreated: case MessageType.ChannelCreated: case MessageType.MigratedToSupergroup: case MessageType.MigratedFromGroup: default: break; } } else { TelegramClient.SendTextMessageAsync(subscriberRecord.ChatId, "You have no current session.", replyMarkup: replyKeyboardMarkup); } }