1

我使用 c# 和 telegram.bot 库。当我使用 getUpdates 方法时一切正常,但在 webhook 方法中不行

OnCallbackQuery在 GetUpdates 方法中,当我在事件一切正常并且机器人得到答案时编写下面的代码时

  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");
                }
    }

但是当我想在 webhook 方法中使用上面的代码时,bot 不起作用并且没有收到来自 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();

                    }


                }

            }

我不知道如何在 webhook 中使用 CallBackQuery;但在更新方法中,我在OnCallbackQuery事件中使用。

4

1 回答 1

0

您如何在 webhook 中获取更新?由于 ASP.NET 的内置序列化程序无法正确解析更新,因此您应该将其作为字符串,然后使用 Newtonsoft.Json 对其进行反序列化

于 2017-10-08T21:24:13.707 回答