Esempio n. 1
0
        private async Task SendComicsAsync(Subscription subscriptionType)
        {
            using (var db = new BotDBContext())
            {
                var clients = (from c in db.Clients
                               join sub in db.Subscriptions on c.Subscription equals sub.Id
                               where sub.SubsctiptionType == (int)subscriptionType
                               select c.ChatId
                               ).Distinct();

                MessageToSend message = (subscriptionType == Subscription.Oglaf) ? GetOglafPicture() : GetXKCDPicture();

                foreach (var client in clients)
                {
                    var lastPostedKey = (from cli in db.Clients
                                         join sub in db.Subscriptions on cli.Subscription equals sub.Id
                                         where cli.ChatId == client && sub.SubsctiptionType == (int)subscriptionType
                                         orderby sub.Id descending
                                         select new
                    {
                        LTK = sub.LastPostedKey,
                        SUBID = sub.Id
                    }
                                         ).First();

                    if (message.Title.Equals(lastPostedKey.LTK))
                    {
                        continue;
                    }

                    DatabaseInteractions.Subscription subToUpdate = db.Subscriptions.Where(x => x.Id == lastPostedKey.SUBID).First();
                    string newHash = message.Title;
                    subToUpdate.LastPostedKey = newHash;
                    db.Update(subToUpdate);
                    //db.Subscriptions.Where(x => x.Id == lastPostedKey.SUBID).First().LastPostedKey = message.Title.GetHashCode().ToString();

                    try
                    {
                        await _bot.SendTextMessageAsync(client, message.Title.ToUpper());

                        await _bot.SendTextMessageAsync(client, message.SubTitle);

                        await _bot.SendPhotoAsync(client, new InputOnlineFile(message.Image));
                    }
                    catch (ChatNotFoundException e)
                    {
                        TraceError.Info(e.Message);
                        var clientsRecords = db.Clients.Where(c => c.ChatId == client).ToList();
                        TraceError.Info("Client Recs to remove: " + string.Join(",", clientsRecords.Select(c => c.ChatId)));
                        var subscriptionsToRemove = db.Subscriptions.Where(x => clientsRecords.Select(o => o.Subscription).Contains(x.Id));
                        TraceError.Info("Subscription Recs to remove: " + string.Join(",", subscriptionsToRemove.Select(s => s.SubsctiptionType.ToString())));
                        db.Subscriptions.RemoveRange(subscriptionsToRemove);
                        db.Clients.RemoveRange(clientsRecords);
                    }
                }
                await db.SaveChangesAsync();
            }
        }
Esempio n. 2
0
        public async Task UpdateSubscriptions(Update update)
        {
            Subscription subscriptionType = Subscription.NoSubscription;

            if (update.CallbackQuery.Data.Equals("/subs=Oglaf"))
            {
                subscriptionType = Subscription.Oglaf;
            }
            if (update.CallbackQuery.Data.Equals("/subs=Xkcd"))
            {
                subscriptionType = Subscription.XKCD;
            }
            if (update.CallbackQuery.Data.Equals("/subs=CoinCM"))
            {
                subscriptionType = Subscription.CoinCapMarket;
            }
            if (update.CallbackQuery.Data.Equals("/subs=ErrL"))
            {
                subscriptionType = Subscription.ErrorMessageLog;
            }

            var userId = update.CallbackQuery.From.Id;

            TraceError.Info("In callback");

            using (BotDBContext db = new BotDBContext())
            {
                try
                {
                    TraceError.Info("DB Init");
                    var exists = (from c in db.Clients
                                  join sub in db.Subscriptions on c.Subscription equals sub.Id
                                  where c.ChatId == userId &&
                                  sub.SubsctiptionType == (int)subscriptionType
                                  select c.Id
                                  ).Count();



                    if (exists == 0)
                    {
                        var subscription = new DatabaseInteractions.Subscription {
                            SubsctiptionType = (int)subscriptionType
                        };
                        db.Subscriptions.Add(subscription);
                        db.SaveChanges();
                        var client = new DatabaseInteractions.Client {
                            ChatId = userId, Subscription = subscription.Id
                        };
                        db.Clients.Add(client);
                        db.SaveChanges();

                        await _bot.SendTextMessageAsync(userId, $"You've subscribed to { subscriptionType.ToString() }!");
                    }
                    else
                    {
                        var clients = db.Clients.Where(x => x.ChatId == userId &&
                                                       db.Subscriptions.Any(y => y.Id == x.Subscription && y.SubsctiptionType == (int)subscriptionType));
                        foreach (var item in clients)
                        {
                            db.Clients.Remove(item);
                            var sub = db.Subscriptions.Where(x => x.Id == item.Subscription).FirstOrDefault();
                            db.Subscriptions.Remove(sub);
                        }
                        db.SaveChanges();
                        await _bot.SendTextMessageAsync(userId, $"Unsubscribed from { subscriptionType.ToString() }!");
                    }
                }
                catch (Exception e)
                {
                    TraceError.Info(e.Message);
                }
            }
        }