private async Task endAttendanceSessionAsync(RelayChannel relay) { // Edit the original message to mark when the call started, var startedMessageModel = new ChatMessageModel() { Body = relay.AttendanceStarted }; await relay.ToChatService .UpdateMessageAsync(relay.ToChannelId, relay.AttendanceSessionMessageId, startedMessageModel); // ... and post a new message to show when the call ended. var duration = DateTimeOffset.Now - relay.AttendanceSessionStarted; var durationStr = duration.ToString("h'h 'm'm 's's'"); var totalAttendanceStr = string.Join(", ", relay.TotalAttendanceSessionUsers.Select(u => u.DisplayName)); var messageBody = $"{relay.AttendanceEndedPrefix} Duration: {durationStr}, " + $"Participants: {totalAttendanceStr}"; var messageModel = new ChatMessageModel() { Body = messageBody }; await relay.ToChatService .SendMessageToChannelIdAsync(relay.ToChannelId, messageModel); relay.AttendanceSessionStarted = DateTimeOffset.MinValue; relay.AttendanceSessionMessageId = ""; relay.CurrentAttendanceSessionUsers.Clear(); relay.TotalAttendanceSessionUsers.Clear(); }
private static async Task SendWeb(RelayChannel relay, string message) { var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, }; using (var httpClient = new HttpClient(handler)) { httpClient.Timeout = TimeSpan.FromSeconds(5); httpClient.DefaultRequestHeaders.Clear(); httpClient.DefaultRequestHeaders.Add("User-Agent", "TED_ChatRelay"); httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip"); using (var responseMessage = await httpClient.PostAsync($"{relay.Endpoint}?msg={EncodeParam(message)}&code={EncodeCode(relay.Code)}&ch={EncodeParam(relay.EveChannelName)}", null)) { var r = await responseMessage.Content.ReadAsStringAsync(); if (!responseMessage.IsSuccessStatusCode) { //if(responceMessage.StatusCode == ) Console.WriteLine("ERROR: Bad client request!"); } if (r != "OK" && r != "DUPE") { Console.WriteLine(r.StartsWith("ERROR") ? $"RESPONSE -> {r}" : "ERROR: Server not configured!"); } } } }
private void updateRelayAttendance(RelayChannel relay, List <UserModel> users) { foreach (var user in users) { if (!(relay.TotalAttendanceSessionUsers.Any(u => (u.Id == user.Id)))) { relay.TotalAttendanceSessionUsers.Add(user); } } relay.CurrentAttendanceSessionUsers = users; }
private async Task startNewAttendanceSessionAsync(RelayChannel relay, List <UserModel> users) { relay.TotalAttendanceSessionUsers.Clear(); relay.AttendanceSessionStarted = DateTimeOffset.Now; updateRelayAttendance(relay, users); var attendanceString = string.Join(", ", users.Select(u => u.DisplayName)); var messageBody = $"{relay.AttendancePrefix} {attendanceString}"; var messageModel = new ChatMessageModel() { Body = messageBody }; var messageId = await relay.ToChatService .SendMessageToChannelIdAsync(relay.ToChannelId, messageModel); relay.AttendanceSessionMessageId = messageId; }
private async Task processAttendanceUpdateAsync(RelayChannel relay, ChannelModel channelUpdate) { var updatedAttendance = channelUpdate.Users .Where(u => (!u.IsTheorem) && (u.Presence == UserModel.PresenceKind.Online)) .ToList(); if (isUserListEqual(relay.CurrentAttendanceSessionUsers, updatedAttendance)) { return; } if (relay.CurrentAttendanceSessionUsers.Count > 0) { await updateAttendanceSessionAsync(relay, updatedAttendance); } else { await startNewAttendanceSessionAsync(relay, updatedAttendance); } }
private async Task updateAttendanceSessionAsync(RelayChannel relay, List <UserModel> users) { updateRelayAttendance(relay, users); if (users.Count == 0) { await endAttendanceSessionAsync(relay); return; } var attendanceString = string.Join(", ", users.Select(u => u.DisplayName)); var messageBody = $"{relay.AttendancePrefix} {attendanceString}"; var messageModel = new ChatMessageModel() { Body = messageBody }; relay.AttendanceSessionMessageId = await relay.ToChatService .UpdateMessageAsync(relay.ToChannelId, relay.AttendanceSessionMessageId, messageModel); }
private static void SendMessage(RelayChannel relay, string message) { try { Console.WriteLine($"ENQUEUE->[{relay.EveChannelName}]: {message}"); if (message.Length > MAX_MESSAGE_LENGTH) { foreach (var line in message.SplitToLines(MAX_MESSAGE_LENGTH)) { Package.Enqueue(new Tuple <RelayChannel, string>(relay, line)); } } else { Package.Enqueue(new Tuple <RelayChannel, string>(relay, message)); } } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } }
public RelayClient(RelayChannel client) => Session = new(client);