1

I use c# and telegram.bot library. When i use getUpdates method everything is ok but in webhook method not ok

in GetUpdates method when i write below code in OnCallbackQuery event everything is ok and bot get answers

  private static void Bot_OnCallbackQuery(object sender,        
                 Telegram.Bot.Args.CallbackQueryEventArgs e) 
                    {
                     long b;
        if (e.CallbackQuery != null && long.TryParse(e.CallbackQuery.Data, out b))//show Post
        {
            //PostContent
            var post = dba.BlogPosts.Find(Convert.ToInt64(e.CallbackQuery.Data));
            if (post != null)
            {
                string removedTag = Regex.Replace(post.Content, "<br>", Environment.NewLine);
                removedTag = Regex.Replace(removedTag, "<.*?>", String.Empty);
               // HtmlTagsRemover.CleanTagsExceptPbr(postContent.Content);

                Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, removedTag, parseMode: ParseMode.Html);
            }
        }
        else
        {
            if (e.CallbackQuery != null && e.CallbackQuery.Data.Contains("more_")) // user clicked on MoreButton
            {

                Post p = new Post();
                var posts = p.BlogPostPaging(PostsList, 5, moreCount);
                #region InlineKeyboard

                var inlineButtons = posts.Select(title => new[]
                        {InlineKeyboardButton.WithCallbackData(title.Subject, title.BlogPostId.ToString())})
                    .ToArray();

                InlinePostsKeyboard = new InlineKeyboardMarkup(inlineButtons);
                #endregion

                if (posts.Count>0)
                {
                    Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, "page: " + moreCount, replyMarkup: InlinePostsKeyboard);// ShowMoreButton
                    Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, "see More...", replyMarkup: InlineBtnMoreKeyboard);// MoreButton
                    moreCount++;

                }
                else
                {
                    Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id,
                        "End");
                }
    }

but when i want use above code in webhook method , bot does not work And no response is received from bot

#region QueryCallBack
            var e = update;

            long b;
            if (e.CallbackQuery != null && long.TryParse(e.CallbackQuery.Data, out b))//show post
            {
                await Bot.AnswerCallbackQueryAsync(e.CallbackQuery.Id, "test1");

                //post Content
                var post = _blogPost.EfGetOneBlogPost(b);
                if (post != null)
                {
                    var removedTag = Regex.Replace(post.Content, "<br>", Environment.NewLine);
                    removedTag = Regex.Replace(removedTag, "<.*?>", string.Empty);

                    await Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, removedTag, parseMode: ParseMode.Html);
                    return Ok();

                }
            }
            else
            {
                if (e.CallbackQuery != null && e.CallbackQuery.Data.Contains("more_")) // user clicked on MoreButton
                {

                    TelegramPostsPaging p = new TelegramPostsPaging();
                    var posts = p.BlogPostPaging(PostsList, 5, moreCount);
                    #region InlineKeyboard
                    var inlineButtons = posts.Select(title => new[]{InlineKeyboardButton.WithCallbackData(title.Subject, title.BlogPostId.ToString())}).ToArray();

                    InlinePostsKeyboard = new InlineKeyboardMarkup(inlineButtons);
                    #endregion

                    if (posts.Count > 0)
                    {
                        await Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, "page: " + moreCount, replyMarkup: InlinePostsKeyboard);// show SeeMore Button
                        await Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, "see More...", replyMarkup: InlineBtnMoreKeyboard);// show SeeMore Button
                        moreCount++;
                        return Ok();


                    }
                    else
                    {
                        await Bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id,
                             "End");
                        return Ok();

                    }


                }

            }

I don't know how to use CallBackQuery in webhook; but in update method i use in OnCallbackQuery event.

1 Answer 1

0

How are you getting the update in the webhook? Because the built-in serializer of ASP.NET does not correctly parse the update, you should instead take it as a string and then deserialize it using Newtonsoft.Json

3
  • Do you have a project for sample?
    – arman
    Oct 17, 2017 at 3:47
  • 1
    It's simple, actually. You get Newtonsoft.Json on nuget and use JsonConvert.DeserializeObject<Update>(thestringyougot) Oct 18, 2017 at 4:20
  • cannot convert from 'Telegram.Bot.Types.Update' to 'string' ,var updateConvert = JsonConvert.DeserializeObject<Update>(update);
    – arman
    Oct 24, 2017 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.