Example #1
0
        /// <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!");
            }

            #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


            telegrams.Add(new Tuple <String, IEnumerable <String> >(Message[Languages.en], 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);
        }
Example #2
0
        /// <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);
        }