Esempio n. 1
0
        private async void TelegramBot_OnCallbackQuery(object _sender, CallbackQueryEventArgs _e)
        {
            if (_e.CallbackQuery?.Data == null)
            {
                return;
            }

            if (PostLikeHelper.TryParseLikeInfo(_e.CallbackQuery.Data, out var likeInfo))
            {
                try
                {
                    if (likeInfo.IsLiked)
                    {
                        await m_telegramBot.AnswerCallbackQueryAsync(_e.CallbackQuery.Id, text : "Отметка ❤ уже поставлена");

                        return;
                    }

                    var user = m_userManager.GetUser(_e.CallbackQuery.Message.Chat.Id.ToString());

                    if (user == null)
                    {
                        await m_telegramBot.AnswerCallbackQueryAsync(_e.CallbackQuery.Id, text : "Пользователь не найден");

                        return;
                    }

                    try
                    {
                        await m_telegramBot.AnswerCallbackQueryAsync(_e.CallbackQuery.Id, text : "Вам ❤ это");

                        likeInfo.IsLiked = true;

                        var likeButton = KeyBoardBuilder.BuildInlineKeyboard(new[]
                        {
                            new KeyValuePair <string, string>("✅❤", PostLikeHelper.SerializeInfo(likeInfo))
                        }) as InlineKeyboardMarkup;

                        await m_telegramBot.EditMessageReplyMarkupAsync(_e.CallbackQuery.Message.Chat.Id,
                                                                        _e.CallbackQuery.Message.MessageId, replyMarkup : likeButton);

                        m_vkApi.LikePost(-likeInfo.OwnerId, (uint)likeInfo.ItemId, user.Token);
                    }
                    catch (InvalidParameterException)
                    {
                        //too late response
                    }
                    catch (Exception ex)
                    {
                        likeInfo.IsLiked = false;

                        await m_telegramBot.AnswerCallbackQueryAsync(_e.CallbackQuery.Id, text : "Не удалось поставить ❤");

                        var likeButton = KeyBoardBuilder.BuildInlineKeyboard(new[]
                        {
                            new KeyValuePair <string, string>("❤", PostLikeHelper.SerializeInfo(likeInfo))
                        }) as InlineKeyboardMarkup;

                        await m_telegramBot.EditMessageReplyMarkupAsync(_e.CallbackQuery.Message.Chat.Id,
                                                                        _e.CallbackQuery.Message.MessageId, replyMarkup : likeButton);

                        Console.WriteLine(ex);
                    }
                }
                catch (InvalidParameterException)
                {
                    //too late response
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
Esempio n. 2
0
        private async Task SendMessage(long _userId, Post _post)
        {
            if (_post is null)
            {
                throw new ArgumentNullException(nameof(_post));
            }

            var text = $"Группа: {_post.GroupName} \n \n {_post.Text}";

            var serializedLikeInfo = PostLikeHelper.SerializeInfo(new LikeInfo
            {
                OwnerId = _post.GroupId,
                ItemId  = _post.PostId
            });

            var likeButton = KeyBoardBuilder.BuildInlineKeyboard(new[] { new KeyValuePair <string, string>("❤", serializedLikeInfo) });

            if (!_post.Items.Any())
            {
                await m_telegramBot.SendTextMessageAsync(_userId, text, replyMarkup : likeButton);
            }
            else
            {
                if (_post.Items.Length == 1)
                {
                    switch (_post.Items.First())
                    {
                    case ImageItem imageItem:
                        await m_telegramBot.SendPhotoAsync(_userId,
                                                           new InputOnlineFile(imageItem.UrlMedium ?? imageItem.UrlSmall), text, replyMarkup : likeButton);

                        break;

                    case VideoItem videoItem:
                        if (!string.IsNullOrEmpty(videoItem.Url))
                        {
                            await m_telegramBot.SendTextMessageAsync(_userId, $"{text}\n{videoItem.Url}", replyMarkup : likeButton);
                        }
                        break;

                    case DocumentItem documentItem:
                        await m_telegramBot.SendDocumentAsync(_userId,
                                                              new InputOnlineFile(documentItem.Url), $"{text}\n{documentItem.Title}", replyMarkup : likeButton);

                        break;

                    case LinkItem linkItem:
                        await m_telegramBot.SendTextMessageAsync(_userId, $"{text}\n{linkItem.Url}", replyMarkup : likeButton);

                        break;
                    }
                }
                else
                {
                    await m_telegramBot.SendTextMessageAsync(_userId, text, replyMarkup : likeButton);

                    var media = new List <IAlbumInputMedia>();

                    foreach (var postItem in _post.Items)
                    {
                        switch (postItem)
                        {
                        case ImageItem imageItem:
                            media.Add(new InputMediaPhoto(
                                          new InputMedia(imageItem.UrlLarge ?? imageItem.UrlMedium ?? imageItem.UrlSmall)));
                            break;

                        case VideoItem videoItem:
                            if (!string.IsNullOrEmpty(videoItem.Url))
                            {
                                media.Add(new InputMediaVideo(new InputMedia(videoItem.Url)));
                            }
                            break;
                        }
                    }
                    if (media.Any())
                    {
                        await m_telegramBot.SendMediaGroupAsync(media, _userId);
                    }
                }
            }
        }