如何在webhook方法电报Bot中使用Inlinekeyboard? C#

时间:2017-09-29 20:41:34

标签: c# telegram telegram-bot telegram-webhook

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

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

  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的响应

#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事件中使用。

1 个答案:

答案 0 :(得分:0)

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