/// <summary> /// Send a multi-language Telegram to the given users in their preferred language. /// </summary> /// <param name="Message">The multi-language text of the message.</param> /// <param name="Usernames">An enumeration of usernames.</param> public async Task <IEnumerable <MessageEnvelop> > SendTelegram(I18NString Message, IEnumerable <String> Usernames, ParseMode ParseMode) { #region Initial checks Usernames = Usernames.SafeSelect(username => username?.Trim()). SafeWhere(username => !username.IsNullOrEmpty()). ToArray(); if (Message.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Message), "The given message must not be null or empty!"); } if (Usernames.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Usernames), "The given enumeration of usernames must not be null or empty!"); } var responseMessages = new List <MessageEnvelop>(); #endregion var eventTrackingId = EventTracking_Id.New; #region Send OnSendTelegramRequest event var StartTime = Timestamp.Now; try { if (OnSendTelegramRequest != null) { await Task.WhenAll(OnSendTelegramRequest.GetInvocationList(). Cast <OnSendTelegramRequestDelegate>(). Select(e => e(StartTime, this, eventTrackingId, Message, Usernames))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramRequest)); } #endregion telegrams.Add(new Tuple <String, IEnumerable <String> >(Message[Languages.en], Usernames)); foreach (var username in Usernames) { responseMessages.Add(new MessageEnvelop(username, username.GetHashCode(), new Telegram.Bot.Types.Message() { Text = "Ok" })); } #region Send OnSendTelegramResponse event var Endtime = Timestamp.Now; try { if (OnSendTelegramResponse != null) { await Task.WhenAll(OnSendTelegramResponse.GetInvocationList(). Cast <OnSendTelegramResponseDelegate>(). Select(e => e(Endtime, this, eventTrackingId, Message, Usernames, responseMessages, Endtime - StartTime))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramResponse)); } #endregion return(responseMessages); }
/// <summary> /// Send a Telegram to the given users. /// </summary> /// <param name="Message">The text of the message.</param> /// <param name="Usernames">An enumeration of usernames.</param> public async Task <IEnumerable <MessageEnvelop> > SendTelegrams(String Message, IEnumerable <String> Usernames, ParseMode ParseMode) { #region Initial checks Message = Message?.Trim(); Usernames = Usernames.SafeSelect(username => username?.Trim()). SafeWhere(username => !username.IsNullOrEmpty()). ToArray(); if (Message.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Message), "The given message must not be null or empty!"); } if (Usernames.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Usernames), "The given enumeration of usernames must not be null or empty!"); } var responseMessages = new List <MessageEnvelop>(); #endregion var eventTrackingId = EventTracking_Id.New; var message = I18NString.Create(Languages.en, Message); #region Send OnSendTelegramRequest event var StartTime = Timestamp.Now; try { if (OnSendTelegramRequest != null) { await Task.WhenAll(OnSendTelegramRequest.GetInvocationList(). Cast <OnSendTelegramRequestDelegate>(). Select(e => e(StartTime, this, eventTrackingId, message, Usernames))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramRequest)); } #endregion foreach (var username in Usernames) { if (UserByUsername.TryGetValue(username, out TelegramUser User)) { responseMessages.Add(new MessageEnvelop(username, User.ChatId, await TelegramAPI.SendTextMessageAsync( ChatId: User.ChatId, Text: Message, ParseMode: ParseMode ))); } else { responseMessages.Add(new MessageEnvelop(username, new Telegram.Bot.Types.Message() { Text = "Unknown Telegram user '" + username + "'!" })); } } #region Send OnSendTelegramResponse event var Endtime = Timestamp.Now; try { if (OnSendTelegramResponse != null) { await Task.WhenAll(OnSendTelegramResponse.GetInvocationList(). Cast <OnSendTelegramResponseDelegate>(). Select(e => e(Endtime, this, eventTrackingId, message, Usernames, responseMessages, Endtime - StartTime))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramResponse)); } #endregion return(responseMessages); }
/// <summary> /// Send a Telegram to the given user. /// </summary> /// <param name="Message">The text of the message.</param> /// <param name="Username">The name of the user.</param> public async Task <MessageEnvelop> SendTelegram(String Message, String Username, ParseMode ParseMode) { #region Initial checks Message = Message?.Trim(); Username = Username?.Trim(); if (Message.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Message), "The given message must not be null or empty!"); } if (Username.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Username), "The given username must not be null or empty!"); } #endregion var eventTrackingId = EventTracking_Id.New; var message = I18NString.Create(Languages.en, Message); var usernames = new String[] { Username }; #region Send OnSendTelegramRequest event var StartTime = Timestamp.Now; try { if (OnSendTelegramRequest != null) { await Task.WhenAll(OnSendTelegramRequest.GetInvocationList(). Cast <OnSendTelegramRequestDelegate>(). Select(e => e(StartTime, this, eventTrackingId, message, usernames))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramRequest)); } #endregion telegrams.Add(new Tuple <String, IEnumerable <String> >(Message, new String[] { Username })); var responseMessage = new MessageEnvelop(Username, new Telegram.Bot.Types.Message() { Text = "Ok" }); #region Send OnSendTelegramResponse event var Endtime = Timestamp.Now; try { if (OnSendTelegramResponse != null) { await Task.WhenAll(OnSendTelegramResponse.GetInvocationList(). Cast <OnSendTelegramResponseDelegate>(). Select(e => e(Endtime, this, eventTrackingId, message, usernames, new MessageEnvelop[] { responseMessage }, Endtime - StartTime))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramResponse)); } #endregion return(responseMessage); }
/// <summary> /// Send a multi-language Telegram to the given user in his/her preferred language. /// </summary> /// <param name="Message">The multi-language text of the message.</param> /// <param name="Username">The name of the user.</param> public async Task <MessageEnvelop> SendTelegram(I18NString Message, String Username, ParseMode ParseMode) { #region Initial checks Username = Username?.Trim(); if (Message.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Message), "The given message must not be null or empty!"); } if (Username.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Username), "The given username must not be null or empty!"); } MessageEnvelop responseMessage; #endregion var eventTrackingId = EventTracking_Id.New; var usernames = new String[] { Username }; #region Send OnSendTelegramRequest event var StartTime = Timestamp.Now; try { if (OnSendTelegramRequest != null) { await Task.WhenAll(OnSendTelegramRequest.GetInvocationList(). Cast <OnSendTelegramRequestDelegate>(). Select(e => e(StartTime, this, eventTrackingId, Message, usernames))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramRequest)); } #endregion if (UserByUsername.TryGetValue(Username, out TelegramUser User)) { responseMessage = new MessageEnvelop(Username, User.ChatId, await TelegramAPI.SendTextMessageAsync( ChatId: User.ChatId, Text: Message[User.PreferredLanguage] ?? Message[Languages.en] )); } else { responseMessage = new MessageEnvelop(Username, new Telegram.Bot.Types.Message() { Text = "Unknown Telegram user '" + Username + "'!" }); } #region Send OnSendTelegramResponse event var Endtime = Timestamp.Now; try { if (OnSendTelegramResponse != null) { await Task.WhenAll(OnSendTelegramResponse.GetInvocationList(). Cast <OnSendTelegramResponseDelegate>(). Select(e => e(Endtime, this, eventTrackingId, Message, usernames, new MessageEnvelop[] { responseMessage }, Endtime - StartTime))). ConfigureAwait(false); } } catch (Exception e) { DebugX.Log(e, nameof(TelegramStore) + "." + nameof(OnSendTelegramResponse)); } #endregion return(responseMessage); }