【问题标题】:How can i use Inlinekeyboard in webhook method telegram Bot ? c#如何在 webhook 方法电报 Bot 中使用 Inlinekeyboard? C#
【发布时间】:2017-09-29 20:41:33
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 您有项目示例吗?
    • 其实很简单。您在 nuget 上获得 Newtonsoft.Json 并使用 JsonConvert.DeserializeObject(thestringyougot)
    • 无法从 'Telegram.Bot.Types.Update' 转换为 'string' ,var updateConvert = JsonConvert.DeserializeObject&lt;Update&gt;(update);
    猜你喜欢
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2017-07-31
    • 1970-01-01
    • 2018-10-17
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多