Пример #1
0
        private async Task Subscribe(string result, long chatId)
        {
            if (string.IsNullOrEmpty(result))
            {
                await _tgService.SendMessage(chatId, Texts.CommandFormatShouldBeNext);
            }
            else
            {
                var group = await _vkService.GetGroupInfo(result);

                if (group == null)
                {
                    await _tgService.SendMessage(chatId, string.Format(Texts.GroupIsNotFound, result));

                    return;
                }
                var post = (await _vkService.GetPosts(-group.gid, 0, 1)).FirstOrDefault();
                if (post?.id == null)
                {
                    await _tgService.SendMessage(chatId, Texts.AccessDenied);

                    return;
                }
                var subscribed = await _dataService.AddSubscription(-group.gid, group.screen_name, group.name, post.id, chatId);

                await _tgService.SendMessage(chatId,
                                             subscribed
                                             ?string.Format(Texts.YouAreSubscribed, result)
                                             : string.Format(Texts.YouAreAlreadySubscribed, result)
                                             );
            }
        }
Пример #2
0
        private static async Task AsyncMain()
        {
            await _dataService.AddTraceLog("Sync started");

            var subscriptions = await _dataService.GetSubscriptionsToCheck();

            foreach (var subscription in subscriptions)
            {
                var posts = await _vkService.GetPosts(subscription.SubscriptionId, subscription.LastPostId);

                foreach (var post in posts)
                {
                    try
                    {
                        var lastLogDate = await _dataService.GetLastLogDate();

                        if (lastLogDate.Date < DateTime.Now.Date)
                        {
                            await DeleteOldBlobs();
                        }

//                        var link = await _telegraphService.CreatePage(post, subscription.SubscriptionName, subscription.SubscriptionPrettyName ?? subscription.SubscriptionName);
//                        await _dataService.AddLog(subscription.Id, post.id, link);
//
//                        foreach(var user in subscription.Users.Where(u => !u.HasBlocked))
//                        {
//                            try
//                            {
//                                await _tgService.SendMessage(user.ChatId, link);
//                            }
//                            catch(Exception e)
//                            {
//                                if(e.Message.Equals("Forbidden: bot was blocked by the user"))
//                                {
//                                    await _dataService.Block(user.ChatId);
//                                }
//                                else
//                                {
//                                    throw;
//                                }
//                            }
//                        }

                        await _dataService.SetLastPost(subscription.SubscriptionId, post.id);
                    }
                    catch (AggregateException a)
                    {
                        foreach (var exception in a.InnerExceptions)
                        {
                            try
                            {
                                await _dataService.AddErrorLog(exception);
                            }
                            catch (Exception) { }
                        }
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            await _dataService.AddErrorLog(e);
                        }
                        catch (Exception) { }
                    }
                }
            }
            await _dataService.AddTraceLog("Sync finished");
        }