예제 #1
0
        public async Task <UpdateHandlingResult> HandleUpdateAsync(IBot bot, Update update)
        {
            var botUser = _botUserRepository.GetByTelegramId(update.CallbackQuery.Message.Chat.Id);

            if (botUser.VkAccount == null)
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Необходима авторизация");
            }

            var api = new VkApi();
            await api.AuthorizeAsync(new ApiAuthParams()
            {
                AccessToken = botUser.VkAccount.AcessToken
            });

            if (!long.TryParse(update.CallbackQuery.Data.Substring(Constants.MessageForward.Length),
                               out var forwardedMsgId))
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Сообщение не найдено");
            }

            await api.Messages.SendAsync(new MessagesSendParams()
            {
                PeerId          = botUser.VkAccount.CurrentPeer,
                Message         = "fwd",
                ForwardMessages = new long[] { forwardedMsgId }
            });

            await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Сообщение переслано в текущий диалог");

            return(UpdateHandlingResult.Handled);
        }
        public async Task <UpdateHandlingResult> HandleUpdateAsync(IBot bot, Update update)
        {
            var botUser = _botUserRepository.GetByTelegramId(update.CallbackQuery.Message.Chat.Id);

            if (botUser.VkAccount == null)
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Необходима авторизация");
            }

            var vkApi = new VkApi();
            await vkApi.AuthorizeAsync(new ApiAuthParams()
            {
                AccessToken = botUser.VkAccount.AcessToken
            });

            if (!ulong.TryParse(update.CallbackQuery.Data.Substring(Constants.MessageForward.Length),
                                out var msgToDeleteId))
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Сообщение не найдено");
            }

            try
            {
                vkApi.Messages.Delete(new ulong[] { msgToDeleteId }, false, true);
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Сообщение удалено");
            }
            catch (Exception e)
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Не получилось удалить сообщение");

                Console.WriteLine(e);
            }

            return(UpdateHandlingResult.Handled);
        }
예제 #3
0
        public override async Task <UpdateHandlingResult> HandleCommand(IBot bot, Update update,
                                                                        DialogsCommandArgs args)
        {
            var botUser = _botUserRepository.GetByTelegramId(update.Message.Chat.Id);

            if (botUser.VkAccount == null)
            {
                await bot.Client.SendTextMessageAsync(update.Message.Chat.Id, "`Необходима авторизация`", ParseMode.Markdown);

                return(UpdateHandlingResult.Handled);
            }

            var api = new VkApi();
            await api.AuthorizeAsync(new ApiAuthParams
            {
                AccessToken = botUser.VkAccount.AcessToken
            });

            var start         = HandleArgs(args);
            var dialogsMarkup = Markup.Dialogs(api, start, DefaultCount);

            await bot.Client.SendTextMessageAsync(
                update.Message.Chat.Id,
                "Выберете диалог:",
                replyMarkup : dialogsMarkup);

            return(UpdateHandlingResult.Handled);
        }
        public async Task <UpdateHandlingResult> HandleUpdateAsync(IBot bot, Update update)
        {
            var botUser = _botUserRepository.GetByTelegramId(update.CallbackQuery.Message.Chat.Id);

            if (botUser.VkAccount == null)
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Необходима авторизация");
            }

            await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Пока не доступно");

            return(UpdateHandlingResult.Handled);
        }
예제 #5
0
        public async Task <UpdateHandlingResult> HandleUpdateAsync(IBot bot, Update update)
        {
            if (!long.TryParse(update.TrimCallbackCommand(Constants.PickDialog), out var dialogId))
            {
                return(UpdateHandlingResult.Handled);
            }

            var botUser = _botUserRepository.GetByTelegramId(update.CallbackQuery.Message.Chat.Id);

            botUser.VkAccount.CurrentPeer = dialogId;

            await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, $"Выбран диалог: {dialogId}");

            return(UpdateHandlingResult.Handled);
        }
예제 #6
0
        public override async Task <UpdateHandlingResult> HandleCommand(IBot bot, Update update, AuthCommandArgs args)
        {
            var botUser = _botUserRepository.GetByTelegramId(update.Message.Chat.Id);

            if (botUser == null)
            {
                return(UpdateHandlingResult.Handled);
            }

            _notificationsService.DisableNotifications(botUser);
            botUser.VkAccount = null;

            await bot.Client.SendTextMessageAsync(botUser.TelegramChatId, "Log out");

            return(UpdateHandlingResult.Handled);
        }
예제 #7
0
        public async Task <UpdateHandlingResult> HandleUpdateAsync(IBot bot, Update update)
        {
            var botUser = _botUserRepository.GetByTelegramId(update.CallbackQuery.Message.Chat.Id);

            if (botUser.VkAccount == null)
            {
                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Необходима авторизация");
            }

            var vkApi = new VkApi();
            await vkApi.AuthorizeAsync(new ApiAuthParams()
            {
                AccessToken = botUser.VkAccount.AcessToken
            });

            // like / type / id / owner
            var args  = update.CallbackQuery.Data.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var type  = args[1];
            var id    = long.Parse(args[2]);
            var owner = long.Parse(args[3]);

            var isLiked = await vkApi.Likes.IsLikedAsync(LikeObjectType.Post, id, ownerId : owner);

            if (isLiked)
            {
                await vkApi.Likes.DeleteAsync(LikeObjectType.Post, id, owner);

                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Лайк убран");
            }
            else
            {
                await vkApi.Likes.AddAsync(new LikesAddParams()
                {
                    // Тут надо выбирать тип в зависимости от аргументов. А то тупо получается
                    Type    = LikeObjectType.Post,
                    ItemId  = id,
                    OwnerId = owner
                });

                await bot.Client.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Лайк добавлен");
            }

            return(UpdateHandlingResult.Handled);
        }
예제 #8
0
        public override async Task <UpdateHandlingResult> HandleCommand(IBot bot, Update update, AuthCommandArgs args)
        {
            if (args.ArgsInput.Split(" ", StringSplitOptions.RemoveEmptyEntries).Length != 1)
            {
                await bot.Client.SendTextMessageAsync(update.Message.Chat.Id,
                                                      $"`Формат команды: {Constants.CommandFormat}`", ParseMode.Markdown);

                return(UpdateHandlingResult.Handled);
            }

            var acessToken = args.ArgsInput;

            var botUser = _botUserRepository.GetByTelegramId(update.Message.Chat.Id);

            if (botUser == null)
            {
                botUser = new BotUser
                {
                    TelegramChatId = update.Message.Chat.Id,
                    TelegramUserId = update.Message.Chat.Id
                };
            }

            IReplyMarkup replyMarkup = new ReplyKeyboardRemove();

            try
            {
                var userInfo = botUser.AuthorizeVk(acessToken);
                await _notificationsService.EnableNotifications(botUser);

                _botUserRepository.Add(botUser);
                await bot.Client.SendTextMessageAsync(
                    update.Message.Chat.Id,
                    $"`Вы авторизованы как: {userInfo.FirstName} {userInfo.LastName}`",
                    ParseMode.Markdown,
                    replyMarkup : replyMarkup);
            }
            catch (Exception e)
            {
                await bot.Client.SendTextMessageAsync(update.Message.Chat.Id, e.Message);
            }

            return(UpdateHandlingResult.Handled);
        }
예제 #9
0
 public bool CanHandleUpdate(IBot bot, Update update)
 {
     return(update.Type == UpdateType.Message &&
            _botUserRepository.GetByTelegramId(update.Message.Chat.Id).VkAccount != null);;
 }