예제 #1
0
        /// <summary>
        /// Adds a new user into database.
        /// </summary>
        /// <param name="channelAccount">Channel acciunt.</param>
        public async Task <OperationResponse> AddNewUserAsync(ChannelAccount channelAccount)
        {
            var  response         = new OperationResponse();
            User newUserToBeAdded = new User()
            {
                Id          = channelAccount.Id,
                Name        = channelAccount.Name,
                AadObjectId = channelAccount.AadObjectId,
                Role        = channelAccount.Role,
                Properties  = channelAccount.Properties.ToString()
            };

            _telegramContext.Users.Add(newUserToBeAdded);

            try
            {
                var entitiesAdded = await _telegramContext.SaveChangesAsync();

                response.Success = true;
                return(response);
            }
            catch (System.Exception ex)
            {
                response.ErrorMessage = string.IsNullOrEmpty(ex.InnerException.Message) ? ex.InnerException.Message : ex.Message;
                return(response);
            }
        }
예제 #2
0
        public async Task <IActionResult> PutTelegram(long id, Telegram telegram)
        {
            if (id != telegram.Id)
            {
                return(BadRequest());
            }

            _context.Entry(telegram).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TelegramExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #3
0
        public async Task <Command> UpdateCommandAsync(Command _command)
        {
            var command = await _context.Commands.FindAsync(_command.Id);

            if (command is null)
            {
                return(command);
            }

            command.Response = _command.Response;
            _context.Commands.Update(command);

            await _context.SaveChangesAsync();

            return(command);
        }
예제 #4
0
        private static async Task AddOrUpdateLocation(int id, GoogleAddress address)
        {
            using (var telegramContext = new TelegramContext())
            {
                await telegramContext.Database.EnsureCreatedAsync();

                var locationProfile = await telegramContext.UserLocations.FindAsync(id);

                if (locationProfile != null)
                {
                    locationProfile.Latitude         = address.Coordinates.Latitude;
                    locationProfile.Longitude        = address.Coordinates.Longitude;
                    locationProfile.FormattedAddress = address.FormattedAddress;
                    locationProfile.IsAmerica        = address.IsAmerica();
                }
                else
                {
                    await telegramContext.UserLocations.AddAsync(
                        new UserLocation
                    {
                        Id               = id,
                        Latitude         = address.Coordinates.Latitude,
                        Longitude        = address.Coordinates.Longitude,
                        FormattedAddress = address.FormattedAddress,
                        IsAmerica        = address.IsAmerica()
                    }
                        );
                }

                await telegramContext.SaveChangesAsync();
            }
        }
        public async Task SendNewsletter()
        {
            if (MessageCommand.Parameters.Count != 1)
            {
                await ReplyTextMessageAsync("Usage:\n/send_newsletter <newsletter>");

                return;
            }

            var newsletterKey = MessageCommand.Parameters[0];
            var newsletter    = await _newsletterService.GetNewsletterByKeyAsync(newsletterKey);

            if (newsletter != null)
            {
                TelegramChat !.State       = SendingNewsletterState;
                TelegramChat["newsletter"] = newsletterKey;
                await ReplyTextMessageAsync("Ok, now send me the text formatted as HTML");

                await TelegramContext.SaveChangesAsync();
            }
            else
            {
                await ReplyTextMessageAsync(
                    $"The newsletter {newsletterKey} doesn't exist.\n" +
                    $"Use /create_newsletter {newsletterKey} - to create it");
            }
        }
예제 #6
0
        public static async void SaveFile(TelegramBotClient botClient, Message message, int buildingId)
        {
            if (message.Document != null)
            {
                using (TelegramContext db = new TelegramContext())
                {
                    DataFile LessonsData = new DataFile {
                        FileId = message.Document.FileId, Name = message.Document.FileId + "Build", BuildingId = buildingId
                    };
                    await db.DataFiles.AddAsync(LessonsData);

                    await db.SaveChangesAsync();
                }
            }
            else if (message.Photo != null && message.Photo.Count() > 0)
            {
                using (TelegramContext db = new TelegramContext())
                {
                    DataFile LessonsData = new DataFile {
                        FileId = message.Photo[1].FileId, Name = "Photo" + message.Photo[1].FileId, BuildingId = buildingId
                    };
                    await db.DataFiles.AddAsync(LessonsData);

                    await db.SaveChangesAsync();
                }
            }
            else
            {
                //await botClient.SendTextMessageAsync(message.Chat.Id, "Отправте следующий файл, или введите одну из команд!", Telegram.Bot.Types.Enums.ParseMode.Default);
            }
        }
        public async Task Setup()
        {
            TelegramChat.Role = ChatRole.Administrator;
            await _newsletterService.CreateNewsletterAsync(new Newsletter("default", "The default newsletter."));

            await TelegramContext.SaveChangesAsync();

            await ReplyTextMessageAsync("Done");
        }
예제 #8
0
 public static async void SetLastMessage(Telegram.Bot.TelegramBotClient botClient, int chatId, int newMessageId)
 {
     using (TelegramContext db = new TelegramContext())
     {
         var userState = db.UserStates.Where(x => x.User.Id == chatId).SingleOrDefault();
         if (userState != null)
         {
             userState.LastMessageId = newMessageId;
             db.UserStates.Update(userState);
             await db.SaveChangesAsync();
         }
     }
 }
        public async Task SendGlobalNewsletter()
        {
            if (MessageCommand.Parameters.Count != 0)
            {
                await ReplyTextMessageAsync("Usage:\n/send_global_newsletter");

                return;
            }

            TelegramChat !.State       = SendingNewsletterState;
            TelegramChat["newsletter"] = null;
            await ReplyTextMessageAsync("Ok, now send me the text formatted as HTML");

            await TelegramContext.SaveChangesAsync();
        }
예제 #10
0
        private static async Task AddOrUpdateUsername(int id, string newUsername)
        {
            using (var context = new TelegramContext())
            {
                await context.Database.EnsureCreatedAsync();

                var profile = await context.UserProfiles.FindAsync(id);

                if (profile == null)
                {
                    var newProfile = new UserProfile {
                        Id = id, LastFm = newUsername
                    };
                    await context.UserProfiles.AddAsync(newProfile);

                    await context.SaveChangesAsync();
                }
                else
                {
                    profile.LastFm = newUsername;
                    await context.SaveChangesAsync();
                }
            }
        }
예제 #11
0
        public async Task <User> CreateUser(global::Telegram.Bot.Types.User user)
        {
            var account = new User
            {
                ChatId = user.Id,
                Name   = user.Username
            };

            if (user.Username == null)
            {
                account.Name = user.FirstName + " " + user.LastName;
            }

            await _context.Accounts.AddAsync(account);

            await _context.SaveChangesAsync();

            return(account);
        }
예제 #12
0
 /// <summary>
 /// Saves chandes in database.
 /// </summary>
 private async Task SaveChangesAsync()
 {
     await _telegramContext.SaveChangesAsync();
 }
예제 #13
0
 public async Task AddPerson(Person person)
 {
     _user.Frens.Add(person);
     await _context.SaveChangesAsync();
 }